Search in sources :

Example 1 with ESStringResponseHandler

use of org.frameworkset.elasticsearch.handler.ESStringResponseHandler in project bboss-elastic by bbossgroups.

the class ESTest method testConfig.

@Test
public void testConfig() throws ParseException {
    ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil("estrace/ESTracesMapper.xml");
    TraceExtraCriteria traceExtraCriteria = new TraceExtraCriteria();
    traceExtraCriteria.setApplication("testweb1");
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    traceExtraCriteria.setStartTime(dateFormat.parse("2017-09-02 00:00:00").getTime());
    traceExtraCriteria.setEndTime(dateFormat.parse("2017-09-10 00:00:00").getTime());
    String data = clientUtil.executeRequest("trace-*/_search", "queryPeriodsTopN", traceExtraCriteria, new ESStringResponseHandler());
    System.out.println("------------------------------");
    System.out.println(data);
    System.out.println("------------------------------");
    Map<String, Object> response = clientUtil.executeRequest("trace-*/_search", "queryPeriodsTopN", traceExtraCriteria, new ESMapResponseHandler());
    if (response.containsKey("error")) {
        return;
    }
}
Also used : ESMapResponseHandler(org.frameworkset.elasticsearch.handler.ESMapResponseHandler) FastDateFormat(org.frameworkset.util.FastDateFormat) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) ClientInterface(org.frameworkset.elasticsearch.client.ClientInterface) ESStringResponseHandler(org.frameworkset.elasticsearch.handler.ESStringResponseHandler) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.Test)

Example 2 with ESStringResponseHandler

use of org.frameworkset.elasticsearch.handler.ESStringResponseHandler in project bboss-elastic by bbossgroups.

the class ESTest method testSearh.

@Test
public void testSearh() throws ParseException {
    ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil("org/frameworkset/elasticsearch/ESTracesMapper.xml");
    TraceExtraCriteria traceExtraCriteria = new TraceExtraCriteria();
    traceExtraCriteria.setApplication("testweb1");
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    traceExtraCriteria.setStartTime(dateFormat.parse("2017-09-02 00:00:00").getTime());
    traceExtraCriteria.setEndTime(dateFormat.parse("2017-09-10 00:00:00").getTime());
    String data = clientUtil.executeRequest("trace-*/_search", "queryPeriodsTopN", traceExtraCriteria, new ESStringResponseHandler());
    System.out.println("------------------------------");
    System.out.println(data);
    System.out.println("------------------------------");
    Map<String, Object> response = clientUtil.executeRequest("trace-*/_search", "queryPeriodsTopN", traceExtraCriteria, new ESMapResponseHandler());
    if (response.containsKey("error")) {
        return;
    }
}
Also used : ESMapResponseHandler(org.frameworkset.elasticsearch.handler.ESMapResponseHandler) FastDateFormat(org.frameworkset.util.FastDateFormat) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) ClientInterface(org.frameworkset.elasticsearch.client.ClientInterface) ESStringResponseHandler(org.frameworkset.elasticsearch.handler.ESStringResponseHandler) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.Test)

Example 3 with ESStringResponseHandler

use of org.frameworkset.elasticsearch.handler.ESStringResponseHandler in project bboss-elastic by bbossgroups.

the class ESTest method testSearhHits.

@Test
public void testSearhHits() throws ParseException {
    ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil("org/frameworkset/elasticsearch/ESTracesMapper.xml");
    TraceExtraCriteria traceExtraCriteria = new TraceExtraCriteria();
    traceExtraCriteria.setApplication("testweb1");
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    traceExtraCriteria.setStartTime(dateFormat.parse("2017-09-02 00:00:00").getTime());
    traceExtraCriteria.setEndTime(dateFormat.parse("2017-09-10 00:00:00").getTime());
    String data = clientUtil.executeRequest("trace-*/_search", "queryPeriodsTopN", traceExtraCriteria, new ESStringResponseHandler());
    System.out.println("------------------------------");
    System.out.println(data);
    System.out.println("------------------------------");
    Map<String, Object> response = clientUtil.executeRequest("trace-*/_search", "queryPeriodsTopN", traceExtraCriteria, new ESMapResponseHandler());
    if (response.containsKey("error")) {
        return;
    }
}
Also used : ESMapResponseHandler(org.frameworkset.elasticsearch.handler.ESMapResponseHandler) FastDateFormat(org.frameworkset.util.FastDateFormat) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) ClientInterface(org.frameworkset.elasticsearch.client.ClientInterface) ESStringResponseHandler(org.frameworkset.elasticsearch.handler.ESStringResponseHandler) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.Test)

