0


ELK之Elasticsearch常用curl命令

一、前言:其实官网上都有

相关的kibana API按照curl的形式赋值出来就行了。

举个例子:如下对比查看索引的kibana指令与curl指令。

  1. #kibana指令,查看es集群下的索引
  2. GET /_cat/indices?v
  3. #如下为复制出来的对应curl指令
  4. #注:对于使用者来说记得把ip:port更换成你当前的ip:port并且确保能访问通就可以了
  5. curl -X GET "localhost:9200/_cat/indices?v"
  6. #如下都是可以的
  7. curl -X GET "10.101.203.15:9200/_cat/indices/?v"
  8. curl -X GET '10.101.203.15:9200/_cat/indices/?v'
  9. curl -X GET 'http://10.101.203.15:9200/_cat/indices?v'

用户名密码认证

如果需要用户名、密码认证的话就在 ip:port 前加上用户名密码,即 {username}:{password}@ip:port;值得注意的是同mongodb一样password中如果有特殊字符也要进行urlencode处理。

  1. #如用户名为qduser0,密码为 qidian)8el1}, 域名为 qd-es.infr.tce.io,则链接语句为
  2. curl -X GET "http://qduser0:qidian)8el1%7D@qd-es.infr.tce.io:48888/_cat/indices/es_qidian_flow_online*?pretty"
  3. 注:密码中的特殊字符,如"}"要进行urlencode才行

二、常用curl命令的罗列

接下来就是罗列一些常用的curl命令了。

1、索引相关操作

  1. 1、查看索引
  2. #列出所有索引
  3. curl -X GET "10.101.203.15:9200/_cat/indices/?v"
  4. #列出"es_qidian_flow"开头的索引
  5. curl -X GET "10.101.203.15:9200/_cat/indices/es_qidian_flow*?v"
  6. GET /_cat/indices/es_qidian_flow*?v
  7. 2、创建索引
  8. #创建一个名为twitter的索引
  9. curl -X PUT "10.101.203.15:9200/twitter?pretty" -H 'Content-Type: application/json' -d'
  10. {
  11. "settings" : {
  12. "index" : {
  13. "number_of_shards" : 3,
  14. "number_of_replicas" : 2
  15. }
  16. }
  17. }
  18. '
  19. #查看这个索引
  20. curl -X GET "10.101.203.15:9200/_cat/indices/twitter*?v"
  21. 3、删除索引
  22. #删除名称为twitter的索引
  23. curl -X DELETE "10.101.203.15:9200/twitter?pretty"
  24. DELETE /twitter

创建索引执行效果如下:

2、查询相关操作 —— _search

官网

  1. #kibana DSL,查询flow_time为1650264117的文档
  2. GET es_qidian_flow_oa_v2_202204/session/_search?routing=2852199840&pretty
  3. {
  4. "query":{
  5. "term":{ "flow_time" : "1650264117" }
  6. }
  7. }
  8. #curl实现同样功能
  9. curl -X GET "10.101.203.15:9200/es_qidian_flow_oa_v2_202204/session/_search?routing=2852199840&pretty" -H 'Content-Type: application/json' -d'
  10. {
  11. "query" : {
  12. "term" : { "flow_time" : "1650264117" }
  13. }
  14. }
  15. '
  16. #kibana DSL,查询msg_content为"大家都觉得你不懂"的文档
  17. GET es_qidian_flow_oa_v2_202204/session/_search?routing=2852199840&pretty
  18. {
  19. "query":{
  20. "match":{
  21. "msg_content":"大家都觉得你不懂"
  22. }
  23. }
  24. }
  25. #curl语句实现同样的功能
  26. curl -X GET "10.101.203.15:9200/es_qidian_flow_oa_v2_202204/session/_search?routing=2852199840&pretty" -H 'Content-Type: application/json' -d'
  27. {
  28. "query":{
  29. "match":{
  30. "msg_content":"大家都觉得你不懂"
  31. }
  32. }
  33. }
  34. '
  35. 注:可以看到主题查询语句部分都是一样的,没有必要继续演示了。

注:其中的pretty的作用就是可以让输出格式更好看一些,和mongodb的.pretty()是一样效果!!

** 注:授人以鱼不如授人以渔,感觉这样就够了;继续罗列下去也没啥意思,用到啥查啥就好。**

后面就是有目的的罗列了,用到上就整理啥。

2.2、count相关操作 —— _count 官网

