Elasticsearchにはどうやってデータ入れるのが良いのか、調べていたらEmbulkというものがあるらしいです。流行っているらしく、これを利用してデータを投入します。
Embulkについてまとめ。
Fluentdのバッチ版Embulk(エンバルク)のまとめ - Qiita
Elasticsearchにcsvファイルをインポートするためのレシピが公式に公開されていますので、それに沿って作業を実施。
Scheduled bulk data loading to Elasticsearch + Kibana 4 from CSV files — Embulk 0.8 documentation
まず、モジュールをダウンロードしてきます。
# wget http://dl.embulk.org/embulk-latest.jar -O /usr/local/bin/embulk # chmod +x /usr/local/bin/embulk # embulk --version embulk 0.8.13
Embulkにはたくさんpluginが作成されているようで、Elasticsearchへアウトプットするためのpluginを追加してあげます。pluginはかなり充実してます。
# embulk gem install embulk-output-elasticsearch
で、Elasticsearchへぶち込むための、Embulk設定ファイルを作成する必要があります。しかしながら、以下のような必要最低限の情報を与えてあげれば、Embulk側で勝手に設定ファイルを作成してくれるみたいです。
[seed.yml]
in:
type: file
path_prefix: ./output/
out:
type: elasticsearch
index: shikiho
index_type: shikiho
nodes:
- host: localhost
以下のguessコマンドを実行することで、実際の設定ファイルであるconfig.ymlを作成してくれます。
# embulk guess ./seed.yml -o config.yml
以下の通り、config.ymlが作成されます。
[config.yml]
in:
type: file
path_prefix: ./output/output
parser:
charset: UTF-8
newline: CRLF
type: csv
delimiter: ','
quote: '"'
escape: '"'
trim_if_not_quoted: false
skip_header_lines: 1
allow_extra_columns: false
allow_optional_columns: false
columns:
- {name: code, type: long}
- {name: creadtedDate, type: string}
- {name: meigaraName, type: string}
- {name: url, type: string}
- {name: accountingPeriod, type: string}
- {name: establishmentDate, type: string}
- {name: listingDate, type: string}
- {name: feature, type: string}
- {name: business, type: string}
- {name: assets, type: string}
- {name: finance, type: string}
- {name: nullColumn1, type: string}
- {name: comment1, type: string}
- {name: comment2, type: string}
- {name: industryType, type: string}
- {name: vendor, type: string}
- {name: customer, type: string}
- {name: rival, type: string}
- {name: headOffice, type: string}
- {name: branchOffice, type: string}
- {name: workerNumber, type: string}
- {name: security, type: string}
- {name: bank, type: string}
- {name: affiliate, type: string}
- {name: nullColumn2, type: string}
- {name: progress, type: string}
- {name: holderNumber, type: string}
out:
type: elasticsearch
index: shikiho
index_type: shikiho
nodes:
- {host: localhost}
作成したconfig.ymlを指定して、runを実行してあげればファイルを読み込んでくれます。
# embulk run config.yml
無事検索できるようになりました。
# curl -XGET 'http://localhost:9200/shikiho/shikiho/_search?q=code:1301'
{
"took": 6,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 8.500806,
"hits": [
{
"_index": "shikiho",
"_type": "shikiho",
"_id": "AVfWtm-63dgHhWAY7GX3",
"_score": 8.500806,
"_source": {
"code": 1301,
"creadtedDate": "作成日:2016年09月16日",
"meigaraName": "1301 (株)極洋 きょくよう [水産・農林業]",
"url": "【URL】 http://www.kyokuyo.co.jp/",
"accountingPeriod": "【決算】3月",
"establishmentDate": "【設立】1937.9",
"listingDate": "【上場】1949.5",
"feature": "【特色】水産品の貿易、加工、買い付け主力。すしネタに強み。加工食品は業務用が軸。海外加工比率高い",
"business": "【連結事業】水産商事50(1)、冷凍食品30(0)、常温食品8(2)、物流サービス1(5)、鰹・鮪11(1)、他0(8) <16・3>",
"assets": "",
"finance": "",
"nullColumn1": "",
"comment1": "【大幅増益】冷凍・常温食品は拡大、塩釜新工場本格稼働が奇与。水産商事はサケ・マスなど魚価が安 定、加工品が拡大、米国市場も堅調。冷蔵運搬船から撤退し物流効率化。新工場償却負担増でも営業増益。",
"comment2": "【養殖】完全養殖のクロマグロは18年初に初出荷へ、天然種苗の確保にも全力。業務用冷食では、すし向けなど商品群の拡充を図る。9月末基準日で10株を1株に併合。",
"industryType": "【業種】 食品 時価総額順位 56/172社",
"vendor": "【仕入先】KAMEC",
"customer": "【販売先】三菱食品",
"rival": "【比較会社】1333 マルハニチ,2875 東洋 水産,1332 日本水産",
"headOffice": "【本社】107-0052東京都港区赤坂3-3-5 TEL03-5545-0701",
"branchOffice": "【支社】大阪TEL06-6315-1251,東京,福岡,他",
"workerNumber": "【従業員】<16.3>連2,249名 単599名(39.8歳)[年]661万 円",
"security": "【証券】[上]東京[幹]日興,野村,大和[名]三菱U信[監]井上",
"bank": "【銀行】りそな,農中,三井住友信,三菱U信,三菱U",
"affiliate": "【連結】極洋水産,極洋商事,極洋食品",
"nullColumn2": "",
"progress": "【四半期進捗率】3期平均19.6% 今期13.3%(-6.3%)",
"holderNumber": "33702"
}
}
]
}
}