一定間隔でjsonデータを作って送り続けるCLIツールを作った

golangproduct

Elasticsearchにリアルタイムなテストデータを投入するために、一定間隔でjsonを作って送り続けるCLIツールを作った。Go製。 urfave/cliを使った。

sambaiz/sendjson

こんなindexにデータを入れてみる。

$ curl -XPUT 'http://localhost:9200/hoge' -d'
{
  "mappings": {
    "test_type": { 
      "_all":       { "enabled": false  }, 
      "properties": { 
        "os_name": { "type": "keyword" },
        "score": { "type": "byte" },
        "@timestamp": { "type": "date", "format": "epoch_second" }
      }
    }
  }
}
'

こんな感じでキーに対してtypeと入る値を定義するとそれっぽいデータができて送られていく。

$ go install github.com/sambaiz/sendjson
$ sendjson --interval 0.5s --duration 10s --url http://localhost:9200/hoge/test_type '
{
    "os_name": {"type": "string", "or": ["windows", "mac", "linux", "ios", "android"]},
    "score":  {"type": "integer", "min": 0, "max": 100},
    "@timestamp": {"type": "time", "time_format": "unix_epoch"}
}'

{"@timestamp":1488635130,"os_name":"linux","score":82}
{"@timestamp":1488635130,"os_name":"windows","score":9}
{"@timestamp":1488635131,"os_name":"windows","score":73}
{"@timestamp":1488635131,"os_name":"ios","score":50}
{"@timestamp":1488635132,"os_name":"windows","score":69}
...

ちゃんと入っていることを確認。

$ curl http://localhost:9200/hoge/_search | jq
{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 29,
    "max_score": 1,
    "hits": [
      {
        "_index": "hoge",
        "_type": "test_type",
        "_id": "AVqZpCjjFTc9Q_rmmMn7",
        "_score": 1,
        "_source": {
          "@timestamp": 1488636356,
          "os_name": "android",
          "score": 38
        }
      },
      {
        "_index": "hoge",
        "_type": "test_type",
        "_id": "AVqZpE-kFTc9Q_rmmMoN",
        "_score": 1,
        "_source": {
          "@timestamp": 1488636366,
          "os_name": "android",
          "score": 87
        }
      },
      ...
  }
}