http4client rest buik elasticsearch
2017-07-20 11:29
253 查看
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);
}
<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; }
相关文章推荐
- 在Kubernetes上搭建EFK(Fluentd+Elasticsearch+Kibana)
- CentOS7中文语言乱码以及Tomcat日志中文乱码问题解决
- Nodejs连接12种数据库例子集合
- Oracle批量查询、删除、更新使用BULK COLLECT提高效率
- 巧用mysql提示符prompt清晰管理数据库的方法
- 两大步骤教您开启MySQL 数据库远程登陆帐号的方法
- 向大家推荐一个收集整理正则表达式的网站
- 最严谨的校验email地址的正则表达式及各种语言对应版
- phpmyadmin 4+ 访问慢的解决方法
- linux系统下实现mysql热备份详细步骤(mysql主从复制)
- Mysql高效分页详解
- CentOS 5.5下安装MySQL 5.5全过程分享
- MySQL复制的概述、安装、故障、技巧、工具(火丁分享)
- MySQL中删除重复数据的简单方法
- MySQL中查询、删除重复记录的方法大全
- PHP中的print_r 与 var_dump 输出数组
- MySQL5.5.21安装配置教程(win7)
- 详解centos7虚拟机安装elasticsearch5.0.x-安装篇
- Elasticsearch.Net使用教程 MVC4图书管理系统(2)
- Elasticsearch.Net使用入门教程(1)