gpt4 book ai didi

PHP AWS 雅典娜 : Need to execute queries against athena

转载 作者:行者123 更新时间:2023-12-04 12:55:44 27 4
gpt4 key购买 nike

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

关于PHP AWS 雅典娜 : Need to execute queries against athena,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67916344/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com