elasticsearch java 基于 RestClientAPI 的增删改查

本文档展示了如何使用Java的RestClientAPI进行Elasticsearch的增删改查操作,包括GET、POST、PUT、HEAD和DELETE请求。通过`EsRestClient`类,实现了对索引、类型和ID的查询、创建、更新和删除。同时,还提供了服务类`MyDataElasticSearchDriverImpl`,用于获取表名、数据库名、字段以及统计数据。
摘要由CSDN通过智能技术生成
RestClient 类客户端 5种请求方式
public class EsRestClient {
   
   private static final String PUT = "PUT";
   private static final String POST = "POST";
   private static final String GET = "GET";
   private static final String HEAD = "HEAD";
   private static final String DELETE = "DELETE";
   
   public static RestClient getClient(){
      return getClient(CommonResource.cloud_es_dm_ip, CommonResource.cloud_es_dm_rest_port);
   }
   
   public static RestClient getClient(String ip, Integer port){
      return RestClient.builder(new HttpHost(ip,port)).setMaxRetryTimeoutMillis(6000).build();
   }

   public static String sendGet(String ip,Integer port,String index,String type,String query){
      RestClient restClient = getClient(ip,port);
      String  rs = null;
      try {
         HttpEntity entity = new NStringEntity(query, ContentType.APPLICATION_JSON);
         String endpoint = "/"+index+"/"+type+"/_search";
         if(StringUtils.isBlank(type)){
            endpoint = "/"+index+"/_search";
         }
         Response response = restClient.performRequest(GET, endpoint, Collections.singletonMap("pretty", "true"),entity);
         rs = EntityUtils.toString(response.getEntity());
      } catch (IOException e) {
         e.printStackTrace();
      }finally {
         close(restClient);
      }
      return rs;
   }
   
   public static String sendPost(List<String> indexs,List<String> types,String query){
      RestClient restClient = getClient();
      String  rs = null;
      try {
         String index = StringUtils.join(indexs, ",");
         String type = "/";
         if(Objects.nonNull(types) && !types.isEmpty()){
            type += StringUtils.join(types, ",")+"/";
         }
         HttpEntity entity = new NStringEntity(query, ContentType.APPLICATION_JSON);
         String endpoint = "/"+index+type+"_search";
         Response response = restClient.performRequest(POST, endpoint, Collections.singletonMap("pretty", "true"),entity);
         rs = EntityUtils.toString(response.getEntity());
      } catch (IOException e) {
         e.printStackTrace();
      }finally {
         close(restClient);
      }
      return rs;
   }
   
   public static String sendPut(String ip, int port,String index,String type,String id,String data){
      RestClient restClient = getClient(ip,port);
      String  rs = null;
      try {
         HttpEntity entity = new NStringEntity(data, ContentType.APPLICATION_JSON);
         String requestType = POST;
         String endpoint = "/"+index+"/"+type;
         if(StringUtils.isNoneBlank(id)){
            requestType = PUT;
            endpoint = "/"+index+"/"+type+"/"+id;
         }
         Response response = restClient.performRequest(requestType, endpoint, Collections.singletonMap("pretty", "true"),entity);
         rs = EntityUtils.toString(response.getEntity());
      } catch (IOException e) {
         e.printStackTrace();
      }finally {
         close(restClient);
      }
      return rs;
   }
   
   public static String sendDelete(RestClient restClient,String index,String type){
      return sendDelete(restClient,index,type,null);
   }
   
   public static String sendDelete(RestClient restClient,String index,String type,String id){

      String rs = null;
      try {
         String endpoint = "/"+index+"/"+type+"/"+id;
         if(StringUtils.isBlank(id)){
            endpoint = "/"+index+"/"+type;
         }else if(StringUtils.isBlank(type)){
            endpoint = "/"+index;
         }
         Response response = restClient.performRequest(DELETE, endpoint);
         rs = EntityUtils.toString(response.getEntity());
      } catch (IOException e) {
         e.printStackTrace();
      }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值