oranie's blog

旧:iをgに変えると・・・なんだっけ・・・

embulk-input-dynamodbを使ってみました

※このブログは所属している組織の意見などは全く含まれてない個人の意見です。技術的な問題などがあっても悪いのぼくです。

DynamoDB + Hiveでゴニョゴニョ試したりしていたのですが、Hiveを全くチューニングせず試していた為結構遅いので

※nginxのアクセスログがDynamoDBに入っていると思ってください。
SELECT COUNT(id) from hoge WHERE status = 504

みたいなちょっとテストデータを確認するようなクエリを投げてもそこそこ時間が掛かってしまい何か楽できないかなーとTwitterでつぶやいていると

と @frsyuki先生からリプライを頂き今まで情報は見ていたけど触っていなかったのでembulkを試してみました。

で、表題のembulk-input-dynamodbを使ってみたんですがまだ開発中だったらしくscanが最大1MBの1回で終わってしまう問題があったんですがこれもTwitterでつぶやいたら開発者の@Lulichnさんが拾ってくれてすぐに修正してくれました。ありがたやありがたや。で、追加で僕もちょっとだけpull req投げさせて貰いこれで

・1回のscanの読み取り回数(最大1MBなのはDynamoDBのscan仕様な為変わらず。1000000000とか書いても1MBの仕様に当たるとそれ以上読まずに次のscanが必要です。)

・embulkで読み取る最大レコード数(100とか書けば最大100レコード出力で終わり。)

・IAM role対応

が出来るようになり、filter部分に条件式を書いて標準出力させればちょっとした確認がすぐに出来るようになりました。

以下簡単な設定の流れ

DynamoDBにテストデータを入れる。

とりあえずここは面倒くさいのでnginxのログをfluent-plugin-dynamodbで入れました。一点補足として動作テストしたEC2インスタンスにはIAM roleが設定されているのでACCESS KEYなどは設定していません。

nginxのログフォーマットは

    log_format ltsv 'time:$time_iso8601\t'
                'remote_addr:$remote_addr\t'
                'request_method:$request_method\t'
                'request_length:$request_length\t'
                'request_uri:$request_uri\t'
                'uri:$uri\t'
                'query_string:$query_string\t'
                'status:$status\t'
                'bytes_sent:$bytes_sent\t'
                'body_bytes_sent:$body_bytes_sent\t'
                'referer:$http_referer\t'
                'useragent:$http_user_agent\t'
                'forwardedfor:$http_x_forwarded_for\t'
                'request_time:$request_time\t'
                'upstream_response_time:$upstream_response_time';

です。fluentdは

<source>
  type tail
  format ltsv 
  tag nginx.access
  path /var/log/nginx/access.log
  pos_file /var/log/td-agent/buffer/access.log.pos
</source>

<match *.**>
  type copy
  <store>
    type dynamodb
    dynamo_db_endpoint dynamodb.ap-northeast-1.amazonaws.com
    dynamo_db_table access_log
  </store>
</match>

こんなレベルです。

embulkインストール

github.com

を読んで環境に合わせてインストールします。

プラグインインストール

embulk gem install embulk-input-dynamodb

で完了。

config設定

ここも稼働させるEC2インスタンスにはIAM roleが設定されているのでcredentials情報などは記載しません。

in:
  type: dynamodb
  table: access_log_range
  region: ap-northeast-1
  scan_limit: 10000
  record_limit: 10
  columns:
    - {name: time, type: string}
    - {name: request_uri, type: string}
    - {name: status, type: string}
out:
  type: stdout

embulk実行

ここまでやったら

[oranie@ip-172-31-6-145 embulk]$ embulk run ./dynamodb.yml
2015-07-01 04:36:50.963 +0000: Embulk v0.6.15
2015-07-01 04:36:52.474 +0000 [INFO] (transaction): Loaded plugin embulk-input-dynamodb (0.0.2)
2015-07-01 04:36:52.523 +0000 [INFO] (transaction): {done:  0 / 1, running: 0}
2015-06-27T07:43:34Z,/,200
2015-06-16T08:38:47Z,/notfound,404
2015-06-18T22:46:35Z,/,200
2015-06-17T08:05:34Z,/,200
2015-06-16T08:38:46Z,/notfound,404
2015-06-19T00:32:04Z,/,200
2015-06-26T16:44:35Z,/,200
2015-06-16T08:38:49Z,/notfound,404
2015-06-27T03:49:35Z,/,200
2015-06-18T21:18:04Z,/,200
2015-07-01 04:36:53.598 +0000 [INFO] (transaction): {done:  1 / 1, running: 0}
2015-07-01 04:36:53.610 +0000 [INFO] (main): Committed.
2015-07-01 04:36:53.610 +0000 [INFO] (main): Next config diff: {"in":{},"out":{}}
[oranie@ip-172-31-6-145 embulk]$

こんな感じでデータが出力されます。

おしまい