【问题标题】:Logstash output is incorrectLogstash 输出不正确
【发布时间】:2016-05-02 10:40:44
【问题描述】:

我是 logstash 和 elasticsearch 的新手。我正在使用 logstash 读取数据库更新并存储到 elasticsearch 中以进行快速搜索。以下是我的 logstash 配置文件(countries.conf)。

    input {
      jdbc {
        jdbc_driver_library => "/home/vagrant/postgresql-9.4-1201.jdbc4.jar"
        jdbc_driver_class => "org.postgresql.Driver"
        jdbc_connection_string => "jdbc:postgresql://192.168.10.123:5432/myDB"
        jdbc_user => "myuser"
        jdbc_password => "mypassword"
        schedule => "* * * * *"
        statement_filepath => "/home/vagrant/countries.sql"
        last_run_metadata_path => "/home/vagrant/logstash/countries.log"
      }
    }
    output {
        elasticsearch {
            index => "myIndex"
            document_type => "countries"
            document_id => "%{id}"
            hosts => "localhost:9200"
        }
        stdout { codec => json_lines }
    }

而我的 countries.sql 文件如下

  SELECT json.id as id,
    row_to_json(json.*) AS _source
   FROM (
        SELECT id, created, modified, name, capital, iso_alpha2, iso_alpha3 
        FROM countries
  ) as json

我使用以下命令运行配置文件

sudo /opt/logstash/bin/logstash -f /home/vagrant/countries.conf

上述命令在标准输出上的输出如下:-

Settings: Default pipeline workers: 1
Pipeline main started
{"_id":6,"_source":{"type":"json","value":"{\"id\":6,\"created\":\"2013-02-07T10:11:00\",\"modified\":\"2016-04-29T11:15:40.329\",\"name\":\"Andorra\",\"capital\":\"Andorra la Vella\",\"iso_alpha2\":\"AD\",\"iso_alpha3\":\"AND\"}"},"@version":"1","@timestamp":"2016-05-02T10:08:00.931Z"}

正如您在上面的输出中看到的,我在 _source 字段中的 json 字符串已更改。理想情况下应该如下所示

{"_id":6,"_source":{\"id\":6,\"created\":\"2013-02-07T10:11:00\",\"modified\":\"2016-04-29T11:15:40.329\",\"name\":\"Andorra\",\"capital\":\"Andorra la Vella\",\"iso_alpha2\":\"AD\",\"iso_alpha3\":\"AND\"},"@version":"1","@timestamp":"2016-05-02T10:08:00.931Z"}

Logstash 正在更改我的 json 字符串。它正在添加 type:"json" 一个额外的字段,并在 value 字段中添加我的实际 json 字符串。我检查了数据库。我的 SQL 查询以我需要的格式正确返回 json 字符串。

有人可以让我确切地知道我缺少什么吗?或者可以引导我正确的方向吗?

提前致谢!

【问题讨论】:

    标签: json logstash


    【解决方案1】:

    查看elasticsearch中的日志。我认为主要是因为您在标准输出中使用codec => json_lines,这就是type=>json 被附加的原因。 Elasticsearch 记录不会有 type 字段。

    如果elasticsearch中的日志也有type=>json,使用mutate去掉该字段。

    filter {
      mutate {
        remove_field => [ "type" ]
      }
    }
    

    【讨论】:

    • 感谢您的回复。我将索引数据检查到弹性搜索中。它也有 type:json 字段。我也使用了 mutate 但它没有被删除。此外,我还在标准输出中删除了 codec => json_lines。但我仍然面临类似的问题。
    • 我也测试过。但是jdbc默认没有添加“type”字段,elasticseach也没有在_source中添加“type”字段。
    • 检查你是否使用任何过滤器。
    猜你喜欢
    • 1970-01-01
    • 2020-09-15
    • 2021-07-02
    • 2021-03-05
    • 2015-01-08
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 2015-03-10
    相关资源
    最近更新 更多