LoginSignup
4
1

More than 5 years have passed since last update.

Elasticsearch + Logstash + Kibana + SudachiプラグインでTwitterを可視化したときのtemplate

Last updated at Posted at 2018-12-06

使用した環境

上の1つ目の手順でセットアップした環境に、2つ目の「手順2:Logstashの設定ファイルを作成->起動する」部分の手順で、Logstashの設定と起動を実施。

Elasticsearchのテンプレート

Tweet情報から、以下のFieldに対し指定したtypeで扱うよう指定。

  • field名:coordinates を geo_point に
  • field名:created_at を date に (日付フォーマット指定も)

参考:Index twitter data with coordinates geo_point parse exception

環境セットアップ時の情報に記載の、dynamic templateでsudachi_analyzerが利用できるようにする設定も合わせて行う。

テンプレートの設定ファイル

mytweet.json
{
  "template": "mytweet*",
  "settings":{
    "index": {
      "analysis": {
        "tokenizer": {
          "sudachi_tokenizer": {
            "type": "sudachi_tokenizer",
            "mode": "search",
            "discard_punctuation": true
          }
        },
        "analyzer": {
          "sudachi_analyzer": {
            "filter": [
            ],
            "tokenizer": "sudachi_tokenizer",
            "type": "custom"
          }
        }
      }
    }
  },
  "mappings": {
    "_default_": {
      "dynamic_templates":[
        {
          "texts": {
            "match_mapping_type": "string",
            "match": "text",
            "mapping": {
              "type": "text",
              "analyzer": "sudachi_analyzer",
              "fielddata": true
            }
          }
        }
      ],
       "properties": {
        "coordinates":{
          "properties": {
            "coordinates": {
              "type": "geo_point"
            },
            "type":{
              "type": "text",
              "fields": {
                "keyword":{
                  "type":"keyword",
                  "ignore_above": 256
                }
              }
            }
          }
        },
        "created_at": {
          "type": "date",
          "format": "EEE MMM dd HH:mm:ss Z YYYY"
        }
      }
    }
  }
}

Indexにテンプレートを適用する

上のmytweet.jsonを使って以下を実行。

$ curl -X PUT -H "Content-Type: application/json" http://localhost:9200/_template/mytweet -d "@mytweet.json"

追記(備忘録)

冒頭の Elasticsearch+Logstash+Kibanaで作るTwitter分析ダッシュボード 作成手順まとめ
「手順2:Logstashの設定ファイルを作成->起動する」部分の手順で、Logstashの設定と起動を実施。

のところの作業ログ。

Logstashの設定ファイルを準備

/usr/share/logstash/kibana_twitter.conf を準備

kibana_twitter.conf
input {
  twitter {
    # TwittrAPIの認証情報を入力
    consumer_key => "xxx"
    consumer_secret => "yyy"
    oauth_token => "zzz"
    oauth_token_secret => "aaa"
    # つぶやきの検索対象に含めるキーワードを配列で指定する
    keywords => ["#紅葉"]
    full_tweet => true
  }
}
output {
  elasticsearch {
    # ElasticSearchのホストパス
    hosts => ["http://public_ip_address:9200/"]
    # インデックス名
    index => "koyotweet"
  }
}

起動とログ監視

# /usr/share/logstash/bin/logstash --path.settings=/etc/logstash --path.data /tmp -f /usr/share/logstash/kibana_twitter.conf 
$ tail -f /var/log/logstash/logstash-plain.log 

停止は kill -9

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