ES(elasticsearch)常用的curl命令

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

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

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

#kibana指令,查看es集群下的索引
GET /_cat/indices?v

#如下为复制出来的对应curl指令
#注:对于使用者来说记得把ip:port更换成你当前的ip:port并且确保能访问通就可以了
curl -X GET "localhost:9200/_cat/indices?v"

#如下都是可以的
curl -X GET "10.101.203.15:9200/_cat/indices/?v"
curl -X GET '10.101.203.15:9200/_cat/indices/?v'
curl -X GET 'http://10.101.203.15:9200/_cat/indices?v'

用户名密码认证

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

#如用户名为qduser0,密码为 qidian)8el1}, 域名为 qd-es.infr.tce.io,则链接语句为

curl -X GET "http://qduser0:qidian)8el1%[email protected]:48888/_cat/indices/es_qidian_flow_online*?pretty"

注:密码中的特殊字符,如"}"要进行urlencode才行

二、常用curl命令的罗列

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

1、索引相关操作

1、查看索引
#列出所有索引
curl -X GET "10.101.203.15:9200/_cat/indices/?v"

#列出"es_qidian_flow"开头的索引
curl -X GET "10.101.203.15:9200/_cat/indices/es_qidian_flow*?v"
GET /_cat/indices/es_qidian_flow*?v


2、创建索引
#创建一个名为twitter的索引
curl -X PUT "10.101.203.15:9200/twitter?pretty" -H 'Content-Type: application/json' -d'
{
    "settings" : {
        "index" : {
            "number_of_shards" : 3, 
            "number_of_replicas" : 2 
        }
    }
}
'

#查看这个索引
curl -X GET "10.101.203.15:9200/_cat/indices/twitter*?v"


3、删除索引
#删除名称为twitter的索引
curl -X DELETE "10.101.203.15:9200/twitter?pretty"

DELETE /twitter





创建索引执行效果如下:

2、查询相关操作 —— _search

官网

#kibana DSL,查询flow_time为1650264117的文档
GET es_qidian_flow_oa_v2_202204/session/_search?routing=2852199840&pretty
{
  "query":{
       "term":{ "flow_time" : "1650264117"  }
  }
}

#curl实现同样功能
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'
{
    "query" : {
        "term" : { "flow_time" : "1650264117" }
    }
}
'


#kibana DSL,查询msg_content为"大家都觉得你不懂"的文档
GET es_qidian_flow_oa_v2_202204/session/_search?routing=2852199840&pretty
{
  "query":{
    "match":{
      "msg_content":"大家都觉得你不懂"
    }
  }
}


#curl语句实现同样的功能
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'
{
  "query":{
    "match":{
      "msg_content":"大家都觉得你不懂"
    }
  }
}
'

注:可以看到主题查询语句部分都是一样的,没有必要继续演示了。

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

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

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

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

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

#查询整个 es_qidian_flow_oa_v2_202205 索引的数据条数
GET es_qidian_flow_oa_v2_202205/_count

#curl版本 查询整个 es_qidian_flow_oa_v2_202205 索引的数据条数
curl -X GET "localhost:9200/es_qidian_flow_online_v2_202205/_count?pretty" -H 'Content-Type: application/json' -d'
{
}
'

#查询满足条件的数据条数(参数作为简单查询串)
GET /twitter/tweet/_count?q=user:kimchy

#查询满足条件的数据条数(DSL查询语句)
GET /twitter/tweet/_count
{
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}

#curl 查询满足条件的数据条数(参数作为简单查询串)
curl -X GET "localhost:9200/twitter/tweet/_count?q=user:kimchy&pretty"

#curl  查询满足条件的数据条数(DSL查询语句)
curl -X GET "localhost:9200/twitter/tweet/_count?pretty" -H 'Content-Type: application/json' -d'
{
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}
'

3、模板相关操作—— templates

Index Templates | Elasticsearch Guide [5.3] | Elastic

看起来也没什么可说的。

3.1 查看模板

#查看名称为es_qidian_flow_oa_template的模板
GET /_template/es_qidian_flow_oa_template

curl -X GET "10.101.203.15:9200/_template/es_qidian_flow_oa_template?pretty"


#查看所有模板
curl -X GET "10.101.203.15:9200/_template"

#查看以"es_qidian_flow_"开头的模板
curl -X GET "10.101.203.15:9200/_template/es_qidian_flow_*"

3.2 创建模板

