java api

ELK/elasticsearch2019. 1. 25. 18:44

package com.forest.test;


import java.io.IOException;

import java.net.InetAddress;

import java.net.UnknownHostException;

import java.nio.charset.Charset;

import java.util.HashMap;

import java.util.Map;

import java.util.concurrent.TimeUnit;


import org.apache.http.HttpHost;

import org.apache.http.ParseException;

import org.apache.http.RequestLine;

import org.apache.http.util.EncodingUtils;

import org.apache.http.util.EntityUtils;

import org.elasticsearch.action.search.SearchRequest;

import org.elasticsearch.action.search.SearchRequestBuilder;

import org.elasticsearch.action.search.SearchResponse;

import org.elasticsearch.client.ElasticsearchClient;

import org.elasticsearch.client.Request;

import org.elasticsearch.client.RequestOptions;

import org.elasticsearch.client.Response;

import org.elasticsearch.client.ResponseListener;

import org.elasticsearch.client.RestClient;

import org.elasticsearch.client.RestHighLevelClient;

import org.elasticsearch.client.transport.TransportClient;

import org.elasticsearch.common.settings.Settings;

import org.elasticsearch.common.transport.TransportAddress;

import org.elasticsearch.common.unit.TimeValue;

import org.elasticsearch.index.query.QueryBuilders;

import org.elasticsearch.search.aggregations.AggregationBuilders;

import org.elasticsearch.search.aggregations.metrics.min.MinAggregationBuilder;

import org.elasticsearch.search.builder.SearchSourceBuilder;

//import org.elasticsearch.index.mapper.ObjectMapper;

import org.elasticsearch.transport.client.PreBuiltTransportClient;

import org.json.simple.JSONArray;

import org.json.simple.JSONObject;

import org.json.simple.parser.JSONParser;


import com.fasterxml.jackson.core.type.TypeReference;

import com.fasterxml.jackson.databind.ObjectMapper;

import com.fasterxml.jackson.databind.ObjectMapper.*;

/*

 * java Low Level REST Client 

 * 

 */

public class LowController {

public Response response = null; // ���� 

public Request request = null; // ��û

public RestClient restClient = null;

public RequestLine reqLine = null; // 

public String responseBody = null;

public int statusCode = 0; // ���� �ڵ�

public RestHighLevelClient client = null;

public void RestClientSetting() {

restClient = RestClient.builder(new HttpHost("192.168.2.10", 9200, "http")).build();

}

public void RequestSend(String paramIndex, String command) {

request = new Request("GET", paramIndex + "/" + command);

try {

response = restClient.performRequest(request);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

// search 

public void doAggr() {

RestHighLevelClient client = new RestHighLevelClient(

        RestClient.builder(

                new HttpHost("192.168.0.106", 9200, "http"),

                new HttpHost("192.168.0.106", 9201, "http")));

// 갯수 count 

SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); 

sourceBuilder.query(QueryBuilders.termQuery("data", "120")); 

sourceBuilder.from(0); 

sourceBuilder.size(5); 

sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS)); 

SearchRequest searchRequest = new SearchRequest();

searchRequest.indices("today190108");

searchRequest.source(sourceBuilder);

try {

SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

System.out.println(searchResponse);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public String ReadResponse() {

String retValue = "";

JSONArray jsonArr = null;

JSONParser parser = new JSONParser();

Object obj = null;

// Reading responses

reqLine = response.getRequestLine();

try {

responseBody = EntityUtils.toString(response.getEntity());

} catch (ParseException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

obj = parser.parse(responseBody);

JSONObject jsonObj = (JSONObject) obj;

JSONObject jsonObjTwo = (JSONObject)jsonObj.get("hits");

JSONArray jsonArray = (JSONArray)jsonObjTwo.get("hits");

for (int i = 0; i < jsonArray.size(); i++) {

JSONObject returnSubject = (JSONObject)jsonArray.get(i);

System.out.println(returnSubject.get("_source").toString());

retValue += returnSubject.get("_source").toString();

retValue += "\n";

}

} catch (org.json.simple.parser.ParseException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return retValue;

}

}



'ELK > elasticsearch' 카테고리의 다른 글

java api elasticsearch aggregation  (0) 2019.01.28
java api2  (0) 2019.01.25
java api _search  (0) 2019.01.25
java elasticsearch api 인덱스 생성  (0) 2019.01.24
Java _ elasticsearch  (0) 2019.01.15