🚀 Languege/php

[AWS SDK for PHP 3] php sdk로 Athena 쿼리해보기

mini_world 2021. 8. 5. 16:31
목차 접기

안녕하세요 🤗

오늘은 문서가 많지 않아서 애를 먹은.... php sdk를 이용한 Athena 쿼리 입니다!!! 

<?php
/*---------------------------
 * AWS SDK 로드 &  클라이언트 인스턴스 생성
----------------------------- */
require './vendor/autoload.php';
$options = [
    'region' => 'ap-northeast-2',
    'version' => 'latest',
];

$athenaClient = new Aws\Athena\AthenaClient($options);

/*---------------------------
 * SQL 실행
----------------------------- */

$result = $athenaClient->startQueryExecution([
    'QueryExecutionContext' => [
        'Database' => 'default', //athena 데이터베이스 이름 쓰세요!
    ],
    'QueryString' => 'select * from cloudfront_logs limit 10', //여기에 쿼리 쓰세요!
    'ResultConfiguration' => [
        'OutputLocation' => 's3://athena-sdk-result/' , //결과 저장될 S3 경로 쓰세요!
    ],
    'WorkGroup' => 'primary', // 워크그룹 쓰세요!!
]);
$result_QueryExecutionId = $result['QueryExecutionId'];

/*---------------------------
 * While 루프돌면서 SQL 쿼리 상태 확인
----------------------------- */
$stat_flg = true;
while ($stat_flg){
    $result = $athenaClient->getQueryExecution([
        'QueryExecutionId' => $result_QueryExecutionId
    ]);

    // Debug
    echo "Query status = ".$result['QueryExecution']['Status']['State'] ."\n";
    if ($result['QueryExecution']['Status']['State'] == 'SUCCEEDED'){
        $stat_flg = false;
    }
}

/*---------------------------
 * SQL 결과 확인
----------------------------- */
$result = $athenaClient->getQueryResults([
    'QueryExecutionId'  => $result_QueryExecutionId
]);

print ($result);

?>

 

 

728x90