14
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

GoodpatchAdvent Calendar 2016

Day 6

既存ログからBigQueryのスキーマをGuessする

Last updated at Posted at 2016-12-05

はじめに

既存のログからBigQueryのスキーマを推測してくれるツールを作ったので、それを紹介します。

結構複雑なスキーマのログをひとつのテーブルで扱いたいという要件があったので作りました。

使い方

bq_guessというGemで、READMEに書いてあることがすべてです。

例えば次のようなJSON Linesのログがあったとき、こんな感じでスキーマを出してくれます。

$ cat existing_log.jsonl
{"required":123,"optional":true,"nested":{"required":1234,"optional":"yes"},"array":[0,1,2,3,4]}
{"required":456,"optional":false,"nested":{"required":1234,"optional":"yes","nested":{"prop":1}},"array":[5,6,7,8,9]}
{"required":789,"nested":{"required":1234,"optional":"yes","additional":"added"},"array":[]}

$ bq_guess existing_log.jsonl
[
  {
    "name": "required",
    "type": "INTEGER",
    "mode": "REQUIRED"
  },
  {
    "name": "optional",
    "type": "BOOLEAN",
    "mode": "NULLABLE"
  },
  {
    "name": "nested",
    "type": "RECORD",
    "mode": "REQUIRED",
    "fields": [
      {
        "name": "required",
        "type": "INTEGER",
        "mode": "REQUIRED"
      },
      {
        "name": "optional",
        "type": "STRING",
        "mode": "REQUIRED"
      },
      {
        "name": "nested",
        "type": "RECORD",
        "mode": "NULLABLE",
        "fields": [
          {
            "name": "prop",
            "type": "INTEGER",
            "mode": "REQUIRED"
          }
        ]
      },
      {
        "name": "additional",
        "type": "STRING",
        "mode": "NULLABLE"
      }
    ]
  },
  {
    "name": "array",
    "type": "INTEGER",
    "mode": "REPEATED"
  }
]

インストールはいつもどおりです。

gem install bq_guess

今のところ、JSON Linesに対応してます。

おわりに

手元の用途だとRECORDとNULLABLEだらけの数千行のスキーマを吐けたのでそこそこ動くんではないかと思います。

ただ、実装がかなり雑なのと自分用にチューニングしてあるのとで、まだまだ何かありそうです。
何かあればこちらまで報告していただけると幸いです :sweat_smile:

14
3
0

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
14
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?