f.c.v.systemのブログ

f.c.v.systemのブログ

IT関連の記事を書いていきます

Amebaでブログを始めよう!

elasticsearchで Bulk APIを使用して、データを登録する方法。


○前までの関連記事
CentOSへのelasticsearchのインストール方法

mobz/elasticsearch-headのインストール方法

elasticsearchのインデックス/データの登録/削除手順


○環境

・Oracle Virtual Box
・Cent OS6.5


○手順

以下のURLを参考

Bulk API


1. httpリクエストにindexとtypeの指定なし


# ひな型
$ curl -X POST http://<IPアドレス>:<port番号>/_bulk --data-binary @jsonリクエスト

# 例
$ cat hoge1.json
{ "index" : { "_index" : "hoge_index", "_type" : "hoge_type", "_id" : "1" } }
{ "field_01": "hoge1", "field_02": "hoge2", "field_03": "hoge3" }

$  curl -X POST http://localhost:9200/_bulk --data-binary @hoge1.json

2. httpリクエストにindexの指定あり


# ひな型
$ curl -X POST http://<IPアドレス>:<port番号>/<インデックス名>/_bulk --data-binary @jsonリクエスト

# 例
$ cat hoge2.json
{ "index" : {"_type" : "hoge_type", "_id" : "2" } } { "field_01": "hoge1", "field_02": "hoge2", "field_03": "hoge3" }
$ curl -X POST http://localhost:9200/hoge_index/_bulk --data-binary @hoge2.json

3. httpリクエストにindexとtypeの指定あり


# ひな型
$ curl -X POST http://<IPアドレス>:<port番号>/<インデックス名>/<タイプ名>/_bulk --data-binary @jsonリクエスト

$ cat hoge3.json
{ "index" : {"_id" : "3" } } { "field_01": "hoge1", "field_02": "hoge2", "field_03": "hoge3" }
$ curl -X POST http://localhost:9200/hoge_index/hoge_type/_bulk --data-binary @hoge3.json

* 補足


①IDを指定しないと適当なIDが入る


$ cat hoge4.json
{ "index" : {} }
{ "field_01": "hoge1", "field_02": "hoge2", "field_03": "hoge3" }

$ curl -X POST http://localhost:9200/hoge_index/hoge_type/_bulk --data-binary @hoge4.json

{"took":2,"errors":false,"items":[{"create":{"_index":"hoge_index","_type":"hoge_type","_id":"r1d7lznBQE6Nl3gapLce7Q","_version":1,"status":201}}]}
②大量にデータを投入する場合は、> /dev/nullなどを実施した方がよいかと