我正在ElasticSearch 6.8中试用ip_range
字段类型(https://www.elastic.co/guide/en/elasticsearch/reference/6.8/range.html)并且很难找到通过logstash将ip数据正确加载到字段中的方法
我能够通过Kibana Dev Tools加载一些示例数据,但无法找到通过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_from
和ip_to
DB字段分别放入ip_from_to_range
的gte
和lte
部分??
我知道我也可以用CIDR表示法插入ip范围,但我希望能够同时使用这两种选项——用CIDR符号加载和作为范围加载。
经过一番尝试和错误,终于找到了logstash配置。
我在这里发布了一个类似的问题,这最终也让我对这个用例的语法走上了正轨。
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 { ... }
过滤器部件说明
mutate add_field
:创建一个新字段[ip_from_to_range]
,其值为json字符串("{…}"(。将字段设为[field_name]
是很重要的,否则将字符串解析为json对象的下一步就无法工作json
:将字符串表示解析为json对象