RSS

Tag Archives: AWS Athena

How to fetch data from AWS Athena using PHP

// https://aws.amazon.com/documentation/sdk-for-php/

require_once ‘aws/aws-autoloader.php’;

use Aws\Athena\AthenaClient;

try {
$Client = AthenaClient::factory(array(
‘version’ => ‘latest’,
‘region’ => ‘REGION’,
‘credentials’ => array(
‘key’ => ‘KEY’,
‘secret’ => ‘SECRET_KEY’,
)
));

$result1 = $Client->StartQueryExecution(
array(
“QueryExecutionContext”=> array( “Database”=> “DB_NAME” ),
“QueryString”=> “SELECT * FROM TABLE_NAME”,
“ResultConfiguration”=> array(
“EncryptionConfiguration”=> array(“EncryptionOption”=> “SSE_S3”),
“OutputLocation”=> “S3 Path” // like s3://BUCKET_NAME/FOLDER_PATH
)
)
);

// wait till query execution complete on Athena
while(1)
{
$result = $Client->getQueryExecution(array(‘QueryExecutionId’ => $result1->get(‘QueryExecutionId’)));
$res = $result->toArray();

if($res[‘QueryExecution’][‘Status’][‘State’]==’FAILED’)
{
echo ‘Query Failed’;
die;
}
else if($res[‘QueryExecution’][‘Status’][‘State’]==’CANCELED’)
{
echo ‘Query was cancelled’;
die;
}
else if($res[‘QueryExecution’][‘Status’][‘State’]==’SUCCEEDED’)
{
break; // break while loop
}

}

$result1 = $Client->GetQueryResults(array(
‘QueryExecutionId’ => $result1->get(‘QueryExecutionId’) // REQUIRED
));

echo ‘<pre>’;
print_r($result1);
echo ‘</pre>’;
die;

}
catch (AthenaException $e) {
// Catch an S3 specific exception.
echo $e->getMessage();
} catch (AwsException $e) {
// This catches the more generic AwsException. You can grab information
// from the exception using methods of the exception object.
echo $e->getAwsRequestId() . “\n”;
echo $e->getAwsErrorType() . “\n”;
echo $e->getAwsErrorCode() . “\n”;
}

 
Leave a comment

Posted by on July 20, 2017 in Athena, AWS

 

Tags: , , ,