我需要从我的 PHP 应用程序之一对 AWS Athena 运行查询。我使用了 AWS 以及另一个论坛的文档来尝试编译实现此目的所需的代码。您能否检查一下代码并在必要时进行验证/评论/更正?除了 waitForSucceeded() 函数之外,大部分代码对我来说都有意义?我从来没有见过这样定义的函数?
require "/var/www/app/vendor/autoload.php";
use Aws\Athena\AthenaClient;
$options = [
'version' => 'latest',
'region' => 'eu-north-1',
'credentials' => [
'key' => '12345',
'secret' => '12345'
];
$athenaClient = new Aws\Athena\AthenaClient($options);
$databaseName = 'database';
$catalog = 'AwsDataCTLG';
$sql = 'select * from database limit 3';
$outputS3Location = 's3://BUCKET_NAME/';
$startQueryResponse = $athenaClient->startQueryExecution([
'QueryExecutionContext' => [
'Catalog' => $catalog,
'Database' => $databaseName
],
'QueryString' => $sql,
'ResultConfiguration' => [
'OutputLocation' => $outputS3Location
]
]);
$queryExecutionId = $startQueryResponse->get('QueryExecutionId');
var_dump($queryExecutionId);
$waitForSucceeded = function () use ($athenaClient, $queryExecutionId, &$waitForSucceeded) {
$getQueryExecutionResponse = $athenaClient->getQueryExecution([
'QueryExecutionId' => $queryExecutionId
]);
$status = $getQueryExecutionResponse->get('QueryExecution')['Status']['State'];
print("[waitForSucceeded] State=$status\n");
return $status === 'SUCCEEDED' || $waitForSucceeded();
};
$waitForSucceeded();
$getQueryResultsResponse = $athenaClient->getQueryResults([
'QueryExecutionId' => $queryExecutionId
]);
var_dump($getQueryResultsResponse->get('ResultSet'));
从目前来看,它应该可以正常工作。你有什么执行日志?
waitForSucceeded()
是一个闭包,又名匿名函数。
您可以在这里找到一些文档/详细信息:
https://www.php.net/manual/fr/functions.anonymous.php
https://www.php.net/manual/fr/class.closure.php
这就是闭包的作用:
// Declare your closure and inject scope that will be use inside
$waitForSucceeded = function () use ($athenaClient, $queryExecutionId, &$waitForSucceeded) {
$getQueryExecutionResponse = $athenaClient->getQueryExecution([
'QueryExecutionId' => $queryExecutionId
]);
$status = $getQueryExecutionResponse->get('QueryExecution')['Status']['State'];
print("[waitForSucceeded] State=$status\n");
// If status = SUCCEEDED, return some result, else relaunch the function
return $status === 'SUCCEEDED' || $waitForSucceeded();
};
// Launch the function, which must return true when $status === 'SUCCEEDED'
$waitForSucceeded();
$getQueryResultsResponse = $athenaClient->getQueryResults([
'QueryExecutionId' => $queryExecutionId
]);
var_dump($getQueryResultsResponse->get('ResultSet'));