ちょっと話題の記事

Very cool らしい AthenaCLI を試してみたら、やっぱりクールだった件

Very cool らしい AthenaCLI を見つけてしまったので、早速試してみたいと思います。
2018.10.19

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

はじめに

Very cool らしい AthenaCLI を見つけてしまったので、早速試してみたいと思います。

導入

インストール

インストールした環境は、macOS High Sierra / バージョン10.13.6、python3.6です。dbcli/athenacliに書いてあるとおり、pipでインストールします。

$ pip install athenacli
Collecting athenacli
  Downloading https://files.pythonhosted.org/packages/cd/b1/15d38fd6af41b8b83a2eeee90d5b0ab8c9112d75248afaf413711e7b23fc/athenacli-0.1.3-py2.py3-none-any.whl (46kB)
    100% |████████████████████████████████| 51kB 550kB/s
Requirement already satisfied: boto3>=1.4.4 in ./.pyenv/versions/anaconda3-5.0.1/lib/python3.6/site-packages (from athenacli) (1.7.62)
Collecting cli-helpers[styles]>=1.0.1 (from athenacli)
  Downloading https://files.pythonhosted.org/packages/60/69/8bfe43ea99e63d1ae302acd8152eb58618d4f734e8230593e256dfcd9772/cli_helpers-1.1.0-py3-none-any.whl
Requirement already satisfied: configobj>=5.0.5 in ./.pyenv/versions/anaconda3-5.0.1/lib/python3.6/site-packages (from athenacli) (5.0.6)
Requirement already satisfied: click>=4.1 in ./.pyenv/versions/anaconda3-5.0.1/lib/python3.6/site-packages (from athenacli) (6.7)
Requirement already satisfied: Pygments>=1.6 in ./.pyenv/versions/anaconda3-5.0.1/lib/python3.6/site-packages (from athenacli) (2.2.0)
Requirement already satisfied: prompt-toolkit<1.1.0,>=1.0.10 in ./.pyenv/versions/anaconda3-5.0.1/lib/python3.6/site-packages (from athenacli) (1.0.15)
Collecting PyAthena>=1.2.2 (from athenacli)
  Downloading https://files.pythonhosted.org/packages/eb/0e/20f8adb8812b7f559448ef08cfd4042309536ec78a189e1a96e47145da7f/PyAthena-1.4.2-py2.py3-none-any.whl (47kB)
    100% |████████████████████████████████| 51kB 1.3MB/s
Collecting sqlparse<0.3.0,>=0.2.2 (from athenacli)
  Downloading https://files.pythonhosted.org/packages/65/85/20bdd72f4537cf2c4d5d005368d502b2f464ede22982e724a82c86268eda/sqlparse-0.2.4-py2.py3-none-any.whl
Requirement already satisfied: botocore>=1.5.52 in ./.pyenv/versions/anaconda3-5.0.1/lib/python3.6/site-packages (from athenacli) (1.12.18)
Requirement already satisfied: s3transfer<0.2.0,>=0.1.10 in ./.pyenv/versions/anaconda3-5.0.1/lib/python3.6/site-packages (from boto3>=1.4.4->athenacli) (0.1.13)
Requirement already satisfied: jmespath<1.0.0,>=0.7.1 in ./.pyenv/versions/anaconda3-5.0.1/lib/python3.6/site-packages (from boto3>=1.4.4->athenacli) (0.9.3)
Collecting terminaltables>=3.0.0 (from cli-helpers[styles]>=1.0.1->athenacli)
  Downloading https://files.pythonhosted.org/packages/9b/c4/4a21174f32f8a7e1104798c445dacdc1d4df86f2f26722767034e4de4bff/terminaltables-3.1.0.tar.gz
Collecting tabulate[widechars]>=0.8.2 (from cli-helpers[styles]>=1.0.1->athenacli)
  Downloading https://files.pythonhosted.org/packages/12/c2/11d6845db5edf1295bc08b2f488cf5937806586afe42936c3f34c097ebdc/tabulate-0.8.2.tar.gz (45kB)
    100% |████████████████████████████████| 51kB 1.2MB/s
