微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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'));

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。