PHP AWS Athena:需要对 athena 执行查询

问题描述

我需要从我的一个 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'));