I need to run queries against AWS Athena from one of my PHP applications. I have used the documentation from AWS as well as another forum to try and compile the code I need to achieve this. Can you please go through the code and validate/comment/correct where necessary? Most of the code makes sense to me except for the waitForSucceeded() function? I have never seen a function defined this way?
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'));