Requirement already satisfied: six in ./.pyenv/versions/anaconda3-5.0.1/lib/python3.6/site-packages (from configobj>=5.0.5->athenacli) (1.11.0)
Requirement already satisfied: wcwidth in ./.pyenv/versions/anaconda3-5.0.1/lib/python3.6/site-packages (from prompt-toolkit<1.1.0,>=1.0.10->athenacli) (0.1.7)
Collecting future (from PyAthena>=1.2.2->athenacli)
  Downloading https://files.pythonhosted.org/packages/00/2b/8d082ddfed935f3608cc61140df6dcbf0edea1bc3ab52fb6c29ae3e81e85/future-0.16.0.tar.gz (824kB)
    100% |████████████████████████████████| 829kB 869kB/s
Collecting tenacity>=4.1.0 (from PyAthena>=1.2.2->athenacli)
  Downloading https://files.pythonhosted.org/packages/b5/02/f912867529807b879972d8000e23c2f67b8b3755171e1d3c2049e347a3c9/tenacity-5.0.2-py2.py3-none-any.whl
Requirement already satisfied: urllib3<1.24,>=1.20 in ./.pyenv/versions/anaconda3-5.0.1/lib/python3.6/site-packages (from botocore>=1.5.52->athenacli) (1.22)
Requirement already satisfied: docutils>=0.10 in ./.pyenv/versions/anaconda3-5.0.1/lib/python3.6/site-packages (from botocore>=1.5.52->athenacli) (0.14)
Requirement already satisfied: python-dateutil<3.0.0,>=2.1; python_version >= "2.7" in ./.pyenv/versions/anaconda3-5.0.1/lib/python3.6/site-packages (from botocore>=1.5.52->athenacli) (2.6.1)
Building wheels for collected packages: terminaltables, tabulate, future
  Running setup.py bdist_wheel for terminaltables ... done
  Stored in directory: /Users/macuser/Library/Caches/pip/wheels/30/6b/50/6c75775b681fb36cdfac7f19799888ef9d8813aff9e379663e
  Running setup.py bdist_wheel for tabulate ... done
  Stored in directory: /Users/macuser/Library/Caches/pip/wheels/2a/85/33/2f6da85d5f10614cbe5a625eab3b3aebfdf43e7b857f25f829
  Running setup.py bdist_wheel for future ... done
  Stored in directory: /Users/macuser/Library/Caches/pip/wheels/bf/c9/a3/c538d90ef17cf7823fa51fc701a7a7a910a80f6a405bf15b1a
Successfully built terminaltables tabulate future
Installing collected packages: terminaltables, tabulate, cli-helpers, future, tenacity, PyAthena, sqlparse, athenacli
Successfully installed PyAthena-1.4.2 athenacli-0.1.3 cli-helpers-1.1.0 future-0.16.0 sqlparse-0.2.4 tabulate-0.8.2 tenacity-5.0.2 terminaltables-3.1.0
You are using pip version 18.0, however version 18.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

設定

設定ファイルは~/.athenacli/athenaclirc、最初の起動時に自動的に作成されます。

$ athenacli

        Welcome to athenacli!

        It seems this is your first time to run athenacli,
        we generated a default config file for you
            ~/.athenacli/athenaclirc
        Please change it accordingly, and run athenacli again.

設定ファイル ~/.athenacli/athenaclirc の先頭を以下のように書き換えます。

[aws_profile default]
# If you are a user of aws cli, you might want to use some configurations of aws cli,
# please refer to https://athenacli.readthedocs.io/en/latest/awsconfig.html for more infomation.
aws_access_key_id = 'AKIXX99XX9XXXXX9X63X'
aws_secret_access_key = 'apr+iJ1TXn93//ehnS6caKOslgksfOzGczIwk7fOD'
region = 'ap-northeast-1' # e.g us-west-2, us-east-1