#创建名称为 es_qidian_flow_online_template 的模板,如下
curl -X PUT "10.101.203.15:9200/_template/es_qidian_flow_online_template?pretty" -H 'Content-Type: application/json' -d'
{
    "order": 0,
    "template": "es_qidian_flow_online_v2_*",
    "settings": {
      "index": {
        "routing": {
          "allocation": {
            "include": {
              "temperature": "hot"
            }
          }
        },
        "refresh_interval": "120s",
        "number_of_shards": "1",
        "routing_partition_size": "1",
        "translog": {
          "sync_interval": "5s",
          "durability": "async"
        },
        "analysis": {
          "analyzer": {
            "qq_relation_ngram": {
              "tokenizer": "qq_relation_ngram_tokenizer"
            }
          },
          "tokenizer": {
            "qq_relation_ngram_tokenizer": {
              "token_chars": [
                "digit",
                "letter"
              ],
              "min_gram": "1",
              "type": "ngram",
              "max_gram": "1"
            }
          }
        },
        "number_of_replicas": "1"
      }
    },
    "mappings": {
      "session": {
        "_routing": {
          "required": true
        },
        "properties": {
          "session_id": {
            "type": "keyword"
          },
          "flow_time": {
            "type": "date",
            "format": "epoch_second||strict_date_optional_time"
          },
          "flow_type": {
            "type": "long"
          },
          "kfuin": {
            "type": "long"
          },
          "kfext": {
            "type": "long"
          },
          "robotid": {
            "type": "long"
          },
          "device_type": {
            "type": "long"
          },
          "msg_content": {
            "type": "text",
            "analyzer": "qq_relation_ngram",
            "fields": {
              "ik": {
                "type": "text",
                "analyzer": "ik_max_word"
              }
            }
          },
          "msg_direction": {
            "type": "long"
          },
          "customer_name": {
            "type": "keyword"
          },
          "account_type": {
            "type": "long"
          },
          "account": {
            "type": "keyword"
          },
          "msg_type": {
            "type": "long"
          },
          "qqpub": {
            "type": "long"
          },
          "relation_type": {
            "type": "long"
          },
          "account_aggr_field": {
            "type": "keyword"
          },
          "sort_type": {
            "type": "long"
          }
        }
      }
    },
    "aliases": {
      "es_qidian_flow_online": {}
    }
}
'

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

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

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

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

3.3 删除模板

DELETE /_template/template_1

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

4、插入文档

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

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

#DSL插入相关数据
POST es_qidian_flow_online_v2_202205/session?routing=2852165588
{
    "account": "wxfe15ad577e0382e5_ogPSI0vfhhXBRUbK7qtIQ9lTEZog",
    "account_aggr_field": "3_wxfe15ad577e0382e5_ogPSI0vfhhXBRUbK7qtIQ9lTEZog",
    "account_type": 3,
    "device_type": 1,
    "flow_time": 1652579483,
    "flow_type": 4,
    "kfext": 3008063101,
    "kfuin": 2852165588,
    "msg_content": "后台数据涉及游戏代码无法给您提供",
    "msg_direction": 1,
    "msg_type": 0,
    "qqpub": 0,
    "relation_type": 0,
    "retract_msg": 0,
    "robotid": 0,
    "session_id": "wx_2852165588_wxfe15ad577e0382e5_ogPSI0vfhhXBRUbK7qtIQ9lTEZog_1652576687947",
    "sort_type": 1
}


#curl指令插入相关数据
curl -POST "10.101.203.15:9200/es_qidian_flow_online_v2_202205/session?routing=2852165588" -H 'Content-Type: application/json' -d'
{
    "account": "wxfe15ad577e0382e5_ogPSI0vfhhXBRUbK7qtIQ9lTEZog",
    "account_aggr_field": "3_wxfe15ad577e0382e5_ogPSI0vfhhXBRUbK7qtIQ9lTEZog",
    "account_type": 3,
    "device_type": 1,
    "flow_time": 1652579483,
    "flow_type": 4,
    "kfext": 3008063101,
    "kfuin": 2852165588,
    "msg_content": "后台数据涉及游戏代码无法给您提供",
    "msg_direction": 1,
    "msg_type": 0,
    "qqpub": 0,
    "relation_type": 0,
    "retract_msg": 0,
    "robotid": 0,
    "session_id": "wx_2852165588_wxfe15ad577e0382e5_ogPSI0vfhhXBRUbK7qtIQ9lTEZog_1652576687947",
    "sort_type": 1
}
'

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

 

猜你喜欢

转载自blog.csdn.net/mijichui2153/article/details/125009244
今日推荐