LoginSignup
3

More than 5 years have passed since last update.

Embulkを使ってS3にあるJSONをMySQLに保存

Last updated at Posted at 2018-03-12

S3にあるJsonファイルをMySQLにレコードとして保存

状況

Kinesis FirehoseでS3にログを送った後、どうにかして特定のログをMysqlに保存したかった。
なのでEmbulkで定期的にMysqlへ書き込んでやることに。

ちなみにFirehoseのTransform時にLambdaからMysqlにデータを保存しようとしたが、Lambdaがconnectionを使い切るみたいな記事を読んだので辞めた。MySQL + lambdaのgoogle 検索結果

使ったプラグイン

  • embulk-output-mysql
  • embulk-input-s3
  • embulk-parser-jsonl

configファイル

config.yml
in:
  type: s3
  bucket: xxxxx
  path_prefix: dev/2018/03/12/24/
  endpoint: s3-ap-northeast-1.amazonaws.com
  auth_method: instance
  parser:
    type: jsonl
    schema:
      - {name: first_name, type: string}
      - {name: last_name, type: string}
      - {name: sex, type: string}
      - {name: datetime, type: string}

out:
  type: mysql
  host: xxxxxxxxx.ap-northeast-1.rds.amazonaws.com
  user: xxxxx
  password: xxxxxxx
  database: xxxxx
  table: xxxxx
  mode: replace

ハマリポイント

改行コード

Kinesisで普通にログを出力するとJSON同士の切れ目がなくなるのでログ出力時に改行コードを入れるとパースしやすくなる

BAD:

{"a":1}{"b":1}

GOOD

{"a":1}
{"b":1}

embulkを実行しているEC2からS3へアクセスする時

もちろんIAMでS3にアクセス出来るようにしておく必要があるのと
config.ymlauth_method: instance を明記する必要がある

感想

どうしようかなと思って調べてから、約1時間でやりたいことが出来たのでembulkスゴい。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3