es http请求方式
es 可以通过客户端api集成操作,也可以发起 http请求对集群进行操作
(1)curl发起请求
curl 使用curl -u "用户名:密码" -XPOST -H '' -d 'Content-Type: application/json' "http://es....." 可以命令行执行
(2)使用RestTemplate发起http请求
String url = "http://elastic:your_password@your-es-host:9200/_tasks/..."; RestTemplate restTemplate = new RestTemplate();但实际测试中,这种url指定用户名密码不行呢 不起作用...换方法(3)
(3) RestTemplate basicAuth
HttpHeaders head = new HttpHeaders(); if (!StringUtils.isEmpty(password) && !StringUtils.isEmpty(username)){ head.setBasicAuth(username,password); } // // 方式2. 构建 Basic Auth 字符串 // String auth = username + ":" + password; // String authHeader = "Basic " + Base64.encode(auth.getBytes(StandardCharsets.UTF_8)); // // HttpHeaders head = new HttpHeaders(); // head.set("Authorization", authHeader); HttpEntity<String> entity = new HttpEntity<>(head); ResponseEntity<String> response = template.exchange( url, HttpMethod.GET, entity, String.class );这种可以执行成功
es 监控类命令
(1)查询指定索引的分片数
curl -u "用户名:密码" -XGET "http://eshost:9200/_cat/indices/myindex*?v&pretty"
(2)查看myindex开头的索引列表
curl -u "用户名:密码" -XGET "http://eshost:9200/_cat/indices/myindex*?v&pretty"
(3)集群健康
curl -u "用户名:密码" -XGET "http://eshost:9200/_cluster/health"
(4) 线程池状态(拒绝率不能过高)
curl -u "用户名:密码" -XGET "http://eshost:9200/_nodes/stats/thread_pool?filter_path=**.thread_pool.rejected"
(5)磁盘使用率(不能超过85%)
curl -u "用户名:密码" -XGET "http://eshost:9200/_cat/allocation?v&s=disk.percent"
es 删除数据命令
(1)按条件删除指定索引的数据
curl -u "用户名:密码" -XPOST "http://eshost:9200/myindex/_delete_by_query?wait_for_completion=false&slices=auto&refresh=false" -H 'Content-Type: application/json' -d'{ "query": { "bool": { "must": [ { "term": { "userId": { "value": "" } } } ] } } }'
注意wait_for_completion=false说明异步执行(1w的数据量秒删),结果会立即返回一个任务id,es集群会根据任务大小再拆分出小任务
300多万数据消耗大概1分钟
3亿多万条数据大概3小时(当然和脚本有关,脚本里有每删除1天数据等待几秒让集群恢复)
(2)根据任务id查询任务进度
curl -u "用户名:密码" -XGET "http://hosts:9200/_tasks/任务id"
(3)查询es所有的删除任务(不展示明细)
curl -u "用户名:密码" -XGET "http://eshost:9200/_tasks?actions=*/delete/byquery&detailed=false"
(4)任务取消
curl -u "用户名:密码" -XPOST "http://eshost:9200/_tasks/nC10NklZQiyi9Lohtj6a-g:45627061391/_cancel"
(5)查询es索引的文档数,确认是否数据都删除
curl -u "用户名:密码" -XPOST "http://eshosts:9200/myindex/_count" -H 'Content-Type: application/json' -d'{ "query": { "bool": { "must": [ { "term": { "userId": { "value": "" } } } ] } } }'