Common Log Formatのdate型ログをEmbulkでElasticsearchにdate型で入れたメモ

※こちらの記事は2017年にQiitaに投稿した記事を再編集したものです

なんか苦労したのでメモ。

読み込むログ

{"time_local":"05/Feb/2017:03:38:34 +0900","hoge":"piyo"}

NginxやApacheでよく見かけるフォーマット。

Embulkの設定

Elasticsearch的にUTCで入れたほうが扱いやすいのでtimestamp型で取り込んでUTCに戻してあげる。

in:
  type: file
  path_prefix: /path/to/json/
  parser:
    type: jsonl
    charset: UTF-8
    newline: LF
    columns:
      - { name: "time_local", type: "timestamp", format: "%d/%b/%Y:%T %z" }
      - { name: "hoge", type: "string" }
out:
  type: elasticsearch_ruby
  cluster_name: elasticsearch
  mode: normal
  nodes:
    - {host: "es_host", port: 9200}
  index: jsonlog
  index_type: log

このEmbulkの定義で読み込むとtime_localの値は以下の感じで出力される。

2017-02-04 18:38:34 UTC

Elasticsearchのインデックステンプレート

EmbulkでUTCに戻すのはいいが、このまま突っ込むだけだとElasticsearchはdate型としては受け入れてくれない。
突っ込む前にフォーマットを定義してマッピングしておかないと、文字列としてドキュメントが登録されてしまう。

{
  "template": "jsonlog",
  "mappings": {
    "log": {
      "properties": {
        "time_local": {
          "type": "date",
          "format": "YYYY-MM-dd HH:mm:ss' UTC'"
        },
        "hoge": {
          "type": "string",
          "index": "not_analyzed"
        }
      }
    }
  }
}
$ curl -XPUT es_host:9200/_template/jsonlog?pretty -d "$(cat template.json)"

あとは上の設定でEmbulkを使ってbulkインポートしてあげれば時系列データとして意味のあるログになる。

コメント

タイトルとURLをコピーしました