统计指令,查询满足条件的数据条数。

  1. #查询整个 es_qidian_flow_oa_v2_202205 索引的数据条数
  2. GET es_qidian_flow_oa_v2_202205/_count
  3. #curl版本 查询整个 es_qidian_flow_oa_v2_202205 索引的数据条数
  4. curl -X GET "localhost:9200/es_qidian_flow_online_v2_202205/_count?pretty" -H 'Content-Type: application/json' -d'
  5. {
  6. }
  7. '
  8. #查询满足条件的数据条数(参数作为简单查询串)
  9. GET /twitter/tweet/_count?q=user:kimchy
  10. #查询满足条件的数据条数(DSL查询语句)
  11. GET /twitter/tweet/_count
  12. {
  13. "query" : {
  14. "term" : { "user" : "kimchy" }
  15. }
  16. }
  17. #curl 查询满足条件的数据条数(参数作为简单查询串)
  18. curl -X GET "localhost:9200/twitter/tweet/_count?q=user:kimchy&pretty"
  19. #curl 查询满足条件的数据条数(DSL查询语句)
  20. curl -X GET "localhost:9200/twitter/tweet/_count?pretty" -H 'Content-Type: application/json' -d'
  21. {
  22. "query" : {
  23. "term" : { "user" : "kimchy" }
  24. }
  25. }
  26. '

3、模板相关操作—— templates

Index Templates | Elasticsearch Guide [5.3] | Elastic

看起来也没什么可说的。

3.1 查看模板

  1. #查看名称为es_qidian_flow_oa_template的模板
  2. GET /_template/es_qidian_flow_oa_template
  3. curl -X GET "10.101.203.15:9200/_template/es_qidian_flow_oa_template?pretty"
  4. #查看所有模板
  5. curl -X GET "10.101.203.15:9200/_template"
  6. #查看以"es_qidian_flow_"开头的模板
  7. curl -X GET "10.101.203.15:9200/_template/es_qidian_flow_*"

3.2 创建模板

  1. #创建名称为 es_qidian_flow_online_template 的模板,如下
  2. curl -X PUT "10.101.203.15:9200/_template/es_qidian_flow_online_template?pretty" -H 'Content-Type: application/json' -d'
  3. {
  4. "order": 0,
  5. "template": "es_qidian_flow_online_v2_*",
  6. "settings": {
  7. "index": {
  8. "routing": {
  9. "allocation": {
  10. "include": {
  11. "temperature": "hot"
  12. }
  13. }
  14. },
  15. "refresh_interval": "120s",
  16. "number_of_shards": "1",
  17. "routing_partition_size": "1",
  18. "translog": {
  19. "sync_interval": "5s",
  20. "durability": "async"
  21. },
  22. "analysis": {
  23. "analyzer": {
  24. "qq_relation_ngram": {
  25. "tokenizer": "qq_relation_ngram_tokenizer"
  26. }
  27. },
  28. "tokenizer": {
  29. "qq_relation_ngram_tokenizer": {
  30. "token_chars": [
  31. "digit",
  32. "letter"
  33. ],
  34. "min_gram": "1",
  35. "type": "ngram",
  36. "max_gram": "1"
  37. }
  38. }
  39. },
  40. "number_of_replicas": "1"
  41. }
  42. },
  43. "mappings": {
  44. "session": {
  45. "_routing": {
  46. "required": true
  47. },
  48. "properties": {
  49. "session_id": {
  50. "type": "keyword"
  51. },
  52. "flow_time": {
  53. "type": "date",
  54. "format": "epoch_second||strict_date_optional_time"
  55. },
  56. "flow_type": {
  57. "type": "long"
  58. },
  59. "kfuin": {
  60. "type": "long"
  61. },
  62. "kfext": {
  63. "type": "long"
  64. },
  65. "robotid": {
  66. "type": "long"
  67. },
  68. "device_type": {
  69. "type": "long"
  70. },
  71. "msg_content": {
  72. "type": "text",
  73. "analyzer": "qq_relation_ngram",
  74. "fields": {
  75. "ik": {
  76. "type": "text",
  77. "analyzer": "ik_max_word"
  78. }
  79. }
  80. },
  81. "msg_direction": {
  82. "type": "long"
  83. },
  84. "customer_name": {
  85. "type": "keyword"
  86. },
  87. "account_type": {
  88. "type": "long"
  89. },
  90. "account": {
  91. "type": "keyword"
  92. },
  93. "msg_type": {
  94. "type": "long"
  95. },
  96. "qqpub": {
  97. "type": "long"
  98. },
  99. "relation_type": {
  100. "type": "long"
  101. },
  102. "account_aggr_field": {
  103. "type": "keyword"
  104. },
  105. "sort_type": {
  106. "type": "long"
  107. }
  108. }
  109. }
  110. },
  111. "aliases": {
  112. "es_qidian_flow_online": {}
  113. }
  114. }
  115. '

