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

在我们之前的文章 “Beats: 使用 Filebeat 进行日志结构化”,我使用了一种方法来解析一个 JSON 格式的文件,并导入到 Elasticsearch 中。在今天的文章中,我来用另外的一种方式来展示如何导入一个 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 中进行查看:

GET logs_json/_search
{
  "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"
        }
      }
    ]
  }
}

我们可以从上面查看到被导入的两个文档。

你可能感兴趣的:(Beats,elasticsearch,大数据)