1. 程式人生 > >如何為logstash+elasticsearch配置索引模板?

如何為logstash+elasticsearch配置索引模板?

在使用logstash收集日誌的時候,我們一般會使用logstash自帶的動態索引模板,雖然無須我們做任何定製操作,就能把我們的日誌資料推送到elasticsearch索引叢集中,但是在我們查詢的時候,就會發現,預設的索引模板常常把我們不需要分詞的欄位,給分詞了,這樣以來,我們的比較重要的聚合統計就不準確了: 

舉個例子,假如有10臺需要的監控的機器,他們的機器名如下: 

Java程式碼  

  1. search-0-170  

  2. search-1-171  

  3. search-2-172  

  4. search-3-173  

  5. search-4-174  

  6. search-5-175  

  7. search-6

    -176  

  8. search-7-177  

  9. search-8-178  

  10. search-9-179  



如果使用的是logstash的預設模板,它會按-切分機器名,這樣以來想統計那臺機器上的收集日誌最多就有問題了,所以這時候,就需要我們自定義一些索引模板了: 

在logstash與elasticsearch整合的時候,總共有如下幾種使用模板的方式: 

(1)使用預設自帶的索引模板 ,大部分的欄位都會分詞,適合開發和時候快速驗證使用 
(2)在logstash收集端自定義配置模板,因為分散在收集機器上,維護比較麻煩 
(3)在elasticsearc服務端自定義配置模板,由elasticsearch負責載入模板,可動態更改,全域性生效,維護比較容易 

以上幾種方式: 

使用第一種,最簡單,無須任何配置 
使用第二種,適合小規模叢集的日誌收集,需要在logstash的output外掛中使用template指定本機器上的一個模板json路徑, 例如  template => "/tmp/logstash.json" 
使用第三種,適合大規模叢集的日誌收集,如何配置,主要配置logstash的output外掛中兩個引數: 


Java程式碼  

  1. manage_template => false//關閉logstash自動管理模板功能  

  2.  template_name => "crawl"//對映模板的名字  




如果使用了,第三種需要在elasticsearch的叢集中的config/templates路徑下配置模板json,在elasticsearch中索引模板可分為兩種: 

(一):靜態模板 

適合索引欄位資料固定的場景,一旦配置完成,不能向裡面加入多餘的欄位,否則會報錯 

優點:scheam已知,業務場景明確,不容易出現因欄位隨便對映從而造成元資料撐爆es記憶體,從而導致es叢集全部宕機 
缺點:欄位數多的情況下配置稍繁瑣 

一個靜態索引模板配置例子如下: 


Json程式碼  

  1. {  

  2.   "crawl" : {  

  3.       "template""crawl-*",  

  4.         "settings": {  

  5.             "index.number_of_shards"3,  

  6.             "number_of_replicas"0   

  7.         },  

  8.     "mappings" : {  

  9.       "logs" : {  

  10.         "properties" : {  

  11.           "@timestamp" : {  

  12.             "type" : "date",  

  13.             "format" : "dateOptionalTime",  

  14.             "doc_values" : true  

  15.           },  

  16.           "@version" : {  

  17.             "type" : "string",  

  18.             "index" : "not_analyzed",  

  19.         "doc_values" : true      

  20.           },  

  21.           "cid" : {  

  22.             "type" : "string",  

  23.             "index" : "not_analyzed"  

  24.           },  

  25.           "crow" : {  

  26.             "type" : "string",  

  27.             "index" : "not_analyzed"  

  28.           },  

  29.           "erow" : {  

  30.             "type" : "string",  

  31.             "index" : "not_analyzed"  

  32.           },  

  33.           "host" : {  

  34.             "type" : "string",  

  35.             "index" : "not_analyzed"  

  36.           },  

  37.           "httpcode" : {  

  38.             "type" : "string",  

  39.             "index" : "not_analyzed"  

  40.           },  

  41.           "message" : {  

  42.             "type" : "string"  

  43.           },  

  44.           "path" : {  

  45.             "type" : "string"  

  46.           },  

  47.           "pcode" : {  

  48.             "type" : "string",  

  49.             "index" : "not_analyzed"  

  50.           },  

  51.           "pro" : {  

  52.             "type" : "string",  

  53.             "index" : "not_analyzed"  

  54.           },  

  55.           "ptype" : {  

  56.             "type" : "string",  

  57.             "index" : "not_analyzed"  

  58.           },  

  59.           "save" : {  

  60.             "type" : "string",  

  61.             "index" : "not_analyzed"  

  62.           },  

  63.           "t1" : {  

  64.             "type" : "string",  

  65.             "index" : "not_analyzed"  

  66.           },  

  67.           "t2" : {  

  68.             "type" : "string",  

  69.             "index" : "not_analyzed"  

  70.           },  

  71.           "t3" : {  

  72.             "type" : "string",  

  73.             "index" : "not_analyzed"  

  74.           },  

  75.           "url" : {  

  76.             "type" : "string"  

  77.           }  

  78.         }  

  79.       }  

  80.     }  

  81.   }  

  82. }  




(二):動態模板 
適合欄位數不明確,大量欄位的配置型別相同的場景,多加欄位不會報錯 

優點:可動態新增任意欄位,無須改動scheaml, 
缺點:如果新增的欄位非常多,有可能造成es叢集宕機 

如下的一個logstash的動態索引模板,只設置message欄位分詞,其他的欄位預設都不分詞 

Json程式碼  

  1. {  

  2.   "template" : "crawl-*",  

  3.   "settings" : {  

  4.    "index.number_of_shards"5,  

  5.    "number_of_replicas"0    

  6. },  

  7.   "mappings" : {  

  8.     "_default_" : {  

  9.       "_all" : {"enabled" : true, "omit_norms" : true},  

  10.       "dynamic_templates" : [ {  

  11.         "message_field" : {  

  12.           "match" : "message",  

  13.           "match_mapping_type" : "string",  

  14.           "mapping" : {  

  15.             "type" : "string""index" : "analyzed""omit_norms" : true,  

  16.             "fielddata" : { "format" : "disabled" }  

  17.           }  

  18.         }  

  19.       }, {  

  20.         "string_fields" : {  

  21.           "match" : "*",  

  22.           "match_mapping_type" : "string",  

  23.           "mapping" : {  

  24.             "type" : "string""index" : "not_analyzed""doc_values" : true  

  25.           }  

  26.         }  

  27.       } ],  

  28.       "properties" : {  

  29.         "@timestamp": { "type""date" },  

  30.         "@version": { "type""string""index""not_analyzed" },  

  31.         "geoip"  : {  

  32.           "dynamic": true,  

  33.           "properties" : {  

  34.             "ip": { "type""ip" },  

  35.             "location" : { "type" : "geo_point" },  

  36.             "latitude" : { "type" : "float" },  

  37.             "longitude" : { "type" : "float" }  

  38.           }  

  39.         }  

  40.       }  

  41.     }  

  42.   }  

  43. }  





總結: 

定製索引模板,是搜尋業務中一項比較重要的步驟,需要注意的地方有很多,比如: 
(1)欄位數固定嗎 
(2)欄位型別是什麼 
(3)分不分詞 
(4)索引不索引 
(5)儲存不儲存 
(6)排不排序 
(7)是否加權 
除了這些還有其他的一些因素,比如,詞庫的維護改動,搜尋架構的變化等等。 
如果前提沒有充分的規劃好,後期改變的話,改動其中任何一項,都需要重建索引,這個代價是非常大和耗時的,尤其是在一些資料量大的場景中。