【问题标题】:ElasticSearch: populating ip_range type field via logstashElasticSearch:通过 logstash 填充 ip_range 类型字段
【发布时间】:2021-06-28 13:49:47
【问题描述】:

我正在 ElasticSearch 6.8 (https://www.elastic.co/guide/en/elasticsearch/reference/6.8/range.html) 中试验 ip_range 字段类型,并努力寻找一种通过 logstash

将 ip 数据正确加载到字段中的方法

我能够通过 Kibana 开发工具加载一些示例数据,但无法通过 logstash 找到相同的方法。

索引定义

PUT test_ip_range
{
  "mapping": {
    "_doc": {
      "properties": {
        "ip_from_to_range": {
          "type": "ip_range"
        },
        "ip_from": {
          "type": "ip"
        },
        "ip_to": {
          "type": "ip"
        }
      }
    }
  }
}

添加示例文档:

PUT test_ip_range/_doc/3
{
  "ip_from_to_range" : 
  {
    "gte" : "<dotted_ip_from>",
    "lte": "<dotted_ip_to>"
  }
}

Logstash 配置(从数据库读取)

input {
  jdbc {
  ...
  statement => "SELECT ip_from, ip_to, <???> AS ip_from_to_range FROM sample_ip_data"
  }
}
output {
  stdout { codec => json_lines }
  elasticsearch {
  "hosts" => "<host>"
  "index" => "test_ip_range"
  "document_type" => "_doc"
  }
}

问题:

如何通过logstash 配置将ip_fromip_to DB 字段分别放入gtelte 部分??

我知道我也可以以 CIDR 表示法插入 ip 范围,但希望能够同时拥有两种选择 - 以 CIDR 表示法加载和作为范围加载。

【问题讨论】:

    标签: elasticsearch logstash


    【解决方案1】:

    经过反复试验,终于弄明白了logstash的配置。

    我曾发布过类似的问题 here,这最终让我在这个用例的语法上也走上了正轨。

    input { ... }
    filter {
      mutate {
        add_field => {
          "[ip_from_to_range]" => 
          '{
            "gte": "%{ip_from}",
            "lte": "%{ip_to}"
           }'
        }
      }
      json {
        source => "ip_from_to_range"
        target => "ip_from_to_range"
      }
    }
    output { ... }
    

    过滤器部件说明

    1. mutate add_field:创建一个新字段 [ip_from_to_range],其值为 json 字符串 ('{...}')。将字段设为[field_name] 很重要,否则将字符串解析为 json 对象的下一步将不起作用
    2. json:将字符串表示解析成json对象

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-20
      • 2013-03-22
      • 2019-12-05
      • 1970-01-01
      • 2015-11-21
      • 2013-09-04
      • 2021-12-24
      • 1970-01-01
      相关资源
      最近更新 更多