38

充分利用logstash的功能

 3 years ago
source link: http://mp.weixin.qq.com/s?__biz=MzAwOTU4NzM5Ng%3D%3D&%3Bmid=2455771616&%3Bidx=1&%3Bsn=a67290775db59afc9c879b367b9fcbcf
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

今天有个搜索的小需求,找了下解决方案,好久没弄了,生疏了很多。

需求就是原来一篇文章一个品牌,原来使用elk的整型字段存储,现在一篇文章对应多个品牌,如何设计呢?

解决方案就是在文章索引中新增一个数组字段,那么如何将数据录入进去呢?以前的整型字段值是否要合并进新字段?或者ELK同时对新老字段进行查询?

使用logstash的split和merge解决:

mutate { split => { "brand_ids" => ',' } }
mutate { merge => ["brand_ids", "brand_id"]}

mutate{
    convert => {"brand_ids"=> "integer"}
}

新字段是用逗号分隔的,表示多个品牌,先拆分成数组,然后将旧的品牌值合并到数组中,这样ELK查询新字段就可以了。

merge:

Merge two fields of arrays or hashes. String fields will be automatically be converted into an array

注意在logstash中并没有数组类型,integer仅仅表示数组值的类型。

convert:

Convert a field’s value to a different type, like turning a string to an integer. If the field value is an array, all members will be converted.

那么ELK如何查询呢?很简单:

"term": {
   "brand_ids":3
}

由于很久没弄了,所以生疏了很多,调试不方便,尤其logstash特别慢,修改配置文件后再重新运行很耗费时间,可以:

bin/logstash -f apache.config --config.reload.automatic

-r 参数可以自动加载新的配置文件。

深思了很多,如何才能更熟练,困难的是要重新导入数据到ELK中,不过只要删除logstash的pos文件就可以全量重新导入了,不会影响服务。

参考:

  • https://www.elastic.co/guide/en/logstash/current/plugins-filters-mutate.html#plugins-filters-mutate-merge
  • https://www.elastic.co/guide/en/logstash/current/plugins-filters-mutate.html#plugins-filters-mutate-convert
  • https://www.elastic.co/guide/en/logstash/current/reloading-config.html

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK