PHP AWS Athena: Need to execute queries against athena

Viewed 732

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'));
1 Answers

From what is can see, it should work properly. What log do you have on execution?

waitForSucceeded() is a closure, aka anopnymous function. You can find some documentation/ detail here: https://www.php.net/manual/fr/functions.anonymous.php https://www.php.net/manual/fr/class.closure.php

So here is what the closure do:

// 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'));
Related