1

I am trying to run a athena query using aws php sdk. However I am getting an empty array as a result. Can someone please point me on what's wrong with my code

require '/usr/bin/vendor/autoload.php';

use Aws\Athena\AthenaClient;


$options = [
    'version' => 'latest',
    'region'  => 'eu-west-1',
    'credentials' => [
       'key'    => 'mykey',
       'secret' => 'mysecret']
 ];
 $athenaClient = new Aws\Athena\AthenaClient($options);

$result = $athenaClient->startQueryExecution([
    'QueryExecutionContext' => [
        'Database' => 'mydbname',
    ],
    'QueryString' => 'select * from mytable limit 3', // REQUIRED
    'ResultConfiguration' => [ // REQUIRED
        'EncryptionConfiguration' => [
            'EncryptionOption' => 'SSE_S3' // REQUIRED
        ],
        'OutputLocation' => 's3://mybucket/', // REQUIRED
    ],
]);

print_r($result);

Below is the result I am seeing

Aws\Result Object ( [data:Aws\Result:private] => Array ( [QueryExecutionId] => 21221212121212121 [@metadata] => Array ( [statusCode] => 200 [effectiveUri] => https://athena.eu-west-1.amazonaws.com [headers] => Array ( [date] => Tue, 28 Feb 2023 16:09:57 GMT [content-type] => application/x-amz-json-1.1 [content-length] => 59 [connection] => keep-alive [x-amzn-requestid] => 3232323232323232 ) [transferStats] => Array ( [http] => Array ( [0] => Array ( ) ) ) ) ) [monitoringEvents:Aws\Result:private] => Array ( ) )
Santosh Pillai
  • 1,311
  • 1
  • 20
  • 31

1 Answers1

0

For Athena queries, you need to start the execution, wait for the query to finish, then get the results. Check out the docs for example code and more explanation: https://docs.aws.amazon.com/athena/latest/ug/code-samples.html

For your query, you do successfully get the QueryExecutionId in your $result variable, which is the only thing StartQueryExecution is supposed to return. Check out the API definition for that here: https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-athena-2017-05-18.html#startqueryexecution

Then you can use GetQueryExecution to check the status: https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-athena-2017-05-18.html#getqueryexecution

And finally, get the results of your query with GetQueryResults. https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-athena-2017-05-18.html#getqueryresults

JasonQ-AWS
  • 36
  • 4