http4client rest buik elasticsearch


maven

	  <dependency>
	    <groupId>org.apache.httpcomponents</groupId>
	    <artifactId>httpclient</artifactId>
	    <version>4.5.2</version>
	</dependency>
	
	<dependency>
	    <groupId>org.apache.httpcomponents</groupId>
	    <artifactId>httpcore</artifactId>
	    <version>4.4</version>
	</dependency>
	
	<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpmime</artifactId>
    <version>4.4</version>
</dependency>


bulk 

 /**
   * { "delete" : { "_index" : "test", "_type" : "type1", "_id" : "2" } }
   * @return
   */
  private static String buildDelete(JSONObject json,String index,String type,String id){
	  StringBuffer sb = new StringBuffer();
	  String str="{ \"delete\" : { \"_index\" : \""+index+"\", \"_type\" : \""+type+"\", \"_id\" : \""+id+"\" } }";
	  sb.append(str).append("\n");
	  System.out.println("delete "+sb.toString());
	  return sb.toString();
  }
  
  /**
   * { "update" : {"_id" : "1", "_type" : "type1", "_index" : "test"} }
   * { "doc" : {"field2" : "value2"} }
   * @return
   */
  	private static String buildUpdate(JSONObject json,String index,String type,String id){
  		StringBuffer sb = new StringBuffer();
  		String str="{ \"update\" : { \"_index\" : \""+index+"\", \"_type\" : \""+type+"\", \"_id\" : \""+id+"\" } }";
  		
  		sb.append(str).append("\n");
  		sb.append("{ \"doc\" : ").append(json).append("}").append("\n");
  		
  		System.out.println("update: "+ sb.toString());
		return sb.toString();
  	}
  	private static String buildInsert(JSONObject json,String index,String type,String id){
  		StringBuffer sb = new StringBuffer();
  		String str="{ \"index\" : { \"_index\" : \""+index+"\", \"_type\" : \""+type+"\", \"_id\" : \""+id+"\" } }";
  		sb.append(str).append("\n");
  		sb.append(json).append("\n");
  		System.out.println("insert: "+sb.toString());
		return sb.toString();
  	}
  	
  	
	public String postJson(String httpUrl, String json) {
		HttpPost httpPost = new HttpPost(httpUrl);// 创建httpPost
		StringEntity stringEntity = new StringEntity(json, "utf-8");// 解决中文乱码问题
		stringEntity.setContentEncoding("UTF-8");
		stringEntity.setContentType("application/json");
		httpPost.setEntity(stringEntity);
		return sendHttpPost(httpPost);
	}
 private String sendHttpPost(HttpPost httpPost) {  
      CloseableHttpClient httpClient = null;  
      CloseableHttpResponse response = null;  
      HttpEntity entity = null;  
      String responseContent = null;  
      try {  
          httpClient = HttpClients.createDefault();  
          response = httpClient.execute(httpPost);  
          entity = response.getEntity();  
          responseContent = EntityUtils.toString(entity, "UTF-8");  
      } catch (Exception e) {  
          e.printStackTrace();  
      } finally {  
          try {  
              if (response != null) {  
                  response.close();  
              }  
              if (httpClient != null) {  
                  httpClient.close();  
              }  
          } catch (IOException e) {  
              e.printStackTrace();  
          }  
      }  
      return responseContent;  
  }  





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值