LoginSignup
3
4

More than 5 years have passed since last update.

DockerでelasticsearchにWikipediaのデータを取り込むまで

Posted at

elasticsearch(5.6.9 ちょっと古い。。)でWikipediaのデータをlogstashから取り込みkibanaで見れるまでの環境作り

データの用意

Wikipediaのデータはここからダウンロードできるのできます。内容確認して問題なければウィキペディア日本語版のダンプからjawiki-yyyyMMdd-pages-articles-multistream.xml.bz2 の名前ものを落とします。

環境構築

ディレクトリ構成はこんな感じでやります。

.
├── docker-compose.yml
├── elasticsearch
│   └── Dockerfile
├── kibana
│   └── Dockerfile
└── logstash
    ├── Dockerfile
    ├── input
    │   └── test.xml
    └── pipeline
        └── wikipedia.conf

elasticsearchの設定

./elasticsearch/Dockerfile
FROM docker.elastic.co/elasticsearch/elasticsearch:5.6.9

RUN elasticsearch-plugin remove x-pack

RUN elasticsearch-plugin install analysis-kuromoji

RUN elasticsearch-plugin install analysis-icu

日本語検索のために、analysis-kuromojiとanalysis-icuをインストールします。x-packはライセンスがないのでアンインストール。

kibanaの設定

./kibana/Dockerfile
FROM docker.elastic.co/kibana/kibana:5.6.9

RUN kibana-plugin remove x-pack

特に設定はなくx-packのアンインストールのみ。

logstashの設定

はじめに、ダウンロードしたWikipediaのダンプデータを今回はtext.xmlとリネームして./logstash/inputに格納します。

Dockerfileの記述

./logstash/Dockerfile
FROM docker.elastic.co/logstash/logstash:5.6.9

RUN mkdir -p /usr/share/logstash/input

RUN logstash-plugin remove x-pack

RUN sed -i '/xpack/d' /usr/share/logstash/config/logstash.yml

RUN logstash-plugin install logstash-input-file

RUN logstash-plugin install logstash-filter-xml

RUN logstash-plugin install logstash-filter-mutate

RUN logstash-plugin install logstash-filter-grok

RUN logstash-plugin install logstash-filter-date

RUN logstash-plugin install logstash-output-elasticsearch

RUN logstash-plugin install logstash-codec-multiline

xmlからデータを取り込むのでいろいろプラグインを入れていきます。
あとハマりポイントとしてRUN sed -i '/xpack/d' /usr/share/logstash/config/logstash.ymlはこのバージョンでは実行は必須のようです。
RUN logstash-plugin remove x-packでx-packをアンインストールしてもlogstash.ymlにx-packの設定が残っているらしくこれを実行しないとエラーになります。

pipelineの設定

./logstash/pipeline/wikipedia.conf
input {
  file {
    path => "/usr/share/logstash/input/test.xml"
    start_position => beginning
    codec => multiline {
        pattern => "<page"
        negate => true
        what => "previous"
        auto_flush_interval => 1
    }
  }
}
filter {
    xml {
        source => "message"
        target => "doc"
        id => "id"
        store_xml => false
        periodic_flush => true
        xpath => [ "(page/title/text())[1]", "title" ]
        xpath => [ "(page/id/text())[1]", "id" ]
        xpath => [ "page/revision/text", "text" ]
    }
    mutate {
        remove_field => ["doc", "path", "host", "message", "tags"]
        join => ["id", ""]
        join => ["title", ""]
        gsub => [
            "text", "https?[^\s]+|<text xml:space=\"preserve\">|</text>", " ",
            "text", "==See also==(.|\n)+|==References==(.|\n)+|==Further reading==(.|\n)+", " ",
            "text", "(\&lt;.+?\&gt;)", " ",
            "text", "(\/ref|\{\{[c|C]ite.+?\}\})", " ",
            "text", "[\[\[|\]\]|==|=|\(|\)|\{\{|\}\}|]|\#+|'+|\&amp;|\&lt;|\&gt;|&nbsp;", " ",
            "text", "\.", " . ",
            "text", "\,", " , ",
            "text", "\:", " : ",
            "text", "\;", " ; ",
            "text", "\/", " \/ ",
            "text", '"', ' " ',
            "text", " +", " ",
            "text", "\. (\. )+", ". ",
            "text", '\n *(\n| )*', ' <br> '
        ]
    }
}
output {
   elasticsearch {
     hosts => "elasticsearch"
     index => "wiki-"
     document_id => "%{id}"
  }
}

indexは、wiki-です。kibanaからはwiki-*でインデックスを登録すれば画面からも見れます。

docker-compose.ymlの設定

docker-compose.yml
version: "3.3"

services:
  elasticsearch:
    build: elasticsearch
    environment:
      - discovery.type=single-node
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    ports:
      - "9200:9200"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - ./elasticsearch/data:/usr/share/elasticsearch/data
    networks: [elastic]

  kibana:
    build: kibana
    ports:
      - "5601:5601"
    networks: [elastic]
    links:
      - elasticsearch:elasticsearch

  logstash:
    build: logstash
    volumes:
      - ./logstash/pipeline/:/usr/share/logstash/pipeline/
      - ./logstash/input/:/usr/share/logstash/input/
    networks: [elastic]
    links:
      - elasticsearch:elasticsearch
    depends_on:
      - elasticsearch

networks:
  elastic:

あとは、ビルドして実行すると時間は多少かかりますが、wikipediaのデータをlogstashからelasticsearchに入ります。

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