如何使用特定的_type从elasticsearch记录日志

时间:2020-04-21 12:27:36

标签: elasticsearch logstash

我有一个logstash(6.8.1)配置文件,如下所示:

input {
  elasticsearch {
    hosts => [ "127.0.0.1:9200" ]
    index => "myindex"
    codec => "json"
    docinfo => true
  }
}

filter {
  mutate {
    remove_field => ["@timestamp", "@version"]
  }
}

output {
  elasticsearch {
    hosts => [ "127.0.0.1:9300" ]
    index => "%{[@metadata][_index]}"
    document_type => "%{[@metadata][_type]}"
    document_id => "%{[@metadata][_id]}"
  }
}

但是,端口9200中的es是5.x,端口9300中的es是6.x。 此外,端口9200中的es具有多个“类型”。

由于es 6.x只有一个“类型”,所以我需要这样的东西:

input {
  elasticsearch {
    hosts => [ "127.0.0.1:9200" ]
    user => ""
    password => ""
    index => "myindex"
    document_type => "mytype"  <--- like this
    codec => "json"
    docinfo => true
  }
}

我该如何实现? 非常感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

我为您提供了一个詹金斯Logstash配置示例。

您的意思是这个意思

input {
  file {
     path => "/var/log/jenkins/*"
     type => "jenkins-server"
     start_position => "beginning"
 } 
}
  filter 
 {
      if [type] == "jenkins-server" {
          mutate {
              add_field => ["@message_type", "jenkins"]
              add_field => ["@message", "%{message}"]
          }
      }
   }

答案 1 :(得分:0)

最后,我使用elasticsearch-dump实现此操作:

docker run --net=host --rm -it docker.io/taskrabbit/elasticsearch-dump \
    --input=http://user:password@input-es:9200 \
    --input-index=my-user-index/my-user-type \
    --output=http://user:password@output-es:9200 \
    --output-index=my-user-index \
    --type=mapping

PS:要迁移数据,请使用选项--type=data