小于博客 小于博客
首页
  • Java学习笔记
  • Docker专区
  • 实战教程
  • Shell
  • 内存数据库
  • Vue学习笔记
  • Nginx
  • Php
  • CentOS
  • Docker
  • Gitlab
  • GitHub
  • MySql
  • MongoDB
  • OpenVPN
  • 配置文件详解
  • Other
  • ELK
  • K8S
  • Nexus
  • Jenkins
  • 随写编年
  • 电影音乐
  • 效率工具
  • 博客相关
  • 最佳实践
  • 迎刃而解
  • 学习周刊
关于
友链
  • 本站索引

    • 分类
    • 标签
    • 归档
  • 本站页面

    • 导航
    • 打赏
  • 我的工具

    • 备忘录清单 (opens new window)
    • 网站状态 (opens new window)
    • json2go (opens new window)
    • 微信MD编辑 (opens new window)
    • 国内镜像 (opens new window)
    • 出口IP查询 (opens new window)
    • 代码高亮工具 (opens new window)
  • 外站页面

    • 开往 (opens new window)
    • ldapdoc (opens new window)
    • HowToStartOpenSource (opens new window)
    • vdoing-template (opens new window)
GitHub (opens new window)

小于博客

行者常至,为者常成
首页
  • Java学习笔记
  • Docker专区
  • 实战教程
  • Shell
  • 内存数据库
  • Vue学习笔记
  • Nginx
  • Php
  • CentOS
  • Docker
  • Gitlab
  • GitHub
  • MySql
  • MongoDB
  • OpenVPN
  • 配置文件详解
  • Other
  • ELK
  • K8S
  • Nexus
  • Jenkins
  • 随写编年
  • 电影音乐
  • 效率工具
  • 博客相关
  • 最佳实践
  • 迎刃而解
  • 学习周刊
关于
友链
  • 本站索引

    • 分类
    • 标签
    • 归档
  • 本站页面

    • 导航
    • 打赏
  • 我的工具

    • 备忘录清单 (opens new window)
    • 网站状态 (opens new window)
    • json2go (opens new window)
    • 微信MD编辑 (opens new window)
    • 国内镜像 (opens new window)
    • 出口IP查询 (opens new window)
    • 代码高亮工具 (opens new window)
  • 外站页面

    • 开往 (opens new window)
    • ldapdoc (opens new window)
    • HowToStartOpenSource (opens new window)
    • vdoing-template (opens new window)
GitHub (opens new window)
  • Nexus系列文章

  • Jenkins系列文章

  • ELK笔记

    • ELK

    • FileBeat

    • LogStash

    • ElasticSearch

      • elasticsearch高可用的探索
      • elasticsearch索引管理
      • elasticsearch配置等优化
      • elasticsearch-7-x使用xpack进行安全认证
      • elasticsearch的mapping
        • 第一:创建索引的时候指定mapping
        • 2,logstash处理。
      • elasticsearch的日常维护参数
      • elasticsearch-ILM-索引生命周期管理探微
      • 如何优雅地下掉一台es节点
    • Kibana

  • Kubernetes笔记

  • 系列专题
  • ELK笔记
  • ElasticSearch
小于博客
2020-07-05
目录

elasticsearch的mapping

以往采集日志都是比较简单的操作,没有过多深入es的mapping等内容,不过有时候技能都是基于需求驱动的。

现有日志内容如下:

{"sign":"test-log","@timestamp":"2020-07-05T17:43:12+08:00","type":"filter","sale_id":2084090132,"sale_uri":"2003261352dvxv50","shop_id":47516579,"shop_uri":"1910201845lawpvt","cat_id":4,"sec_cat_id":4001,"rule":"startprice","description":"拍品起拍价\u003e0","score":0,"arguments":"{\"startPrice\":2600}"}
1

因为后期会对日志中一些内容进行聚合计算,因此要求日志中score字段写入之后是float类型,但是如果什么都不指定,那么默认写入之后,会分配一个其他的类型。

