RSS

Category Archives: Athena

Amazon Athena is an interactive query service that makes it easy to analyze data in Amazon S3 using standard SQL. Athena is serverless, so there is no infrastructure to manage, and you pay only for the queries that you run.

Athena is easy to use. Simply point to your data in Amazon S3, define the schema, and start querying using standard SQL. Most results are delivered within seconds. With Athena, there’s no need for complex ETL jobs to prepare your data for analysis. This makes it easy for anyone with SQL skills to quickly analyze large-scale datasets.

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”;
}

Advertisements
 
Leave a comment

Posted by on July 20, 2017 in Athena, AWS

 

Tags: , , ,