routing_partition_size:自定义路由到分片,适当降低routing带来的数据倾斜问题。这里

此参数在索引创建时结合着routing一起使用,其意义是使得写入的数据能够集中的落入到routing_partition_size个分片集合中,而不是只能落在一个分片。ES官网指出routing_partition_size的值通常设置为大于1且小于number_of_shards。

  1. 对比两个计算公式,默认:
  2. shard_num = hash(_routing) % num_primary_shards
  3. 设置之后:
  4. shard_num = (hash(_routing) + hash(_id) % routing_partition_size) % num_primary_shards
  5. 不难发现,设置这个参数之后,可以通过routing,id两个参数来决定数据在哪个分片。这样做的目的,是让数据尽可能的均匀分布,解决单一hash的热点问题。

比如索引my_index包含3个分片,若此时routing_partition_size的值设为2,那经过routing写入到my_index的数据只会落入其中的两个分片,而另一个会处于闲置状态。

3.3 删除模板

  1. DELETE /_template/template_1
  2. curl -X DELETE "localhost:9200/_template/template_1?pretty"

4、插入文档

按照模板的使用方法,这里尝试插入一条命中上述es_qidian_flow_online_template的文档。

具体来说就是索引名称满足 "es_qidian_flow_online_v2_*" 正则匹配即可。

  1. #DSL插入相关数据
  2. POST es_qidian_flow_online_v2_202205/session?routing=2852165588
  3. {
  4. "account": "wxfe15ad577e0382e5_ogPSI0vfhhXBRUbK7qtIQ9lTEZog",
  5. "account_aggr_field": "3_wxfe15ad577e0382e5_ogPSI0vfhhXBRUbK7qtIQ9lTEZog",
  6. "account_type": 3,
  7. "device_type": 1,
  8. "flow_time": 1652579483,
  9. "flow_type": 4,
  10. "kfext": 3008063101,
  11. "kfuin": 2852165588,
  12. "msg_content": "后台数据涉及游戏代码无法给您提供",
  13. "msg_direction": 1,
  14. "msg_type": 0,
  15. "qqpub": 0,
  16. "relation_type": 0,
  17. "retract_msg": 0,
  18. "robotid": 0,
  19. "session_id": "wx_2852165588_wxfe15ad577e0382e5_ogPSI0vfhhXBRUbK7qtIQ9lTEZog_1652576687947",
  20. "sort_type": 1
  21. }
  22. #curl指令插入相关数据
  23. curl -POST "10.101.203.15:9200/es_qidian_flow_online_v2_202205/session?routing=2852165588" -H 'Content-Type: application/json' -d'
  24. {
  25. "account": "wxfe15ad577e0382e5_ogPSI0vfhhXBRUbK7qtIQ9lTEZog",
  26. "account_aggr_field": "3_wxfe15ad577e0382e5_ogPSI0vfhhXBRUbK7qtIQ9lTEZog",
  27. "account_type": 3,
  28. "device_type": 1,
  29. "flow_time": 1652579483,
  30. "flow_type": 4,
  31. "kfext": 3008063101,
  32. "kfuin": 2852165588,
  33. "msg_content": "后台数据涉及游戏代码无法给您提供",
  34. "msg_direction": 1,
  35. "msg_type": 0,
  36. "qqpub": 0,
  37. "relation_type": 0,
  38. "retract_msg": 0,
  39. "robotid": 0,
  40. "session_id": "wx_2852165588_wxfe15ad577e0382e5_ogPSI0vfhhXBRUbK7qtIQ9lTEZog_1652576687947",
  41. "sort_type": 1
  42. }
  43. '

如下为插入数据后的效果,显然ES自动创建了这个索引。由此可见模板生效了,完全符合预期。


本文转载自: https://blog.csdn.net/mijichui2153/article/details/125009244
版权归原作者 焱齿 所有, 如有侵权,请联系我们删除。

“ELK之Elasticsearch常用curl命令”的评论:

还没有评论