|
@ -2,26 +2,43 @@ package com.yihu.jw.statistics.util;
|
|
|
|
|
|
import com.alibaba.druid.sql.ast.SQLExpr;
|
|
|
import com.alibaba.druid.sql.ast.expr.SQLQueryExpr;
|
|
|
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock;
|
|
|
import com.alibaba.druid.sql.parser.ParserException;
|
|
|
import com.alibaba.druid.sql.parser.SQLExprParser;
|
|
|
import com.yihu.jw.statistics.etl.save.es.ElasticFactory;
|
|
|
import com.alibaba.druid.sql.parser.Token;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.yihu.jw.statistics.vo.DataModel;
|
|
|
import com.yihu.jw.statistics.vo.SaveModel;
|
|
|
import org.elasticsearch.action.search.SearchRequest;
|
|
|
import org.elasticsearch.action.search.SearchResponse;
|
|
|
import org.elasticsearch.client.Client;
|
|
|
import org.elasticsearch.client.RequestOptions;
|
|
|
import org.elasticsearch.client.RestHighLevelClient;
|
|
|
import org.elasticsearch.index.query.BoolQueryBuilder;
|
|
|
import org.elasticsearch.search.SearchHit;
|
|
|
import org.elasticsearch.search.SearchHits;
|
|
|
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
|
|
import org.nlpcn.es4sql.domain.Select;
|
|
|
import org.nlpcn.es4sql.domain.Where;
|
|
|
import org.nlpcn.es4sql.exception.SqlParseException;
|
|
|
import org.nlpcn.es4sql.jdbc.ObjectResult;
|
|
|
import org.nlpcn.es4sql.jdbc.ObjectResultsExtractor;
|
|
|
import org.nlpcn.es4sql.parse.ElasticSqlExprParser;
|
|
|
import org.nlpcn.es4sql.parse.SqlParser;
|
|
|
import org.nlpcn.es4sql.parse.WhereParser;
|
|
|
import org.nlpcn.es4sql.query.AggregationQueryAction;
|
|
|
import org.nlpcn.es4sql.query.DefaultQueryAction;
|
|
|
import org.nlpcn.es4sql.query.QueryAction;
|
|
|
import org.nlpcn.es4sql.query.SqlElasticSearchRequestBuilder;
|
|
|
import org.nlpcn.es4sql.query.maker.QueryMaker;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import java.lang.reflect.Field;
|
|
|
import java.sql.Timestamp;
|
|
|
import java.text.SimpleDateFormat;
|
|
@ -36,8 +53,8 @@ import java.util.*;
|
|
|
@Component
|
|
|
public class ElasticsearchUtil {
|
|
|
private Logger logger= LoggerFactory.getLogger(ElasticsearchUtil.class);
|
|
|
@Autowired
|
|
|
private ElasticFactory elasticFactory;
|
|
|
@Resource(name="restHighLevelClient")
|
|
|
private RestHighLevelClient restHighLevelClient;
|
|
|
@Value("${es.type}")
|
|
|
private String esType;
|
|
|
@Value("${es.index}")
|
|
@ -312,6 +329,80 @@ public class ElasticsearchUtil {
|
|
|
return quotaDate + "T00:00:00+0800";
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 执行sql
|
|
|
*/
|
|
|
public <T> List<T> executeSql(String sql,Class<T> clazz) throws Exception {
|
|
|
//实例化查询请求对象
|
|
|
SearchRequest request = new SearchRequest();
|
|
|
//实例化SearchSourceBuilder
|
|
|
SearchSourceBuilder searchBuilder = new SearchSourceBuilder();
|
|
|
//根据索引、查询条件构建查询构造器
|
|
|
BoolQueryBuilder boolQueryBuilder = createQueryBuilderBySql(sql);
|
|
|
//将查询构造器注入SearchSourceBuilder
|
|
|
searchBuilder.query(boolQueryBuilder);
|
|
|
//设置请求查询的索引(查询构造器中已指定,无需重复设置)
|
|
|
//request.indices(indexName);
|
|
|
//将构建好的SearchSourceBuilder注入请求
|
|
|
request.source(searchBuilder);
|
|
|
|
|
|
//带入请求执行查询
|
|
|
SearchResponse searchResponse = restHighLevelClient.search(request, RequestOptions.DEFAULT);
|
|
|
//得到查询结果
|
|
|
SearchHits hits = searchResponse.getHits();
|
|
|
|
|
|
SearchHit[] searchHits = hits.getHits();
|
|
|
List<T> listData = new ArrayList<>();
|
|
|
//遍历查询结果
|
|
|
for(SearchHit hit : searchHits){
|
|
|
Map<String,Object> datas = hit.getSourceAsMap();
|
|
|
T t1 = JSON.parseObject(JSON.toJSONString(datas),clazz);
|
|
|
listData.add(t1);
|
|
|
logger.info(datas.toString());
|
|
|
}
|
|
|
|
|
|
return listData;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 构建查询构造器
|
|
|
* @return
|
|
|
*/
|
|
|
public BoolQueryBuilder createQueryBuilderBySql(String sql) {
|
|
|
BoolQueryBuilder boolQuery = null;
|
|
|
try {
|
|
|
SQLExprParser parser = new ElasticSqlExprParser(sql);
|
|
|
|
|
|
SQLQueryExpr sqlExpr = (SQLQueryExpr) toSqlExpr(sql);
|
|
|
SqlParser sqlParser = new SqlParser();
|
|
|
MySqlSelectQueryBlock query = (MySqlSelectQueryBlock) sqlExpr.getSubQuery().getQuery();
|
|
|
WhereParser whereParser = new WhereParser(sqlParser, query);
|
|
|
Where where = whereParser.findWhere();
|
|
|
if (where != null) {
|
|
|
boolQuery = QueryMaker.explan(where);
|
|
|
}
|
|
|
|
|
|
} catch (SqlParseException e) {
|
|
|
logger.info("ReadES.createQueryBuilderByExpress-Exception,"+e.getMessage());
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
return boolQuery;
|
|
|
}
|
|
|
/**
|
|
|
* 验证sql
|
|
|
*
|
|
|
* @param sql sql查询语句
|
|
|
* @return and (a=1 and b=1) or (c=1 and d=1)
|
|
|
*/
|
|
|
private SQLExpr toSqlExpr(String sql) {
|
|
|
SQLExprParser parser = new ElasticSqlExprParser(sql);
|
|
|
SQLExpr expr = parser.expr();
|
|
|
|
|
|
if (parser.getLexer().token() != Token.EOF) {
|
|
|
throw new ParserException("illegal sql expr : " + sql);
|
|
|
}
|
|
|
return expr;
|
|
|
}
|
|
|
/**
|
|
|
* 执行sql查询es
|
|
|
*
|
|
@ -336,11 +427,11 @@ public class ElasticsearchUtil {
|
|
|
SqlElasticSearchRequestBuilder requestBuilder = null;
|
|
|
if (select.isAgg) {
|
|
|
//包含计算的的排序分组的
|
|
|
action = new AggregationQueryAction(elasticFactory.getTransportClient(), select);
|
|
|
action = new AggregationQueryAction((Client) restHighLevelClient.getLowLevelClient(), select);
|
|
|
requestBuilder = action.explain();
|
|
|
} else {
|
|
|
//封装成自己的Select对象
|
|
|
Client client = elasticFactory.getTransportClient();
|
|
|
Client client = (Client) restHighLevelClient.getLowLevelClient();
|
|
|
queryAction = new DefaultQueryAction(client, select);
|
|
|
requestBuilder = queryAction.explain();
|
|
|
}
|
|
@ -351,7 +442,7 @@ public class ElasticsearchUtil {
|
|
|
}else{
|
|
|
queryResult = response.getHits();
|
|
|
}
|
|
|
ObjectResult temp = new ObjectResultsExtractor(true, true, true).extractResults(queryResult, true);
|
|
|
ObjectResult temp = new ObjectResultsExtractor(true, true, true,true,queryAction).extractResults(queryResult, true);
|
|
|
List<String> heads = temp.getHeaders();
|
|
|
temp.getLines().stream().forEach(one -> {
|
|
|
try {
|
|
@ -411,15 +502,18 @@ public class ElasticsearchUtil {
|
|
|
//通过抽象语法树,封装成自定义的Select,包含了select、from、where group、limit等
|
|
|
AggregationQueryAction action = null;
|
|
|
DefaultQueryAction queryAction = null;
|
|
|
QueryAction queryAction1 = null;
|
|
|
SqlElasticSearchRequestBuilder requestBuilder = null;
|
|
|
if (select.isAgg) {
|
|
|
//包含计算的的排序分组的
|
|
|
action = new AggregationQueryAction(elasticFactory.getTransportClient(), select);
|
|
|
action = new AggregationQueryAction((Client) restHighLevelClient.getLowLevelClient(), select);
|
|
|
requestBuilder = action.explain();
|
|
|
queryAction1 =action;
|
|
|
} else {
|
|
|
//封装成自己的Select对象
|
|
|
queryAction = new DefaultQueryAction(elasticFactory.getTransportClient(), select);
|
|
|
queryAction = new DefaultQueryAction((Client) restHighLevelClient.getLowLevelClient(), select);
|
|
|
requestBuilder = queryAction.explain();
|
|
|
queryAction1= queryAction;
|
|
|
}
|
|
|
SearchResponse response = (SearchResponse) requestBuilder.get();
|
|
|
Object queryResult = null;
|
|
@ -428,7 +522,7 @@ public class ElasticsearchUtil {
|
|
|
} else {
|
|
|
queryResult = response.getHits();
|
|
|
}
|
|
|
ObjectResult temp = new ObjectResultsExtractor(true, true, true).extractResults(queryResult, true);
|
|
|
ObjectResult temp = new ObjectResultsExtractor(true, true, true,true,queryAction1).extractResults(queryResult, true);
|
|
|
List<String> heads = temp.getHeaders();
|
|
|
temp.getLines().forEach(one -> {
|
|
|
Object saveModel = null;
|
|
@ -533,16 +627,19 @@ public class ElasticsearchUtil {
|
|
|
//通过抽象语法树,封装成自定义的Select,包含了select、from、where group、limit等
|
|
|
AggregationQueryAction action = null;
|
|
|
DefaultQueryAction queryAction = null;
|
|
|
QueryAction queryAction1 = null;
|
|
|
SqlElasticSearchRequestBuilder requestBuilder = null;
|
|
|
if (select.isAgg) {
|
|
|
//包含计算的的排序分组的
|
|
|
action = new AggregationQueryAction(elasticFactory.getTransportClient(), select);
|
|
|
action = new AggregationQueryAction((Client) restHighLevelClient.getLowLevelClient(), select);
|
|
|
requestBuilder = action.explain();
|
|
|
queryAction1=action;
|
|
|
} else {
|
|
|
//封装成自己的Select对象
|
|
|
Client client = elasticFactory.getTransportClient();
|
|
|
Client client = (Client) restHighLevelClient.getLowLevelClient();
|
|
|
queryAction = new DefaultQueryAction(client, select);
|
|
|
requestBuilder = queryAction.explain();
|
|
|
queryAction1=queryAction;
|
|
|
}
|
|
|
SearchResponse response = (SearchResponse) requestBuilder.get();
|
|
|
Object queryResult = null;
|
|
@ -551,7 +648,7 @@ public class ElasticsearchUtil {
|
|
|
}else{
|
|
|
queryResult = response.getHits();
|
|
|
}
|
|
|
ObjectResult temp = new ObjectResultsExtractor(true, true, true).extractResults(queryResult, true);
|
|
|
ObjectResult temp = new ObjectResultsExtractor(true, true, true,true,queryAction1).extractResults(queryResult, true);
|
|
|
List<String> heads = temp.getHeaders();
|
|
|
for(List<Object> one:temp.getLines()){
|
|
|
// temp.getLines().stream().forEach(one -> {
|