Example 4 with ESStringResponseHandler

use of org.frameworkset.elasticsearch.handler.ESStringResponseHandler in project bboss-elastic by bbossgroups.

the class ElasticSearchRestClient method execute.

public String execute(final String entity, String options) throws ElasticSearchException {
    String endpoint = BULK_ENDPOINT;
    if (options != null) {
        endpoint = new StringBuilder().append(endpoint).append("?").append(options).toString();
    }
    final ESStringResponseHandler responseHandler = new ESStringResponseHandler();
    return _executeHttp(endpoint, responseHandler, new ExecuteRequest() {

        @Override
        public Object execute(ESAddress host, String url, int triesCount) throws Exception {
            Object response = null;
            if (showTemplate) {
                if (logger.isInfoEnabled()) {
                    logger.info("ElasticSearch http request endpoint:{},retry:{},request body:\n{}", url, triesCount, entity);
                }
            }
            response = restSeachExecutor.execute(url, entity, responseHandler);
            return response;
        }
    });
/**
 *		int triesCount = 0;
 *		String response = null;
 *		Throwable e = null;
 *		ESAddress host = null;
 *		String url = null;
 *
 *		while (true) {
 *
 *			try {
 *				host = serversList.get(failAllContinue || !this.healthCheckEnabled());
 *				url = new StringBuilder().append(host.getAddress()).append( "/" ).append( endpoint).toString();
 *
 *//				response = HttpRequestUtil.sendJsonBody(httpPool,entity, url, this.headers);
 *				if(this.showTemplate ){
 *					if(logger.isInfoEnabled()) {
 *						logger.info("ElasticSearch http request endpoint:{},retry:{},request body:\n{}",url,triesCount,entity);
 *					}
 *
 *				}
 *				ESStringResponseHandler responseHandler = new ESStringResponseHandler();
 *				response = restSeachExecutor.execute(url,entity,responseHandler);
 *				e = getException(  responseHandler );
 *				break;
 *			}
 *			catch (HttpHostConnectException ex) {
 *				host.setStatus(1);
 *				e = new NoServerElasticSearchException(ex);
 *				if (triesCount < serversList.size() - 1) {//失败尝试下一个地址
 *					triesCount++;
 *					continue;
 *				} else {
 *					break;
 *				}
 *
 *            } catch (UnknownHostException ex) {
 *            	host.setStatus(1);
 *				e = new NoServerElasticSearchException(ex);
 *            	if (triesCount < serversList.size() - 1) {//失败尝试下一个地址
 *					triesCount++;
 *					continue;
 *				} else {
 *					break;
 *				}
 *
 *            }
 *			catch (NoRouteToHostException ex) {
 *				host.setStatus(1);
 *				e = new NoServerElasticSearchException(ex);
 *				if (triesCount < serversList.size() - 1) {//失败尝试下一个地址
 *					triesCount++;
 *					continue;
 *				} else {
 *					break;
 *				}
 *
 *			}
 *			catch (NoHttpResponseException ex) {
 *				host.setStatus(1);
 *				e = new NoServerElasticSearchException(ex);
 *				if (triesCount < serversList.size() - 1) {//失败尝试下一个地址
 *					triesCount++;
 *					continue;
 *				} else {
 *					break;
 *				}
 *
 *			}
 *			catch (ConnectionPoolTimeoutException ex){//连接池获取connection超时,直接抛出
 *
 *				e = handleConnectionPoolTimeOutException( ex);
 *				break;
 *			}
 *            catch (ConnectTimeoutException connectTimeoutException){
 *				host.setStatus(1);
 *				e = handleConnectionTimeOutException(connectTimeoutException);
 *				if (triesCount < serversList.size() - 1) {//失败尝试下一个地址
 *					triesCount++;
 *					continue;
 *				} else {
 *					break;
 *				}
 *			}
 *//			catch (IOException ex) {
 *//				host.setStatus(1);
 *//				if (triesCount < serversList.size()) {//失败尝试下一个地址
 *//					triesCount++;
 *//					e = ex;
 *//					continue;
 *//				} else {
 *//					e = ex;
 *//					break;
 *//				}
 *//
 *//            }
 *			catch (SocketTimeoutException ex) {
 *				e = handleSocketTimeoutException( ex);
 *				break;
 *			}
 *			catch (NoServerElasticSearchException ex){
 *				e = ex;
 *
 *				break;
 *			}
 *			catch (ClientProtocolException ex){
 *				host.setStatus(1);
 *				e = ex;
 *				if (triesCount < serversList.size() - 1) {//失败尝试下一个地址
 *					triesCount++;
 *					continue;
 *				} else {
 *					break;
 *				}
 *				//throw new ElasticSearchException(new StringBuilder().append("Request[").append(url).append("] handle failed: must use http/https protocol port such as 9200,do not use transport such as 9300.").toString(),ex);
 *			}
 *			catch (ElasticSearchException ex) {
 *				e = ex;
 *				break;
 *			}
 *			catch (Exception ex) {
 *				e = ex;
 *				break;
 *			}
 *			catch (Throwable ex) {
 *				e = ex;
 *				break;
 *			}
 *
 *		}
 *		if (e != null){
 *			if(e instanceof ElasticSearchException)
 *				throw (ElasticSearchException)e;
 *			throw new ElasticSearchException(e);
 *		}
 *		return response;
 */
}
Also used : ESStringResponseHandler(org.frameworkset.elasticsearch.handler.ESStringResponseHandler) ClientProtocolException(org.apache.http.client.ClientProtocolException) ConnectionPoolTimeoutException(org.apache.http.conn.ConnectionPoolTimeoutException) NoHttpResponseException(org.apache.http.NoHttpResponseException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException) SocketTimeoutException(java.net.SocketTimeoutException) NoRouteToHostException(java.net.NoRouteToHostException)