两种解决方式。

# 第一:创建索引的时候指定mapping

PUT test-index
{
    "mappings" : {
        "properties" : {
            "score" : {
                "type" : "float"
            }
        }
    }
}
1
2
3
4
5
6
7
8
9
10

返回结果:

{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "test-indexa"
}
1
2
3
4
5

查看索引mapping:

GET test-index/_mapping
1

返回结果

{
  "test-index" : {
    "mappings" : {
      "properties" : {
        "score" : {
          "type" : "float"
        }
      }
    }
  }
}
1
2
3
4
5
6
7
8
9
10
11

这样写进来之后对应的 score字段就是float类型了。

但是这样有一个问题,因为刚刚是指定了单个索引的mapping,正常情况下,我们的日志索引都会按天来存,那么新的索引就无法自动进行对照了。接下来要引入索引模板的配置定义。

PUT _template/template_test
{
  "index_patterns": ["test*"],
  "order" : 1,
    "settings" : {
        "number_of_shards": 1,
        "number_of_replicas" : 2
    },
    "mappings" : {
      "properties" : {
        "score" : {
          "type" : "float"
        }
      }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

创建一个索引模板,只要是以test开头的索引,那么创建索引并写入进来之后,对应的score字段就应该是float类型了。

GET test-index-2020-03-30/_mapping

{
  "test-index-2020-03-30" : {
    "mappings" : {
      "properties" : {
        "@timestamp" : {
          "type" : "date"
        },
        "@version" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "arguments" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "batch" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "cat_id" : {
          "type" : "long"
        },
        "description" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "host" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "path" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "rule" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "sale_id" : {
          "type" : "long"
        },
        "sale_uri" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "score" : {
          "type" : "float"
        },
        "sec_cat_id" : {
          "type" : "long"
        },
        "shop_id" : {
          "type" : "long"
        },
        "shop_uri" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "sign" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "type" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127

img

# 2,logstash处理。

还有一种相对简便的方案是在lgostash层面来做,让日志在从logstash转过来的时候,指定某些字段的类型,配置如下:

input {
  kafka {
    bootstrap_servers => "192.168.0.1:9092"
    group_id => "test-index"
    consumer_threads => 6
    topics => ["test-index"]
    client_id => "test-index"
    codec => "json"
    check_crcs => "false"
  }
}

filter {
   mutate {
      convert    => {
         "score" => "float"
      }
   }
}

output {
  elasticsearch {
    hosts => ["http://192.168.0.2:9208"]
    index => "test-index-%{+YYYY-MM-dd-HH}"
  }
}
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

实际生产中,也会利用这一功能,对NGINX的access日志进行一些特殊处理:

input {
  kafka {
      bootstrap_servers  => "192.168.0.1:9092"
      group_id          => "nginx_access"
      consumer_threads => 6
      topics            => "nginx_access"
      codec             => "json"
   }
}
filter {
   mutate {
      split           => ["request_uri" , "?"]
      add_field       => {
          "uri_path"  => "%{request_uri[0]}"
          "uri_query" => "%{request_uri[1]}"
      }
      remove_field    => ["request_uri"]
      convert         => {
         "response"               => "integer"
         "body_bytes_sent"        => "integer"
         "request_time"           => "float"
         "upstream_response_time" => "float"
      }
   }
}
output {
   elasticsearch {
      hosts => ["http://192.168.0.2:9208"]
      index => "nginx_access-%{+YYYY.MM.dd}-1"
   }
}
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
#elk#elasticsearch
上次更新: 2024/01/11, 14:21:50

← elasticsearch-7-x使用xpack进行安全认证 elasticsearch的日常维护参数→

最近更新
01
SpringBoot 快速实现 api 加密!
03-21
02
SpringBoot整合SQLite
03-07
03
SpringBoot配置使用H2数据库的简单教程
02-21
更多文章>
Theme by Vdoing | Copyright © 2017-2024 | 点击查看十年之约 | 豫ICP备2022014539号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式