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();
}