【es】通过API操作es集群

ES是海量数据查询的搜索引擎,广泛用于全文检索、日志分析、监控分析等场景。

ES有三个特点轻松支持各种复杂的查询条件:

1.分布式实时文件存储,采用倒排索引及自定义打分、排序能力与丰富的分词插件等,实现复杂查询条件的全文检索需求。

2.可扩展性强:天然支持分布式存储,可简单实现上千台服务器的分布式横向火绒。

3.高可用,容灾性能好:通过主备节点及故障自动检测与恢复,实现高可用。

一、关于ES和MySQL的概念对比

ES7.x之前版本和MySQL的结构对比

MySQL 中的数据库(DataBase),等价于 ES 中的索引(Index)。

MySQL 中一个数据库下面有 N 张表(Table),等价于1个索引 Index 下面有 N 多类型(Type)。

MySQL 中一个数据库表(Table)下的数据由多行(Row)多列(column,属性)组成,等价于1个 Type 由多个文档(Document)和多 Field 组成。

MySQL 中定义表结构、设定字段类型等价于 ES 中的 Mapping。举例说明,在一个关系型数据库里面,Schema 定义了表、每个表的字段,还有表和字段之间的关系。与之对应的,在 ES 中,Mapping 定义索引下的Type的字段处理规则,即索引如何建立、索引类型、是否保存原始索引 JSON 文档、是否压缩原始 JSON 文档、是否需要分词处理、如何进行分词处理等。

MySQL 中的增 insert、删 delete、改 update、查 search 操作等价于 ES 中的增 PUT/POST、删 Delete、改 _update、查 GET。其中的修改指定条件的更新 update 等价于 ES 中的 update_by_query,指定条件的删除等价于 ES 中的 delete_by_query。

MySQL 中的 group by、avg、sum 等函数类似于 ES 中的 Aggregations 的部分特性。

MySQL 中的去重 distinct 类似 ES 中的 cardinality 操作。

MySQL 中的数据迁移等价于 ES 中的 reindex 操作。

ES7.X版本和MySQL的结构对比

二、ES相关接口操作

通过API操作es集群

https://endymecy.gitbooks.io/elasticsearch-guide-chinese/content/getting-started/exploring-cluster.html

我们可以发现访问Elasticsearch中数据的一个模式:

curl -X<REST Verb> <Node>:<Port>/<Index>/<Type>/<ID>

这个REST访问模式普遍适用于所有的API命令,如果你能记住它,你就会为掌握Elasticsearch 开一个好头。

查看分类

curl http://xx.xx.xx.xx:9200/_cat

查看集群节点

curl http://10.64.3.9:9200/_cat/nodes

查看节点详情  

curl http://10.64.3.9:9200/_nodes/process?pretty

查看分片状态

curl http://xx.xx.xx.xx:9200/_cat/shards

统计索引数据量

curl http://xx.xx.xx.xx:9200/_cat/count/${index_name}

查看索引设置

 curl http://xx.xx.xx.xx:9200/${index_name}/_settings?pretty

查看索引个数和每个索引概况

 curl http://xx.xx.xx.xx:9200/_cat/indices

注:indices为index的复数

查看集群分片分配详情

curl  -X  GET xx.xx.xx.xx:9200/_cluster/allocation/explain?pretty

为索引的每个分片设置副本数

curl    -X  PUT   http://xx.xx.xx.xx:9200/${index_name}/_settings  \

--header 'Content-Type: application/json'   \

 -d '{"index":{"number_of_replicas":1}}'

创建索引 

curl -X PUT http://xx.xx.xx.xx:9200/${index_name}

删除索引

 curl -X DELETE  http://xx.xx.xx.xx:9200/${index_name}

查看索引的mapping详情

curl http://xx.xx.xx.xx:9200/${index_name}/_mapping?pretty

查看集群状态

curl  -X GET -s   http://xx.xx.xx.xx:9200/_cluster/health?pretty

$ curl http://10.64.3.9:9201/_cat?

#############################

=^.^=

/_cat/allocation

/_cat/shards

/_cat/shards/{index}

/_cat/master

/_cat/nodes

/_cat/tasks

/_cat/indices

/_cat/indices/{index}

/_cat/segments

/_cat/segments/{index}

/_cat/count

/_cat/count/{index}

/_cat/recovery

/_cat/recovery/{index}

/_cat/health

/_cat/pending_tasks

/_cat/aliases

/_cat/aliases/{alias}

/_cat/thread_pool

/_cat/thread_pool/{thread_pools}

/_cat/plugins

/_cat/fielddata

/_cat/fielddata/{fields}

/_cat/nodeattrs

/_cat/repositories

/_cat/snapshots/{repository}

/_cat/templates

#####################################

三、参考

ES与MySQL的概念对比

https://www.cnblogs.com/smallzhen/p/14711478.html

通过API操作es集群

https://endymecy.gitbooks.io/elasticsearch-guide-chinese/content/getting-started/exploring-cluster.html

Elasticsearch Guide [7.5] » REST APIs

https://www.elastic.co/guide/en/elasticsearch/reference/7.5/rest-apis.html

通过API操作阿里云Elasticsearch

https://help.aliyun.com/document_detail/155919.html

elasticsearch-guide

https://endymecy.gitbooks.io/elasticsearch-guide-chinese/content/index.html

json测试数据生成器

https://www.json-generator.com

The most elegant way to create random data.

With help of powerful and flexible templates written in good old JavaScript.

Elasticsearch Guide [7.14] » REST APIs » Document APIs

https://www.elastic.co/guide/en/elasticsearch/reference/current/docs.html

猜你喜欢

转载自blog.csdn.net/michaelwoshi/article/details/119921404