5

我正在寻找一个命令行工具来查询 Amazon Athena。

它适用于 JDBC,使用驱动程序com.amazonaws.athena.jdbc.AthenaDriver,但我还没有找到任何适用于它的命令行工具。

4

4 回答 4

7

扩展来自@MasonWinsauer 的先前答案。需要 bash 和 jq。

    #!/bin/bash

    # Athena queries are fundamentally Asynchronous.  So we have to :
    # 1) Make the query, and tell Athena where in s3 to put the results (tell it the same place as the UI uses).
    # 2) Wait for the query to finish
    # 3) Pull down the results and un-wacky-Jsonify them.


    # run the query, use jq to capture the QueryExecutionId, and then capture that into bash variable
    queryExecutionId=$(
        aws athena start-query-execution \
        --query-string "SELECT Count(*) AS numBooks FROM books" \
        --query-execution-context "Database=demo_books" \
        --result-configuration "OutputLocation"="s3://whatever_is_in_the_athena_UI_settings" \
        --region us-east-1 | jq -r ".QueryExecutionId"
    )

    echo "queryExecutionId was ${queryExecutionId}"


    # Wait for the query to finish running.
    # This will wait for up to 60 seconds (30 * 2)
    for i in $(seq 1 30); do

        queryState=$(
            aws athena get-query-execution --query-execution-id "${queryExecutionId}"  --region us-east-1 | jq -r ".QueryExecution.Status.State"
        );

        if [[ "${queryState}" == "SUCCEEDED" ]]; then
            break;
        fi;

        echo "  Awaiting queryExecutionId ${queryExecutionId} - state was ${queryState}"

        if [[ "${queryState}" == "FAILED" ]]; then
            # exit with "bad" error code
            exit 1;
        fi;

        sleep 2
    done


    # Get the results.
    aws athena get-query-results \
        --query-execution-id "${queryExecutionId}" \
        --region us-east-1 > numberOfBooks_wacky.json

    # Todo un-wacky the json with jq or something
    # cat numberOfBooks_wacky.json | jq -r ".ResultSet.Rows[] | .Data[0].VarCharValue"
于 2018-03-08T03:49:05.603 回答
3

从版本 1.11.89 开始,AWS 命令​​行工具支持 Amazon Athena 操作。

首先,您需要将 AmazonAthenaFullAccess 策略附加到调用用户的 IAM 角色。

然后,要开始查询,您将使用以下start-query-execution命令:

aws athena start-query-execution 
    --query-string "SELECT * FROM MyDb.MyTable" 
    --result-configuration "OutputLocation"="s3://MyBucket/logs" [Optional: EncryptionConfiguration]
    --region <region>

这将返回 QueryExecutionId 的 JSON 对象,该对象可用于使用以下命令检索查询结果:

aws athena get-query-results
    --query-execution-id <id>
    --region <region>

它还返回结果和元数据的 JSON 对象。

更多信息可以在官方AWS 文档中找到。

希望这可以帮助!

于 2017-06-29T21:29:16.367 回答
2

您可以尝试 AthenaCLI,它是 Athena 服务的命令行客户端,可以进行自动补全和语法高亮,并且是 dbcli 社区的骄傲成员

https://github.com/dbcli/athenacli

于 2018-09-20T20:07:25.937 回答
0

athena-cli应该是一个好的开始。

于 2018-03-26T03:16:40.273 回答