# Amazon S3 staging directory where query results are stored
# NOTE: S3 should in the same region as specified above.
# The format is 's3://<your s3 directory path>'
s3_staging_dir = 's3://aws-athena-query-results-123456789012-ap-northeast-1/'

s3_staging_dirは、Athenaコンソールの Query result location を指定しました。

今回は、~/.athenacli/athenaclircにクレデンシャルを記入しましたが、下記のように環境変数に定義しても良いです。

$ export AWS_ACCESS_KEY_ID=YOUR_ACCESS_KEY_ID
$ export AWS_SECRET_ACCESS_KEY=YOUR_SECRET_ACCESS_KEY
$ export AWS_DEFAULT_REGION=ap-northeast-1
$ export AWS_ATHENA_S3_STAGING_DIR=s3://YOUR_S3_BUCKET/path/to/

athenacliを使ってみた!

クエリの実行

athenacliコマンドの引数に、クエリを指定します。クエリのテーブル名のみ指定した場合、defaultデータベースが補完されます。awscliコマンドはクエリを実行してトークンを受け取り〜トークンでステータスをポーリング〜クエリが終わったらクエリ結果を取りにいくという流れを自分で実装する必要がありますが、athenacliならクエリ実行が終わるまでブロッキングするのでスクリプトからの呼び出しに使ってもコードが簡潔になり、便利ではないかと思います。

$ athenacli -e 'select * from elb_parquet limit 3'
request_timestamp,elb_name,request_ip,request_port,backend_ip,backend_port,request_processing_time,backend_processing_time,client_response_time,elb_response_code,backend_response_code,received_bytes,sent_bytes,request_verb,url,protocol,user_agent,ssl_cipher,ssl_protocol,day
2015-01-30T10:47:46.052219Z,elb_demo_008,250.112.162.45,8518,172.43.157.66,8888,0.000399,0.001021,0.000257,200,200,0,1418,GET,http://www.example.com/articles/644,HTTP/1.1,"""Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:15.0) Gecko/20100101 Firefox/15.0.1""",-,-,30
2015-01-30T10:47:46.065791Z,elb_demo_009,246.133.78.151,3769,172.54.166.178,8888,0.000897,0.001854,0.000122,200,200,0,2531,GET,https://www.example.com/jobs/239,HTTP/1.1,"""Mozilla/5.0 (X11; CrOS x86_64 8172.45.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.64 Safari/537.36""",DHE-RSA-AES128-SHA,TLSv1.2,30
2015-01-30T10:47:46.087323Z,elb_demo_003,252.78.143.235,21429,172.41.34.234,80,0.001508,0.000646,0.001106,200,200,0,3718,GET,http://www.example.com/jobs/926,HTTP/1.1,"""Mozilla/5.0 (X11; CrOS x86_64 8172.45.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.64 Safari/537.36""",-,-,30

補完されるデータベースを変更したい場合は、athenacliの後にデータベース名を書きます。例えば、

$ athenacli sampledb -e 'select * from elb_logs limit 3'

と書きます。もちろん、テーブル名の前にデータベース名を書いても結果は同じになります。

$ athenacli -e 'select * from sampledb.elb_logs limit 3'

REPL

対話的にクエリを入力・実行できます。やはりathenacliの特長は高度なシンタックスハイライトやスマートコンプリートです。

次の特長があります。

  • データベース内のテーブルやカラムだけでなく、SQLキーワードの入力時にも自動的に完了します。
  • シンタックスハイライト
  • スマート・コンプリートは状況依存の完了を示唆します。
    • SELECT * FROM テーブル名のみが表示されます。
    • SELECT * FROM users WHERE 列名のみが表示されます。
  • 表形式のデータとさまざまな表形式を簡単に表示します。
  • 例えばお気に入りのクエリのような、特別なコマンドがあります。
  • エイリアスのサポート。カラムの補完は、テーブル名にエイリアスがあっても機能します。

具体的には、Featuresをご覧ください。

最後に

インストールや設定はとても簡単でしたので、Athenaユーザーは導入することをおすすめします。Very cool らしい AthenaCLI を試してみたら、やっぱりクールでした。