| 
 | 1 | +---  | 
 | 2 | +layout: client  | 
 | 3 | +category: clients  | 
 | 4 | +name: Athena  | 
 | 5 | +package: async-aws/athena  | 
 | 6 | +---  | 
 | 7 | + | 
 | 8 | +## Usage  | 
 | 9 | + | 
 | 10 | +### List Databases  | 
 | 11 | + | 
 | 12 | +```php  | 
 | 13 | +use AsyncAws\Athena\AthenaClient;  | 
 | 14 | +use AsyncAws\Athena\Input\ListDatabasesInput;  | 
 | 15 | + | 
 | 16 | +$athena = new AthenaClient();  | 
 | 17 | + | 
 | 18 | +$result = $athena->listDatabases(new ListDatabasesInput([  | 
 | 19 | +    'CatalogName' => 'my_catalog'  | 
 | 20 | +]));  | 
 | 21 | + | 
 | 22 | +foreach ($result->getDatabaseList() as $database) {  | 
 | 23 | +    echo 'Database name : ' . $database->getName();  | 
 | 24 | +    echo 'Database description : ' . $database->getDescription();  | 
 | 25 | +    echo 'Database parameter : '.PHP_EOL;  | 
 | 26 | +    print_r($database->getParameters());  | 
 | 27 | +}  | 
 | 28 | + | 
 | 29 | +```  | 
 | 30 | +more information [listDatabases](https://docs.aws.amazon.com/athena/latest/APIReference/API_ListDatabases.html)  | 
 | 31 | + | 
 | 32 | +### Query to Amazon Athena  | 
 | 33 | + | 
 | 34 | +```php  | 
 | 35 | +use AsyncAws\Athena\AthenaClient;  | 
 | 36 | +use AsyncAws\Athena\Input\StartQueryExecutionInput;  | 
 | 37 | +use AsyncAws\Athena\Input\DescribeTableInput;  | 
 | 38 | +use AsyncAws\Athena\ValueObject\QueryExecutionContext;  | 
 | 39 | +use AsyncAws\Athena\ValueObject\ResultConfiguration;  | 
 | 40 | +use AsyncAws\Athena\ValueObject\EncryptionConfiguration;  | 
 | 41 | +use AsyncAws\Athena\ValueObject\AclConfiguration;  | 
 | 42 | +use AsyncAws\Athena\ValueObject\ResultReuseByAgeConfiguration;  | 
 | 43 | +use AsyncAws\Athena\Input\GetQueryExecutionInput;  | 
 | 44 | +use AsyncAws\Athena\Input\GetQueryResultsInput;  | 
 | 45 | +use AsyncAws\Athena\ValueObject\Row;  | 
 | 46 | +use AsyncAws\Athena\ValueObject\Datum;  | 
 | 47 | +use AsyncAws\Athena\Enum\QueryExecutionState;  | 
 | 48 | + | 
 | 49 | +$athena = new AthenaClient();  | 
 | 50 | + | 
 | 51 | +// Submits a sample query to Amazon Athena and returns the execution ID of the query.  | 
 | 52 | +$startQueryResult = $athena->startQueryExecution(new StartQueryExecutionInput([  | 
 | 53 | +        'QueryString' => 'select * from product limit 30',  | 
 | 54 | +        'QueryExecutionContext' => new QueryExecutionContext([  | 
 | 55 | +             'Database' => 'production_db', // REQUIRED  | 
 | 56 | +        ]),  | 
 | 57 | +        'ResultConfiguration' => new ResultConfiguration([  | 
 | 58 | +            'OutputLocation' => 's3://output_bucket_Location', // REQUIRED  | 
 | 59 | +            'EncryptionConfiguration' => new EncryptionConfiguration([  | 
 | 60 | +                'EncryptionOption' => 'SSE_S3', // REQUIRED SSE_S3|SSE_KMS|CSE_KMS  | 
 | 61 | +            ])  | 
 | 62 | +        ]),  | 
 | 63 | +]));  | 
 | 64 | + | 
 | 65 | + | 
 | 66 | +// Wait for an Amazon Athena query to complete, fail or to be cancelled.  | 
 | 67 | +$isQueryStillRunning = true;  | 
 | 68 | +while ($isQueryStillRunning) {  | 
 | 69 | +    $queryExecutionResult = $athena->getQueryExecution( new GetQueryExecutionInput([  | 
 | 70 | +        'QueryExecutionId' => $startQueryResult->getQueryExecutionId(), // REQUIRED  | 
 | 71 | +    ]));  | 
 | 72 | +    $queryState=$queryExecutionResult->getQueryExecution()->getStatus()->getState();  | 
 | 73 | +    if($queryState === QueryExecutionState::FAILED) {  | 
 | 74 | +        throw new \Exception(  | 
 | 75 | +        'Athena query failed to run with error message: '.$queryExecutionResult->getQueryExecution()->getStatus()->getStateChangeReason()  | 
 | 76 | +        )  | 
 | 77 | +    } elseif ($queryState === QueryExecutionState::CANCELLED) {  | 
 | 78 | +        throw  new \Exception('Athena query was cancelled.')  | 
 | 79 | +    } elseif ($queryState === QueryExecutionState::SUCCEEDED) {  | 
 | 80 | +        $isQueryStillRunning = false;  | 
 | 81 | +    }  | 
 | 82 | +    echo 'The current status is: : ' . $queryState;  | 
 | 83 | +}  | 
 | 84 | + | 
 | 85 | + | 
 | 86 | +// retrieves the results of a query  | 
 | 87 | +$results = $athena->getQueryResults(new GetQueryResultsInput([  | 
 | 88 | +    'QueryExecutionId' => $startQueryResult->getQueryExecutionId(),  | 
 | 89 | +    'MaxResults' => 10000  | 
 | 90 | +]));  | 
 | 91 | + | 
 | 92 | +/** @var Row $row */  | 
 | 93 | +foreach ($results => $row) {  | 
 | 94 | +    if ($index === 0) {  | 
 | 95 | +        $columnLabels = array_column($row->getData(), 'VarCharValue'); // $row->getData() return [ 'VarCharValue' => value]  | 
 | 96 | +    }  | 
 | 97 | +    $columnValues[] = array_column($row->getData(), 'VarCharValue');  | 
 | 98 | +}  | 
 | 99 | + | 
 | 100 | +// retrieves the results column structure details  | 
 | 101 | +$columnsDetail = $result->getResultSet()->getResultSetMetadata()->getColumnInfo();  | 
 | 102 | + | 
 | 103 | +print_r($columnsDetail);  | 
 | 104 | +```  | 
0 commit comments