|
@ -3,9 +3,11 @@ package com.yihu.wlyy.statistics.util;
|
|
import com.alibaba.druid.sql.ast.SQLExpr;
|
|
import com.alibaba.druid.sql.ast.SQLExpr;
|
|
import com.alibaba.druid.sql.ast.expr.SQLQueryExpr;
|
|
import com.alibaba.druid.sql.ast.expr.SQLQueryExpr;
|
|
import com.alibaba.druid.sql.parser.SQLExprParser;
|
|
import com.alibaba.druid.sql.parser.SQLExprParser;
|
|
|
|
import com.yihu.wlyy.entity.patient.Patient;
|
|
import com.yihu.wlyy.statistics.etl.save.es.ElasticFactory;
|
|
import com.yihu.wlyy.statistics.etl.save.es.ElasticFactory;
|
|
import com.yihu.wlyy.statistics.vo.DataModel;
|
|
import com.yihu.wlyy.statistics.vo.DataModel;
|
|
import com.yihu.wlyy.statistics.vo.SaveModel;
|
|
import com.yihu.wlyy.statistics.vo.SaveModel;
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.elasticsearch.action.search.SearchResponse;
|
|
import org.elasticsearch.action.search.SearchResponse;
|
|
import org.elasticsearch.client.Client;
|
|
import org.elasticsearch.client.Client;
|
|
import org.nlpcn.es4sql.domain.Select;
|
|
import org.nlpcn.es4sql.domain.Select;
|
|
@ -20,10 +22,14 @@ import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
import java.sql.Timestamp;
|
|
|
|
import java.text.DateFormat;
|
|
|
|
import java.text.ParseException;
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
import java.util.*;
|
|
|
|
|
|
/**
|
|
/**
|
|
* Created by chenweida on 2017/7/17.
|
|
* Created by chenweida on 2017/7/17.
|
|
@ -374,6 +380,112 @@ public class ElasticsearchUtil {
|
|
return saveModels;
|
|
return saveModels;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public<T> List<T> excute(String sql, Class<T> clazz) {
|
|
|
|
List saveModels = new ArrayList<>();
|
|
|
|
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXX");
|
|
|
|
SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
|
|
|
try {
|
|
|
|
//解决 group by之后默认是200的问题
|
|
|
|
if (sql.toLowerCase().contains("group by")) {
|
|
|
|
sql = sql + " limit 0,2000";
|
|
|
|
}
|
|
|
|
|
|
|
|
SQLExprParser parser = new ElasticSqlExprParser(sql);
|
|
|
|
SQLExpr expr = parser.expr();
|
|
|
|
SQLQueryExpr queryExpr = (SQLQueryExpr) expr;
|
|
|
|
|
|
|
|
Select select = null;
|
|
|
|
select = new SqlParser().parseSelect(queryExpr);
|
|
|
|
|
|
|
|
//通过抽象语法树,封装成自定义的Select,包含了select、from、where group、limit等
|
|
|
|
AggregationQueryAction action = null;
|
|
|
|
DefaultQueryAction queryAction = null;
|
|
|
|
SqlElasticSearchRequestBuilder requestBuilder = null;
|
|
|
|
if (select.isAgg) {
|
|
|
|
//包含计算的的排序分组的
|
|
|
|
action = new AggregationQueryAction(elasticFactory.getTransportClient(), select);
|
|
|
|
requestBuilder = action.explain();
|
|
|
|
} else {
|
|
|
|
//封装成自己的Select对象
|
|
|
|
queryAction = new DefaultQueryAction(elasticFactory.getTransportClient(), select);
|
|
|
|
requestBuilder = queryAction.explain();
|
|
|
|
}
|
|
|
|
SearchResponse response = (SearchResponse) requestBuilder.get();
|
|
|
|
Object queryResult = null;
|
|
|
|
if (sql.toUpperCase().indexOf("GROUP") != -1 || sql.toUpperCase().indexOf("SUM") != -1 || select.isAgg) {
|
|
|
|
queryResult = response.getAggregations();
|
|
|
|
} else {
|
|
|
|
queryResult = response.getHits();
|
|
|
|
}
|
|
|
|
ObjectResult temp = new ObjectResultsExtractor(true, true, true).extractResults(queryResult, true);
|
|
|
|
List<String> heads = temp.getHeaders();
|
|
|
|
temp.getLines().forEach(one -> {
|
|
|
|
Object saveModel = null;
|
|
|
|
try {
|
|
|
|
|
|
|
|
saveModel = clazz.newInstance();
|
|
|
|
} catch (Exception e) {
|
|
|
|
logger.error(e.getMessage());
|
|
|
|
}
|
|
|
|
for (int i = 0; i < one.size(); i++) {
|
|
|
|
try {
|
|
|
|
String key = null;
|
|
|
|
Object value = one.get(i);
|
|
|
|
if (heads.get(i).startsWith("_")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
key = "set" + UpFirstStr(heads.get(i));
|
|
|
|
if (heads.get(i).contains("quotaDate") || heads.get(i).contains("createTime") || heads.get(i).contains("date_histogram")) {
|
|
|
|
if (heads.get(i).contains("date_histogram")) {
|
|
|
|
key = "setQuotaDate";
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
//yyyy-MM-dd'T'HH:mm:ssXX
|
|
|
|
value = dateFormat.parse(String.valueOf(one.get(i)));
|
|
|
|
} catch (Exception e) {
|
|
|
|
//yyyy-MM-dd HH:mm:ss
|
|
|
|
try {
|
|
|
|
value = dateFormat1.parse(String.valueOf(one.get(i)));
|
|
|
|
}catch (Exception e1){
|
|
|
|
Timestamp ts = new Timestamp(Long.parseLong(String.valueOf(one.get(i))));
|
|
|
|
try {
|
|
|
|
Date date = new Date();
|
|
|
|
date = ts;
|
|
|
|
value =date;
|
|
|
|
|
|
|
|
} catch (Exception e2) {
|
|
|
|
value = String.valueOf(one.get(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
// value = DateUtil.strToDate(String.valueOf(value).replace("T00:00:00+0800", " 00:00:00"), "yyyy-MM-dd HH:mm:ss");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value instanceof String) {
|
|
|
|
clazz.getMethod(key, String.class).invoke(saveModel, value);
|
|
|
|
} else if (value instanceof Integer) {
|
|
|
|
clazz.getMethod(key, Integer.class).invoke(saveModel, value);
|
|
|
|
} else if (value instanceof Double) {
|
|
|
|
clazz.getMethod(key, Double.class).invoke(saveModel, value);
|
|
|
|
} else if (value instanceof java.util.Date) {
|
|
|
|
clazz.getMethod(key, java.util.Date.class).invoke(saveModel, value);
|
|
|
|
} else if (value instanceof java.util.List) {
|
|
|
|
clazz.getMethod(key, java.util.List.class).invoke(saveModel, value);
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
logger.warn(e.getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
saveModels.add(saveModel);
|
|
|
|
});
|
|
|
|
} catch (Exception e) {
|
|
|
|
logger.error(e.getMessage());
|
|
|
|
}
|
|
|
|
return saveModels;
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* 执行sql查询es
|
|
* 执行sql查询es
|
|
@ -416,7 +528,8 @@ public class ElasticsearchUtil {
|
|
}
|
|
}
|
|
ObjectResult temp = new ObjectResultsExtractor(true, true, true).extractResults(queryResult, true);
|
|
ObjectResult temp = new ObjectResultsExtractor(true, true, true).extractResults(queryResult, true);
|
|
List<String> heads = temp.getHeaders();
|
|
List<String> heads = temp.getHeaders();
|
|
temp.getLines().stream().forEach(one -> {
|
|
|
|
|
|
for(List<Object> one:temp.getLines()){
|
|
|
|
// temp.getLines().stream().forEach(one -> {
|
|
try {
|
|
try {
|
|
DataModel dataModel = new DataModel();
|
|
DataModel dataModel = new DataModel();
|
|
for (int i = 0; i < one.size(); i++) {
|
|
for (int i = 0; i < one.size(); i++) {
|
|
@ -448,8 +561,8 @@ public class ElasticsearchUtil {
|
|
saveModels.add(dataModel);
|
|
saveModels.add(dataModel);
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
}
|
|
|
|
});
|
|
|
|
|
|
}}
|
|
|
|
// });
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
}
|
|
}
|