Athena client
This page contains examples with the Athena client. See the client introduction for a more detailed description how to use a client. You may also want to consider the authentication documentation to understand the many ways you can authenticate with AWS.
The Athena package could be installed with Composer.
composer require async-aws/athena
A new client object may be instantiated by:
use AsyncAws\Athena\AthenaClient;
$athena = new AthenaClient();
The authentication parameters is read from the environment by default. You can also specify a AWS access id and secret:
use AsyncAws\Athena\AthenaClient;
$athena = new AthenaClient([
'accessKeyId' => 'my_access_key',
'accessKeySecret' => 'my_access_secret',
'region' => 'eu-central-1',
]);
For all available options, see the configuration reference.
Usage¶
List Databases¶
use AsyncAws\Athena\AthenaClient;
use AsyncAws\Athena\Input\ListDatabasesInput;
$athena = new AthenaClient();
$result = $athena->listDatabases(new ListDatabasesInput([
'CatalogName' => 'my_catalog'
]));
foreach ($result->getDatabaseList() as $database) {
echo 'Database name : ' . $database->getName();
echo 'Database description : ' . $database->getDescription();
echo 'Database parameter : '.PHP_EOL;
print_r($database->getParameters());
}
more information listDatabases
Query to Amazon Athena¶
use AsyncAws\Athena\AthenaClient;
use AsyncAws\Athena\Input\StartQueryExecutionInput;
use AsyncAws\Athena\Input\DescribeTableInput;
use AsyncAws\Athena\ValueObject\QueryExecutionContext;
use AsyncAws\Athena\ValueObject\ResultConfiguration;
use AsyncAws\Athena\ValueObject\EncryptionConfiguration;
use AsyncAws\Athena\ValueObject\AclConfiguration;
use AsyncAws\Athena\ValueObject\ResultReuseByAgeConfiguration;
use AsyncAws\Athena\Input\GetQueryExecutionInput;
use AsyncAws\Athena\Input\GetQueryResultsInput;
use AsyncAws\Athena\ValueObject\Row;
use AsyncAws\Athena\ValueObject\Datum;
use AsyncAws\Athena\Enum\QueryExecutionState;
$athena = new AthenaClient();
// Submits a sample query to Amazon Athena and returns the execution ID of the query.
$startQueryResult = $athena->startQueryExecution(new StartQueryExecutionInput([
'QueryString' => 'select * from product limit 30',
'QueryExecutionContext' => new QueryExecutionContext([
'Database' => 'production_db', // REQUIRED
]),
'ResultConfiguration' => new ResultConfiguration([
'OutputLocation' => 's3://test_output_bucket', // REQUIRED
'EncryptionConfiguration' => new EncryptionConfiguration([
'EncryptionOption' => 'SSE_S3', // REQUIRED
])
]),
]));
// Wait for an Amazon Athena query to complete, fail or to be cancelled.
$isQueryStillRunning = true;
while ($isQueryStillRunning) {
$queryExecutionResult = $athena->getQueryExecution( new GetQueryExecutionInput([
'QueryExecutionId' => $startQueryResult->getQueryExecutionId(), // REQUIRED
]));
$queryState=$queryExecutionResult->getQueryExecution()->getStatus()->getState();
if($queryState === QueryExecutionState::FAILED) {
throw new \Exception(
'Athena query failed to run with error message: '.$queryExecutionResult->getQueryExecution()->getStatus()->getStateChangeReason()
)
} elseif ($queryState === QueryExecutionState::CANCELLED) {
throw new \Exception('Athena query was cancelled.')
} elseif ($queryState === QueryExecutionState::SUCCEEDED) {
$isQueryStillRunning = false;
}
echo 'The current status is: : ' . $queryState;
}
// retrieves the results of a query
$results = $athena->getQueryResults(new GetQueryResultsInput([
'QueryExecutionId' => $startQueryResult->getQueryExecutionId()
]));
// Check results structure
var_dump($results);
//Retrieve results rows values as Array
/** @var Row $row */
foreach ($results as $row) {
$resultRowsToArray [] = array_map(
static function ($data) {
return (string)$data->getVarCharValue();
},
$row->getData()
);
}
var_dump($resultRowsToArray);
// retrieves the results column structure details
$columnsDetail = $result->getResultSet()->getResultSetMetadata()->getColumnInfo();
var_dump($columnsDetail);
The source code to this page is found on GitHub.