栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

curl 操作 Elasticsearch

curl 操作 Elasticsearch

_cat系列命令

_cat系列命令提供了一系列查询elasticsearch集群状态的接口。你可以通过执行下面请求,获取所有_cat系列操作

 curl -XGET localhost:9200/_cat
=^.^=
/_cat/allocation
/_cat/shards
/_cat/shards/{index}
/_cat/master
/_cat/nodes
/_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/plugins
/_cat/fielddata
/_cat/fielddata/{fields}

可以通过在命令后面加 ?v的方式打印表头,如

 curl -XGET 127.0.0.1:9200/_cat/master?v

返回:

id                     host         ip           node
iBophuJTRdiZAZ7hWKWBeA 127.0.0.1 127.0.0.1 node-1
_cluster系列

1、查询设置集群状态

curl -XGET localhost:9200/_cluster/health?pretty=true
#pretty=true表示格式化输出
#level=indices 表示显示索引状态
#level=shards 表示显示分片信息

2、显示集群系统信息,包括CPU JVM等等

curl -XGET localhost:9200/_cluster/stats?pretty=true

3、集群的详细信息。包括节点、分片等。

 curl -XGET localhost:9200/_cluster/state?pretty=true

4、获取集群堆积的任务

 curl -XGET localhost:9200/_cluster/pending_tasks?pretty=true

5、修改集群配置,如

curl -XPUT localhost:9200/_cluster/settings -d '{
    "persistent" : {
        "discovery.zen.minimum_master_nodes" : 2
    }
}'
# transient 表示临时的,persistent表示永久的

6、对shard的手动控制,参考http://zhaoyanblog.com/archives/687.html

 curl -XPOST ‘localhost:9200/_cluster/reroute’ -d ‘xxxxxx’

7、关闭节点

#关闭指定192.168.1.1节点
 curl -XPOST ‘http://192.168.1.1:9200/_cluster/nodes/_local/_shutdown’
 curl -XPOST ‘http://localhost:9200/_cluster/nodes/192.168.1.1/_shutdown’
#关闭主节点
 curl -XPOST ‘http://localhost:9200/_cluster/nodes/_master/_shutdown’
#关闭整个集群
  curl -XPOST ‘http://localhost:9200/_shutdown?delay=10s’
#delay=10s表示延迟10秒关闭
  curl -XPOST ‘http://localhost:9200/_cluster/nodes/_shutdown’
  curl -XPOST ‘http://localhost:9200/_cluster/nodes/_all/_shutdown’
_nodes系列
curl -XGET ‘http://localhost:9200/_nodes/stats?pretty=true’
curl -XGET ‘http://localhost:9200/_nodes/192.168.1.2/stats?pretty=true’
curl -XGET ‘http://localhost:9200/_nodes/process’
curl -XGET ‘http://localhost:9200/_nodes/_all/process’
curl -XGET ‘http://localhost:9200/_nodes/192.168.1.2,192.168.1.3/jvm,process’
curl -XGET ‘http://localhost:9200/_nodes/192.168.1.2,192.168.1.3/info/jvm,process’
curl -XGET ‘http://localhost:9200/_nodes/192.168.1.2,192.168.1.3/_all
curl -XGET ‘http://localhost:9200/_nodes/hot_threads
索引操作

1.创建索引

curl  -H "Content-Type:application/json"  -XPUT  'http://127.0.0.1:9200/{index}' -d 
'{
    "settings": {
        "index": {
            "number_of_shards": "1",
            "number_of_replicas": "1"
        }
    },
    "mappings": {
        "{type}": {
            "dynamic": "strict",
            "properties": {
                "date": {
                    "type": "long"
                },
                "name": {
                    "type": "string",
                    "index": "not_analyzed"
                },
                "status": {
                    "type": "integer"
                },
                "type": {
                    "type": "integer"
                }
            }
        }
    }
}'

2、获取索引

curl -XGET ‘http://localhost:9200/{index}/{type}/{id}’

3、索引数据

curl -XPOST ‘http://localhost:9200/{index}/{type}/{id}’ -d'{“a”:”avalue”,”b”:”bvalue”}’

4、删除索引

curl -XDELETe ‘http://localhost:9200/{index}/{type}/{id}’

5、设置mapping

curl -XPUT http://localhost:9200/{index}/{type}/_mapping -d '{
  "{type}" : {
	"properties" : {
	  "date" : {
		"type" : "long"
	  },
	  "name" : {
		"type" : "string",
		"index" : "not_analyzed"
	  },
	  "status" : {
		"type" : "integer"
	  },
	  "type" : {
		"type" : "integer"
	  }
	}
  }
}'

6、查看索引setting

 curl -XGET 127.0.0.1:9200/{index}/{type}/settings

7.索引数据备份

#先创建一个相同的索引,可以设置分片为1(防止复制时间过长)
 curl -H "Content-Type:application/json" -XPOST 'http://localhost:9200/_reindex' -d '{"source":{"index":"tbl_stdmpool_store_inf"},"dest":{"index":"tbl_stdmpool_store_inf_copy"}}'

8.查询

curl -H "Content-Type:application/json" -XGET 'http://localhost:9200/{index}/{type}/_search' -d '{
    "query" : {
        "term" : { "user" : "kimchy" } //查所有 "match_all": {}
    },
	"sort" : [{ "age" : {"order" : "asc"}},{ "name" : "desc" } ],
	"from":0,
	"size":100
}'
curl -H "Content-Type:application/json" -XGET 'http://localhost:9200/{index}/{type}/_search' -d '{
    "filter": {"and":{"filters":[{"term":{"age":"123"}},{"term":{"name":"张三"}}]},
	"sort" : [{ "age" : {"order" : "asc"}},{ "name" : "desc" } ],
	"from":0,
	"size":100
}'

9.开启x-pack认证后查询

# 需要在请求是增加用户密码参数 -u user:password 或者(--user user:password)
curl -H "Content-Type:application/json" --user user:password -XGET 'http://localhost:9200/{index}/{type}/_search' -d '{
    "query" : {
        "term" : { "user" : "kimchy" } //查所有 "match_all": {}
    },
	"sort" : [{ "age" : {"order" : "asc"}},{ "name" : "desc" } ],
	"from":0,
	"size":100
}'
curl -H "Content-Type:application/json"  -u user:password -XGET 'http://localhost:9200/{index}/{type}/_search' -d '{
    "filter": {"and":{"filters":[{"term":{"age":"123"}},{"term":{"name":"张三"}}]},
	"sort" : [{ "age" : {"order" : "asc"}},{ "name" : "desc" } ],
	"from":0,
	"size":100
}'
#或者使用如下方式
curl  -XGET HTTP://user:password@127.0.0.1:9200/{index}/{type}/_search

10.给索引新增字段

curl  -H "Content-Type:application/json" -XPOST "http://127.0.0.1:9200/{index}/{type}/_mapping?pretty" -d '{
    "tbl_stdmpool_store_inf_dev": {
        "properties": {
            "falg": {
                "type": "keyword"
            }
        }
    }
}'

11.批量修改索引某个字段的值

curl -H "Content-Type:application/json"    -XPOST 'http://127.0.0.1:9200/{index}/{type}/_update_by_query?pretty' -d '{"query":{"bool":{"must_not":{"term":{"flag":"0"}}}},"size":10000,"sort":{"store_id":{"order":"asc"}},"script":{"inline":"ctx._source['''focus_cash''']='''0'''"}}'
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/707209.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号