elk日志无法写入问题

问题描述

今天在查看logstash日志时,发现大量映射错误

1
[2019-10-09T06:54:10,480][WARN ][logstash.outputs.elasticsearch] Failed action. {:status=>400, :action=>["index", {:_id=>nil, :_index=>"discovery-req-2019.10.09", :_type=>"logs", :_routing=>nil}, 2019-10-09T06:54:08.553Z 10.77.105.53 %{message}], :response=>{"index"=>{"_index"=>"index-2019.10.09", "_type"=>"logs", "_id"=>"AW2vS-KfYuqYnWgYJN6D", "status"=>400, "error"=>{"type"=>"mapper_parsing_exception", "reason"=>"failed to parse [doc.seqid]", "caused_by"=>{"type"=>"number_format_exception", "reason"=>"For input string: \"157060404810501266095\""}}}}}

问题解决

查阅资料后了解,一个索引创建时提前设定字段及字段映射类型,elasticsearch为了方便数据插入使用了字段映射自动检测机制,在索引插入第一条数据时会自动对相关字段根据检测结果增加相应映射信息,那是怎么导致如上报错的呢?
如例子所示,插入第一条记录的同时es创建了索引twitter,并且根据添加数据自动判断增加字段映射,其中count字段映射为long,新增数据时count值为字符串且按照数字的话超出long范畴,所以报错(如果不超出long范畴可以正常字符串的形式存储)

为解决这个问题,我们有多种办法
1.使用索引模板,对指明count字段映射为text
2.在写入数据时,count值改为字符串,这样在自动添加映射关系时会以text方式添加

当索引映射创建后便不可再做修改

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//插入数据
PUT twitter/tweet/1
{
"count":1
}
GET twitter/tweet/1
//结果:
{
"_index": "twitter",
"_type": "tweet",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"count": 1
}
}
//查看映射信息
GET twitter/_mapping
//结果:
{
"twitter": {
"mappings": {
"tweet": {
"properties": {
"count": {
"type": "long"
}
}
}
}
}
}
//新增记录
PUT twitter/tweet/2
{
"count":"123456789123456789123456789"
}
//结果
{
"error": {
"root_cause": [
{
"type": "mapper_parsing_exception",
"reason": "failed to parse [count]"
}
],
"type": "mapper_parsing_exception",
"reason": "failed to parse [count]",
"caused_by": {
"type": "number_format_exception",
"reason": "For input string: \"123456789123456789123456789\""
}
},
"status": 400
}