๐Ÿš€ 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