Beats:使用 Filebeat 導入 JSON 格式的日志文件


轉載自:https://blog.csdn.net/UbuntuTouch/article/details/108504014

在今天的文章中,我來用另外的一種方式來展示如何導入一個 JSON 格式的文件。

准備數據

我們還是以之前的那篇文章中的數據為例,我們使用如下的文件:

sample.json

{"user_name": "arthur", "id": 42, "verified": false, "event": "logged_in"}
{"user_name": "arthur", "id": 42, "verified": true, "event": "changed_state"}

這里面就只有兩條數據。

創建 Filebeat 的配置文件

為了能夠解析上面的 JSON 文件,我們使用如下的配置文件:

filebeat.yml

filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /Users/liuxg/data/processors/sample.json
 
processors:
 - decode_json_fields:
     fields: ['message']
     target: ''
     overwrite_keys: true
 
 - drop_fields:
     fields: ["message", "ecs", "agent", "log"]
 
setup.template.enabled: false
setup.ilm.enabled: false
 
output.elasticsearch:
  hosts: ["localhost:9200"]
  index: "logs_json"
  bulk_max_size: 1000

在上面,我們使用了 processor decode_json_fields 來解析 JSON 日志。上面文件的 JSON 文件 sample.json 的位置依賴於你的文件的位置需要進行修改。這個和之前文章中介紹的方法不一樣。你可以比較一下。同時,我們使用了 drop_fields 來刪除一些並不需要的字段。

導入數據

我們可以使用如下的命令來把數據導入到 Elasticsearch 中:

./filebeat -e -c  filebeat.yml 

經過上面的命令的運行,我們可以在 Kibana 中進行查看:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "logs_json",
        "_type" : "_doc",
        "_id" : "hzdVc3QBk0AMDyd4y0Cq",
        "_score" : 1.0,
        "_source" : {
          "@timestamp" : "2020-09-09T14:47:15.338Z",
          "host" : {
            "name" : "liuxg"
          },
          "user_name" : "arthur",
          "id" : 42,
          "input" : {
            "type" : "log"
          },
          "verified" : false,
          "event" : "logged_in"
        }
      },
      {
        "_index" : "logs_json",
        "_type" : "_doc",
        "_id" : "iDdVc3QBk0AMDyd4y0Cq",
        "_score" : 1.0,
        "_source" : {
          "@timestamp" : "2020-09-09T14:47:15.338Z",
          "id" : 42,
          "verified" : true,
          "host" : {
            "name" : "liuxg"
          },
          "input" : {
            "type" : "log"
          },
          "user_name" : "arthur",
          "event" : "changed_state"
        }
      }
    ]
  }
}

我們可以從上面查看到被導入的兩個文檔。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM