use of org.sagacity.sqltoy.model.DataSetResult in project sagacity-sqltoy by chenrenfei.
the class ElasticSearchUtils method extractFieldValue.
/**
* @todo 从返回的JSON对象中根据字段属性提取数据并以集合形式返回
* @param sqlToyContext
* @param sqlToyConfig
* @param json
* @param fields
* @return
*/
public static DataSetResult extractFieldValue(SqlToyContext sqlToyContext, SqlToyConfig sqlToyConfig, JSONObject json, String[] fields) {
// 聚合数据提取
if (sqlToyConfig.getNoSqlConfigModel().isHasAggs() || json.getJSONObject("aggregations") != null)
return extractAggsFieldValue(sqlToyContext, sqlToyConfig, json, fields);
DataSetResult resultModel = new DataSetResult();
// 设置总记录数量
if (json.getJSONObject("hits") != null) {
Long total = json.getJSONObject("hits").getLong("total");
resultModel.setTotalCount(total);
}
NoSqlConfigModel nosqlConfig = sqlToyConfig.getNoSqlConfigModel();
List result = new ArrayList();
String[] valuePath = (nosqlConfig.getValueRoot() == null) ? new String[] { "hits", "hits" } : nosqlConfig.getValueRoot();
JSONObject root = json;
String lastKey = valuePath[valuePath.length - 1];
for (int i = 0; i < valuePath.length - 1; i++) {
if (root != null)
root = root.getJSONObject(valuePath[i]);
else
return resultModel;
}
Object realRoot = root.get(lastKey);
if (realRoot == null)
return resultModel;
String[] realFields = new String[fields.length];
String[] translateFields = new String[fields.length];
System.arraycopy(fields, 0, realFields, 0, fields.length);
System.arraycopy(fields, 0, translateFields, 0, fields.length);
int aliasIndex = 0;
for (int i = 0; i < realFields.length; i++) {
aliasIndex = realFields[i].indexOf(":");
if (aliasIndex != -1) {
realFields[i] = realFields[i].substring(0, aliasIndex).trim();
translateFields[i] = translateFields[i].substring(aliasIndex + 1).trim();
}
}
JSONObject rowJson, sourceData;
if (realRoot instanceof JSONArray) {
JSONArray array = (JSONArray) realRoot;
for (int i = 0; i < array.size(); i++) {
rowJson = (JSONObject) array.get(i);
// 非聚合,数据取_source
sourceData = rowJson.getJSONObject("_source");
addRow(result, sourceData, realFields);
}
} else if (realRoot instanceof JSONObject) {
addRow(result, (JSONObject) realRoot, realFields);
}
resultModel.setRows(result);
resultModel.setLabelNames(translateFields);
return resultModel;
}
use of org.sagacity.sqltoy.model.DataSetResult in project sagacity-sqltoy by chenrenfei.
the class ElasticSearchUtils method extractAggsFieldValue.
/**
* @todo 提取聚合数据
* @param sqlToyContext
* @param sqlToyConfig
* @param json
* @param fields
* @return
*/
public static DataSetResult extractAggsFieldValue(SqlToyContext sqlToyContext, SqlToyConfig sqlToyConfig, JSONObject json, String[] fields) {
DataSetResult resultModel = new DataSetResult();
// 切取实际字段{field:aliasName}模式,冒号前面的实际字段
String[] realFields = new String[fields.length];
String[] translateFields = new String[fields.length];
int index = 0;
for (String field : fields) {
if (field.indexOf(":") != -1) {
realFields[index] = field.substring(0, field.indexOf(":")).trim();
translateFields[index] = field.substring(field.indexOf(":") + 1).trim();
} else {
realFields[index] = field;
translateFields[index] = field;
}
index++;
}
// 获取json对象的根
String[] rootPath = (sqlToyConfig.getNoSqlConfigModel().getValueRoot() == null) ? new String[] { "aggregations" } : sqlToyConfig.getNoSqlConfigModel().getValueRoot();
Object root = json;
// 确保第一个路径是聚合统一的名词
if (!rootPath[0].equalsIgnoreCase("aggregations"))
root = ((JSONObject) root).get("aggregations");
for (String str : rootPath) {
root = ((JSONObject) root).get(str);
}
// 循环取根
while (root != null && (root instanceof JSONObject)) {
JSONObject tmp = (JSONObject) root;
if (tmp.containsKey("buckets")) {
root = tmp.get("buckets");
break;
}
// 判断属性是否跟所取字段有交集,有则表示取到了根
int matchCnt = 0;
for (String key : tmp.keySet()) {
for (String field : realFields) {
if (key.equalsIgnoreCase(field)) {
matchCnt++;
break;
}
}
}
if (matchCnt > 0) {
root = tmp;
break;
} else {
if (tmp.keySet().size() == 1) {
root = tmp.values().iterator().next();
}
}
}
if (root == null) {
logger.error("请正确配置es聚合查询,包括:fields配置是否匹配等!");
return resultModel;
}
List result = new ArrayList();
if (root instanceof JSONObject) {
processRow(result, (JSONObject) root, realFields);
} else if (root instanceof JSONArray) {
JSONArray array = (JSONArray) root;
for (Object tmp : array) {
processRow(result, (JSONObject) tmp, realFields);
}
}
if (result != null)
resultModel.setTotalCount(Long.valueOf(result.size()));
resultModel.setRows(result);
resultModel.setLabelNames(translateFields);
return resultModel;
}
use of org.sagacity.sqltoy.model.DataSetResult in project sagacity-sqltoy by chenrenfei.
the class ElasticSearchPlugin method executeQuery.
/**
* @todo 执行实际查询处理
* @param sqlToyContext
* @param sqlToyConfig
* @param jsonQuery
* @param resultClass
* @return
* @throws Exception
*/
private static DataSetResult executeQuery(SqlToyContext sqlToyContext, SqlToyConfig sqlToyConfig, JSONObject jsonQuery, String resultClass) throws Exception {
NoSqlConfigModel noSqlModel = sqlToyConfig.getNoSqlConfigModel();
String source = "_source";
// 是否设置了fields
boolean hasFields = false;
if (jsonQuery.containsKey(source)) {
hasFields = true;
} else if (jsonQuery.containsKey(source.toUpperCase())) {
hasFields = true;
source = source.toUpperCase();
}
String[] fields = null;
if (noSqlModel.getFields() != null) {
fields = noSqlModel.getFields();
// 没有设置显示字段,且是非聚合查询时将配置的fields设置到查询json中
if (!hasFields && !noSqlModel.isHasAggs()) {
JSONArray array = new JSONArray();
for (String field : fields) array.add(field);
jsonQuery.fluentPut("_source", array);
}
} else if (hasFields) {
Object[] array = (Object[]) jsonQuery.getJSONArray(source).toArray();
fields = new String[array.length];
for (int i = 0; i < fields.length; i++) fields[i] = array[i].toString();
} else if (resultClass != null && !resultClass.equalsIgnoreCase("map") && !resultClass.equalsIgnoreCase("hashmap") && !resultClass.equalsIgnoreCase("linkedHashMap") && !resultClass.equalsIgnoreCase("linkedMap")) {
fields = BeanUtil.matchSetMethodNames(Class.forName(resultClass));
}
// 执行请求
JSONObject json = HttpClientUtils.doPost(sqlToyContext, noSqlModel, jsonQuery);
if (json == null || json.isEmpty())
return new DataSetResult();
DataSetResult resultSet = ElasticSearchUtils.extractFieldValue(sqlToyContext, sqlToyConfig, json, fields);
MongoElasticUtils.processTranslate(sqlToyContext, sqlToyConfig, resultSet.getRows(), resultSet.getLabelNames());
// 不支持指定查询集合的行列转换
ResultUtils.calculate(sqlToyConfig, resultSet, null, sqlToyContext.isDebug());
// 将结果数据映射到具体对象类型中
resultSet.setRows(MongoElasticUtils.wrapResultClass(resultSet.getRows(), resultSet.getLabelNames(), resultClass));
return resultSet;
}
use of org.sagacity.sqltoy.model.DataSetResult in project sagacity-sqltoy by chenrenfei.
the class ElasticSqlPlugin method findTop.
/**
* @todo 提取符合条件的前多少条记录
* @param sqlToyContext
* @param sqlToyConfig
* @param queryExecutor
* @param topSize
* @return
* @throws Exception
*/
public static List findTop(SqlToyContext sqlToyContext, SqlToyConfig sqlToyConfig, QueryExecutor queryExecutor, Integer topSize) throws Exception {
String realMql = MongoElasticUtils.wrapES(sqlToyConfig, queryExecutor.getParamsName(sqlToyConfig), queryExecutor.getParamsValue(sqlToyConfig)).trim();
// sql模式
if (topSize != null) {
realMql = realMql + " limit 0," + topSize;
}
if (sqlToyContext.isDebug()) {
out.println("execute eql={" + realMql + "}");
}
DataSetResult result = ElasticSearchUtils.executeQuery(sqlToyContext, sqlToyConfig, realMql, queryExecutor.getResultTypeName());
return result.getRows();
}
use of org.sagacity.sqltoy.model.DataSetResult in project sagacity-sqltoy by chenrenfei.
the class ElasticSqlPlugin method findPage.
/**
* @todo 基于es的分页查询
* @param sqlToyContext
* @param sqlToyConfig
* @param pageModel
* @param queryExecutor
* @return
* @throws Exception
*/
public static PaginationModel findPage(SqlToyContext sqlToyContext, SqlToyConfig sqlToyConfig, PaginationModel pageModel, QueryExecutor queryExecutor) throws Exception {
String realMql = MongoElasticUtils.wrapES(sqlToyConfig, queryExecutor.getParamsName(sqlToyConfig), queryExecutor.getParamsValue(sqlToyConfig)).trim();
// sql模式
realMql = realMql + " limit " + (pageModel.getPageNo() - 1) * pageModel.getPageSize() + "," + pageModel.getPageSize();
if (sqlToyContext.isDebug()) {
out.println("execute eql={" + realMql + "}");
}
PaginationModel page = new PaginationModel();
page.setPageNo(pageModel.getPageNo());
page.setPageSize(pageModel.getPageSize());
DataSetResult result = ElasticSearchUtils.executeQuery(sqlToyContext, sqlToyConfig, realMql, queryExecutor.getResultTypeName());
page.setRows(result.getRows());
page.setRecordCount(result.getTotalCount());
return page;
}
Aggregations