海量级日志系统ElasticSearch技术实践
主要的基础概念有:Node, Index,Type,Document,Field,shard和replicas.
Node(节点):运行单个ES实例的服务器
Cluster(集群):一个或多个节点构成集群
Index(索引):索引是多个文档的集合
Type(类型):一个Index可以定义一种或多种类型,将Document逻辑分组
Document(文档):Index里每条记录称为Document,若干文档构建一个Index
Field(字段):ES存储的最小单元
Shards(分片):ES将Index分为若干份,每一份就是一个分片
Replicas(副本):Index的一份或多份副本
为了便于理解,我们和mysql这种关系型数据库做一个对比:
关系型数据库(如mysql,oracle等) | elasticsearch |
---|---|
database或schema | index |
table | type |
row | document |
column或field | field |
ES是分布式搜索引擎,每个索引有一个或多个分片(shard),索引的数据被分配到各个分片上。你可以看作是一份数据分成了多份给不同的节点。
当ES集群增加或删除节点时,shard会在多个节点中均衡分配。默认是5个primary shard(主分片)和1个replica shard(副本,用于容错)。
ES基础API操作
前面我们通过http://es02:9200/_cluster/health?pretty查看ES集群状态,其实就是它的一种API操作。
什么是API?
API(Application Programming Interface)应用程序编程接口,就是无需访问程序源码或理解内部工作机制就能实现一些相关功能的接口。
RestFul API 格式
curl -X<verb> ‘<protocol>://<host>:<port>/<path>?<query_string>’-d ‘<body>’
参数 | 描述 |
---|---|
verb | HTTP方法,比如GET、POST、PUT、HEAD、DELETE |
host | ES集群中的任意节点主机名 |
port | ES HTTP服务端口,默认9200 |
path | 索引路径 |
query_string | 可选的查询请求参数。例如?pretty参数将返回JSON格式数据 |
-d | 里面放一个GET的JSON格式请求主体 |
body | 自己写的 JSON格式的请求主体 |
elasticseearch的API很多, 我们运维人员主要用到以下几个要介绍的较简单的API。
更多API参考: https://www.elastic.co/guide/en/elasticsearch/reference/6.2/index.html
查看节点信息
通过curl或浏览器访问http://es02:9200/_cat/nodes?v(ip为ES节点IP,如果有ES集群,则为ES任意节点IP)
#curl http://es01:9200/_cat
#*:表示的是master;从机:-符号
[root@es01 ~]# curl http://es01:9200/_cat/nodes?v
ip heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
es02 29 94 2 2.33 1.88 0.85 mdi - es02
es01 26 92 0 0.24 0.37 0.33 mdi * es01
[root@es02 ~]#curl http://es01:9200/_cat/master
py4qa8rOQW64xtzncVGeCA 10.0.0.71 10.0.0.71 es01
查看索引信息
通过curl或浏览器访问http://es02:9200/_cat/indices?v
[root@es01 ~]# curl http://es01:9200/_cat/indices?v
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
默认现在没有任何索引
#不用记忆,对照即可
[root@es02 ~]#curl http://es02:9200/_cat/indices
green open .geoip_databases 6lYd3xPuTpiL9RQi6u2l2w 1 1 40 0 76.2mb 38.1mb
新增索引
[root@es01 ~]# curl -X PUT http://es02:9200/nginx_access_log
#下面是响应的结果
{"acknowledged":true,"shards_acknowledged":true,"index":"nginx_access_log"}
[root@es01 ~]# curl http://es02:9200/_cat/indices?v
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open nginx_access_log 90Z7DvInTz6seXMBYhHVAw 5 1 0 0 2.2kb 1.1kb
460b
green:所有的主分片和副本分片都已分配。你的集群是100%可用的。
yellow:所有的主分片已经分片了,但至少还有一个副本是缺失的。不会有数据丢失,所以搜索结果依然是完整的。不过,你的高可用性在某种程度上被弱化。如果 更多的 分片消失,你就会丢数据了。把 yellow 想象成一个需要及时调查的警告。
red:至少一个主分片(以及它的全部副本)都在缺失中。这意味着你在缺少数据:搜索只能返回部分数据,而分配到这个分片上的写入请求会返回一个异常。
删除索引
[root@es01 ~]# curl -X DELETE http://es02:9200/nginx_access_log
{"acknowledged":true}
修改:扩展暂时省略
curl -X PUT "http://es02:9200/nginx_access_log/_mapping" -H 'Content-Type: application/json' -d'
{
"properties": {
"response_time": {"type": "float"}
}
}
ES查询语句
ES提供一种可用于执行查询JSON式的语言,被称为Query DSL。
针对elasticsearch的操作,可以分为增、删、改、查四个动作。
查询匹配条件:
-
match_all
-
from,size
-
match
-
bool
-
range
查询应用案例:
导入数据源
使用官方提供的示例数据:
#上传accounts.json到linux
#导入进elasticsearch,bank索引,文档_doc,_bulk:批量
[root@es01 ~]# curl -H "Content-Type: application/json" -XPOST "es01:9200/bank/_doc/_bulk?pretty&refresh" --data-binary "@accounts.json"
查询确认
[root@es01 ~]# curl "es01:9200/_cat/indices?v"
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open bank CzFQ_Gu1Qr2-bpV5MF0OBg 5 1 1000 0 874.7kb 434.4kb
2, 查询bank索引的数据(使用查询字符串进行查询)
[root@es01 ~]# curl -X GET "es01:9200/bank/_search?q=*&sort=account_number:asc&pretty"
说明:
默认结果为10条
_search 属于一类API,用于执行查询操作
q=* ES批量索引中的所有文档
sort=account_number:asc 表示根据account_number按升序对结果排序
pretty调整显示格式
3, 查询bank索引的数据 (使用json格式进行查询)
[root@es01 ~]# curl -X GET "es01:9200/bank/_search" -H 'Content-Type: application/json' -d'
{
"query": { "match_all": {} },
"sort": [
{ "account_number": "asc" }
]
}
'
注意: 最后为单引号
问题: 怎么将上面json格式进行pretty查询?
查询匹配动作及案例:
-
match_all
-
匹配所有文档。默认查询
-
示例:查询所有,默认只返回10个文档
-
[root@es01 ~]# curl -X GET "es01:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": { "match_all": {} }
}
'
# query告诉我们查询什么
# match_all是我们查询的类型
# match_all查询仅仅在指定的索引的所有文件进行搜索
-
from,size
-
除了query参数外,还可以传递其他参数影响查询结果,比如前面提到的sort,接下来使用的size
-
[root@es01 ~]# curl -X GET "es01:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": { "match_all": {} },
"size": 1
}
'
查询1条数据
指定位置与查询条数
[root@es01 ~]# curl -X GET "es01:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": { "match_all": {} },
"from": 0,
"size": 2
}
'
from 0表示从第1个开始
size 指定查询的个数
示例: 查询account_number从第501条到510条的数据
curl -X GET "es01:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": { "match_all": {} },
"from": 500,
"size": 10,
"sort": [
{ "account_number": "asc" }
]
}
' 2>/dev/null |grep account_number
#操作两次:第一次写''内容;第二次全部,看account_number
#找想要的字段,字段排序,balance余额.
-
匹配查询字段
-
返回_source字段中的片段字段
-
限定返回的列名
[root@es01 ~]# curl -X GET "es01:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": { "match_all": {} },
"_source": ["account_number", "balance"]
}
'
-
match
-
基本搜索查询,针对特定字段或字段集合进行搜索
-
查询编号为20的账户
[root@es01 ~]# curl -X GET "es01:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": { "match": { "account_number": 20 } }
}
'
返回地址中包含mill的账户
[root@es01 ~]# curl -X GET "es01:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": { "match": { "address": "mill" } }
}
'
返回地址有包含mill或lane的所有账户
[root@es01 ~]# curl -X GET "es01:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": { "match": { "address": "mill lane" } }
}
'|grep address
-
bool
bool must 查询的字段必须同时存在
查询包含mill和lane的所有账户
[root@es01 ~]# curl -X GET "es01:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"must": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}
'
bool should 查询的字段仅存在一即可
查询包含mill或lane的所有账户
[root@es01 ~]# curl -X GET "es01:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"should": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}
'|grep address
#这个是 "query": { "match": { "address": "mill lane" } }效果一致。
-
range
-
指定区间内的数字或者时间
-
操作符:gt大于,gte大于等于,lt小于,lte小于等于
-
查询余额大于或等于20000且小于等于30000的账户
[root@es01 ~]# curl -X GET "es01:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"must": { "match_all": {} },
"filter": {
"range": {
"balance": {
"gte": 20000,
"lte": 30000
}
}
}
}
}
}
'|grep address
总结
索引操作
创建索引
功能:指定分片、副本数及映射规则。
示例:PUT /<index_name>,可定义 settings(如分片数)和 mappings(字段类型)。
修改索引配置
动态参数:如副本数、刷新间隔等,可通过 PUT /<index_name>/_settings 调整。
静态参数:如初始分片数,仅能在创建时设置2。
删除索引
命令:DELETE /<index_name>,直接移除整个索引及其数据。
判断索引状态
存在性检测:HEAD /<index_name>,返回成功表示存在。
文档操作(索引内数据操作)
新增文档
方式:PUT /<index>/_doc/<id>(覆盖已有文档);POST /<index>/_doc/<id>(自动生成ID)。
区别:CREATE 仅在文档不存在时成功。
查询文档
单条查询:GET /<index>/_doc/<id>,获取完整文档及元数据。
存在性检测:HEAD /<index>/_doc/<id>。
更新文档
局部更新:POST /<index>/_update/<id>,通过 doc 字段修改特定属性。
全量覆盖:PUT /<index>/_doc/<id>,替换整个文档。
删除文档
命令:DELETE /<index>/_doc/<id>,软删除(标记为已删除,后续合并时物理去除)。
- 点赞
- 收藏
- 关注作者
评论(0)