Example 5 with ESStringResponseHandler

use of org.frameworkset.elasticsearch.handler.ESStringResponseHandler in project bboss-elastic by bbossgroups.

the class ElasticSearchRestClient method executeRequest.

public String executeRequest(String path, final String entity) throws ElasticSearchException {
    final ESStringResponseHandler responseHandler = new ESStringResponseHandler();
    return _executeHttp(path, responseHandler, new ExecuteRequest() {

        @Override
        public Object execute(ESAddress host, String url, int triesCount) throws Exception {
            Object response = null;
            if (showTemplate) {
                if (logger.isInfoEnabled()) {
                    if (entity != null)
                        logger.info("ElasticSearch http request endpoint:{},retry:{},request body:\n{}", url, triesCount, entity);
                    else
                        logger.info("ElasticSearch http request endpoint:{},retry:{}", url, triesCount);
                }
            }
            response = restSeachExecutor.executeSimpleRequest(url, entity, responseHandler);
            return response;
        }
    });
/**
 *		int triesCount = 0;
 *		String response = null;
 *		Throwable e = null;
 *
 *		ESAddress host = null;
 *		String url = null;
 *		while (true) {
 *
 *			try {
 *				host = serversList.get(!healthCheckEnabled());
 *				url =  getPath(host.getAddress(),path);
 *//				if (entity == null)
 *//					response = HttpRequestUtil.httpPostforString(url, null, this.headers);
 *//				else
 *//					response = HttpRequestUtil.sendJsonBody(entity, url, this.headers);
 *				if(this.showTemplate ){
 *					if(logger.isInfoEnabled()) {
 *						if(entity != null)
 *							logger.info("ElasticSearch http request endpoint:{},retry:{},request body:\n{}",url,triesCount,entity);
 *						else
 *							logger.info("ElasticSearch http request endpoint:{},retry:{}",url,triesCount);
 *					}
 *				}
 *				ESStringResponseHandler responseHandler = new ESStringResponseHandler();
 *				response = this.restSeachExecutor.executeSimpleRequest(url,entity,responseHandler);
 *				e = getException(  responseHandler );
 *				break;
 *			}
 *
 *			catch (HttpHostConnectException ex) {
 *				host.setStatus(1);
 *				e = new NoServerElasticSearchException(ex);
 *				if (triesCount < serversList.size() - 1) {//失败尝试下一个地址
 *					triesCount++;
 *					continue;
 *				} else {
 *					break;
 *				}
 *
 *            } catch (UnknownHostException ex) {
 *            	host.setStatus(1);
 *				e = new NoServerElasticSearchException(ex);
 *            	if (triesCount < serversList.size() - 1) {//失败尝试下一个地址
 *					triesCount++;
 *					continue;
 *				} else {
 *					break;
 *				}
 *
 *            }
 *			catch (NoRouteToHostException ex) {
 *				host.setStatus(1);
 *				e = new NoServerElasticSearchException(ex);
 *				if (triesCount < serversList.size() - 1) {//失败尝试下一个地址
 *					triesCount++;
 *					continue;
 *				} else {
 *					break;
 *				}
 *
 *			}
 *
 *			catch (NoHttpResponseException ex) {
 *				host.setStatus(1);
 *				e = new NoServerElasticSearchException(ex);
 *				if (triesCount < serversList.size() - 1) {//失败尝试下一个地址
 *					triesCount++;
 *					continue;
 *				} else {
 *					break;
 *				}
 *
 *			}
 *			catch (ConnectionPoolTimeoutException ex){//连接池获取connection超时,直接抛出
 *
 *				e = handleConnectionPoolTimeOutException( ex);
 *				break;
 *			}
 *			catch (ConnectTimeoutException connectTimeoutException){
 *				host.setStatus(1);
 *				e = handleConnectionTimeOutException(connectTimeoutException);
 *				if (triesCount < serversList.size() - 1) {//失败尝试下一个地址
 *					triesCount++;
 *					continue;
 *				} else {
 *					break;
 *				}
 *			}
 *//			catch (IOException ex) {
 *//				host.setStatus(1);
 *//				if (triesCount < serversList.size()) {//失败尝试下一个地址
 *//					triesCount++;
 *//					e = ex;
 *//					continue;
 *//				} else {
 *//					e = ex;
 *//					break;
 *//				}
 *//
 *//            }
 *			catch (SocketTimeoutException ex) {
 *				e = handleSocketTimeoutException( ex);
 *				break;
 *			}
 *			catch (NoServerElasticSearchException ex){
 *					e = ex;
 *				break;
 *			}
 *			catch (ClientProtocolException ex){
 *				host.setStatus(1);
 *				e = ex;
 *				if (triesCount < serversList.size() - 1) {//失败尝试下一个地址
 *					triesCount++;
 *					continue;
 *				} else {
 *					break;
 *				}
 *//				throw new ElasticSearchException(new StringBuilder().append("Request[").append(url).append("] handle failed: must use http/https protocol port such as 9200,do not use transport such as 9300.").toString(),ex);
 *			}
 *			catch (ElasticSearchException ex) {
 *				throw ex;
 *			}
 *
 *			catch (Exception ex) {
 *				e = ex;
 *				break;
 *			}
 *			catch (Throwable ex) {
 *				e = ex;
 *				break;
 *			}
 *		}
 *		if (e != null){
 *			throw new ElasticSearchException(e);
 *		}
 *		return response;
 */
}
Also used : ESStringResponseHandler(org.frameworkset.elasticsearch.handler.ESStringResponseHandler) ClientProtocolException(org.apache.http.client.ClientProtocolException) ConnectionPoolTimeoutException(org.apache.http.conn.ConnectionPoolTimeoutException) NoHttpResponseException(org.apache.http.NoHttpResponseException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException) SocketTimeoutException(java.net.SocketTimeoutException) NoRouteToHostException(java.net.NoRouteToHostException)

Aggregations

ESStringResponseHandler (org.frameworkset.elasticsearch.handler.ESStringResponseHandler)5 DateFormat (java.text.DateFormat)3 SimpleDateFormat (java.text.SimpleDateFormat)3 ClientInterface (org.frameworkset.elasticsearch.client.ClientInterface)3 ESMapResponseHandler (org.frameworkset.elasticsearch.handler.ESMapResponseHandler)3 FastDateFormat (org.frameworkset.util.FastDateFormat)3 Test (org.junit.Test)3 IOException (java.io.IOException)2 NoRouteToHostException (java.net.NoRouteToHostException)2 SocketTimeoutException (java.net.SocketTimeoutException)2 UnknownHostException (java.net.UnknownHostException)2 NoHttpResponseException (org.apache.http.NoHttpResponseException)2 ClientProtocolException (org.apache.http.client.ClientProtocolException)2 ConnectTimeoutException (org.apache.http.conn.ConnectTimeoutException)2 ConnectionPoolTimeoutException (org.apache.http.conn.ConnectionPoolTimeoutException)2 HttpHostConnectException (org.apache.http.conn.HttpHostConnectException)2