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 indexs,List 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();
}finally {
close(restClient);
}
return rs;
}
public static boolean sendHead(RestClient restClient,String index,String type){
int code = 200;
try {
String endpoint = "/"+index+"/"+type;
// String endpoint = "/"+index+"/_mapping/"+type;//5.x
if(StringUtils.isBlank(type)){
endpoint = "/"+index;
}
Response response = restClient.performRequest(HEAD, endpoint);//200存在,404不存在
code = response.getStatusLine().getStatusCode();
} catch (IOException e) {
e.printStackTrace();
}finally {
close(restClient);
}
return code == 200?true:false;
}
public static void close(RestClient restClient){
if(Objects.nonNull(restClient)){
try {
restClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
RestClient service类
@Service
public class MyDataElasticSearchDriverImpl implements IMyDataDriver {
private static final Logger log = Logger.getLogger(MyDataElasticSearchDriverImpl.class);
@Override
public List getTables(String ip, Integer port, String username,String password, String database) {
List tables = Lists.newArrayList();
RestClient restClient = EsRestClient.getClient(ip,port);
Map requestData = Maps.newHashMap();
requestData.put("from", 1);
requestData.put("size", 100);
HttpEntity entity = new NStringEntity(JSON.toJSONString(requestData), ContentType.APPLICATION_JSON);
Response response;
JSONObject jsonObj = null;
try {
response = restClient.performRequest("GET", "/"+database, Collections.singletonMap("pretty", "true"),entity);
jsonObj = JSON.parseObject(EntityUtils.toString(response.getEntity()));
} catch (IOException e) {
e.printStackTrace();
}
@SuppressWarnings("unchecked")
Map mappings = (Map)(jsonObj.getJSONObject(database).get("mappings"));
for (Map.Entry m : mappings.entrySet()) {
tables.add(m.getKey());
}
return tables;
}
@Override
public List getDatabases(String ip, Integer port, String username,String password) {
List dataBases = Lists.newArrayList();
RestClient restClient = EsRestClient.getClient(ip, port);
try {
Response response = restClient.performRequest("GET", "/_cat/indices?v", Collections.singletonMap("pretty", "true"));
String rs = EntityUtils.toString(response.getEntity());
BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(rs.getBytes(Charset.forName("utf8"))), Charset.forName("utf8")));
String line = null;
int index = 0;
while((line = br.readLine()) != null){
if(index > 0){
List indices = Lists.newLinkedList();
String[] data = line.split(" ");
int a=0;
for (String s : data) {
if(!"".equals(s)){
a++;
}
if(a==3) {
dataBases.add(s);
a=0;
break;
}
}
}
index++;
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
EsRestClient.close(restClient);
return dataBases;
}
@Override
public List getFields(String ip, Integer port, String username,String password, String database, String table) {
List fields = Lists.newArrayList();
RestClient restClient = EsRestClient.getClient(ip, port);
try {
Response response = restClient.performRequest("GET", "/"+database+"/_mapping/"+table, Collections.singletonMap("pretty", "true"));
String rs = EntityUtils.toString(response.getEntity());
JSONObject jsonObj = JSON.parseObject(rs);
@SuppressWarnings("unchecked")
Map properties = (Map)(jsonObj.getJSONObject(database).getJSONObject("mappings").getJSONObject(table).get("properties"));
for (Map.Entry m : properties.entrySet()) {
fields.add(m.getKey());
}
} catch (IOException e) {
e.printStackTrace();
}
EsRestClient.close(restClient);
return fields;
}
@Override
public RestData
测试
public Boolean addMapping2(String index, String type,List colList) {
String es_ip = CommonResource.cloud_es_dm_ip;
int es_port = CommonResource.cloud_es_dm_tcp_port;
try{
//this.myDataElasticSearchDriver.createTable(es_ip, CommonResource.cloud_es_dm_rest_port, index, type, colList);
List databases=new ArrayList<>();
databases.add("cars");
List tables=new ArrayList<>();
tables.add("transactions");
List fields=new ArrayList<>();
fields.add("title");
fields.add("cooperationWay");
fields.add("type");
String query="{\"match\":{\"title\":\"技\"}}";
String aggs="{\"colors\":{\"terms\":{ \"field\": \"color\"}}}";
Integer returnType=0;
Integer pageSize=0;
int start=0;
int offset=100;
//RestData> mapRestData = this.myDataElasticSearchDriver.searchData(es_ip, CommonResource.cloud_es_dm_rest_port, index, type, databases, tables,fields,query,sort,start,offset);
StatDataBean statDataBean = this.myDataElasticSearchDriver.statDimension1(es_ip, CommonResource.cloud_es_dm_rest_port, index, type, databases, tables, aggs, "", returnType, pageSize);
for(String s :tables)
System.out.println(s);
return true;
}catch (Exception e){
return false;
}
}