【问题标题】:Logstash : Parsing fails while parsing Json data received through web service like twitter / facebook etcLogstash:解析通过 twitter / facebook 等 Web 服务接收的 Json 数据时解析失败
【发布时间】:2016-12-06 22:21:31
【问题描述】:

我正在接收来自 Twitter 的 Web 服务数据并记录到文件中,之后我需要将该数据发送到 Logstash,以便将这些数据编入 Elasticsearch 的索引。

我正在使用下面的配置,这给 jsonparsefailure 异常为

JSON 解析失败。回退到纯文本 {:error=>#> LogStash::Json::ParserError: Unexpected character (':' (code 58)): 期望一个>有效值(数字、字符串、数组、对象、'true'、'false' 或 'null')

我的 logstash conf 文件看起来像:

input
    {
        file
        {
            path => ["/mnt/volume2/ELK_Prashant/at/events.json"]
            codec => json
            type => json
        start_position => "beginning"
            sincedb_path => "/dev/null"
        }
    }
    output
    {
        stdout { codec => rubydebug }
    }

events.json 中的数据可以参考https://dev.twitter.com/rest/reference/get/search/tweets,示例如下: events.json

[
{ "location": "LA, CA",
        "follow_request_sent": null,
        "profile_link_color": "0084B4",
        "is_translator": false,
        "id_str": "137238150",
        "entities": {
          "url": {
            "urls": [
              {
                "expanded_url": null,
                "url": ""
              }
            ]
          }
        }
}
]

【问题讨论】:

  • 在您的 events.json 文件中,每个 JSON 事件是否都在一行中,没有任何换行符?

标签: json elasticsearch logstash logstash-file


【解决方案1】:

从您的示例 events.json 文件中,很明显您正在使用完整的 json 对象作为 logstash file 插件的输入,但该插件默认情况下假定每个事件都是单行的,因此只有它是能够检测新事件的到来并跟踪当前位置。

所以你的输入文件应该是这样的,其中每个事件由换行符分隔

{"location":"LA, CA","follow_request_sent":null,"profile_link_color":"0084B4","is_translator":false,"id_str":"137238150","entities":{"url":{"urls":[{"expanded_url":null,"url":""}]}}}
{"location":"LA, CA","follow_request_sent":null,"profile_link_color":"0084B4","is_translator":false,"id_str":"137238150","entities":{"url":{"urls":[{"expanded_url":null,"url":""}]}}}
{"location":"LA, CA","follow_request_sent":null,"profile_link_color":"0084B4","is_translator":false,"id_str":"137238150","entities":{"url":{"urls":[{"expanded_url":null,"url":""}]}}}

或者您必须在输入插件中使用多行编解码器或过滤器。更多信息可以在here找到。

【讨论】:

    猜你喜欢
    • 2019-08-17
    • 2018-10-19
    • 1970-01-01
    • 1970-01-01
    • 2017-07-18
    • 1970-01-01
    • 1970-01-01
    • 2021-07-03
    • 1970-01-01
    相关资源
    最近更新 更多