use of org.sagacity.sqltoy.model.inner.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);
} else if (json.containsKey("suggest")) {
return extractSuggestFieldValue(sqlToyContext, sqlToyConfig, json, fields);
}
DataSetResult resultModel = new DataSetResult();
// 设置总记录数量
JSONObject hits = json.getJSONObject("hits");
if (hits != null && hits.containsKey("total")) {
Object total = hits.get("total");
if (total instanceof JSONObject) {
resultModel.setRecordCount(((JSONObject) total).getLong("value"));
} else {
resultModel.setRecordCount(Long.parseLong(total.toString()));
}
}
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;
}
NoSqlFieldsModel fieldModel = MongoElasticUtils.processFields(fields, null);
String[] realFields = fieldModel.getFields();
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(fieldModel.getAliasLabels());
return resultModel;
}
use of org.sagacity.sqltoy.model.inner.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 {
QueryExecutorExtend extend = queryExecutor.getInnerModel();
String realSql = MongoElasticUtils.wrapES(sqlToyConfig, extend.getParamsName(sqlToyConfig), extend.getParamsValue(sqlToyContext, sqlToyConfig)).trim();
// sql模式
if (topSize != null) {
realSql = realSql + " limit " + topSize;
}
if (sqlToyContext.isDebug()) {
if (logger.isDebugEnabled()) {
logger.debug("findTopByElastic sql=" + realSql);
} else {
System.out.println("findTopByElastic sql=" + realSql);
}
}
DataSetResult result = ElasticSearchUtils.executeQuery(sqlToyContext, sqlToyConfig, realSql, (Class) extend.resultType, extend.humpMapLabel);
return result.getRows();
}
use of org.sagacity.sqltoy.model.inner.DataSetResult in project sagacity-sqltoy by chenrenfei.
the class ElasticSearchPlugin method executeQuery.
/**
* @todo 执行实际查询处理
* @param sqlToyContext
* @param sqlToyConfig
* @param jsonQuery
* @param resultClass
* @param humpMapLabel
* @return
* @throws Exception
*/
private static DataSetResult executeQuery(SqlToyContext sqlToyContext, SqlToyConfig sqlToyConfig, JSONObject jsonQuery, Class resultClass, boolean humpMapLabel) throws Exception {
NoSqlConfigModel noSqlModel = sqlToyConfig.getNoSqlConfigModel();
ElasticEndpoint esConfig = sqlToyContext.getElasticEndpoint(noSqlModel.getEndpoint());
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();
int aliasIndex;
for (String field : fields) {
aliasIndex = field.indexOf(":");
if (aliasIndex == -1) {
array.add(field);
} else {
array.add(field.substring(0, aliasIndex).trim());
}
}
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) {
Class superClass = resultClass.getSuperclass();
if (!resultClass.equals(ArrayList.class) && !resultClass.equals(List.class) && !resultClass.equals(Array.class) && !resultClass.equals(Collection.class) && !resultClass.equals(HashMap.class) && !resultClass.equals(Map.class) && !resultClass.equals(ConcurrentMap.class) && !resultClass.equals(ConcurrentHashMap.class) && !HashMap.class.equals(superClass) && !Map.class.equals(superClass) && !LinkedHashMap.class.equals(superClass) && !ConcurrentHashMap.class.equals(superClass)) {
fields = BeanUtil.matchSetMethodNames(resultClass);
}
}
if (sqlToyContext.isDebug()) {
if (logger.isDebugEnabled()) {
logger.debug("execute elastic eql=" + jsonQuery.toJSONString());
} else {
System.out.println("execute elastic eql=" + jsonQuery.toJSONString());
}
}
// 执行请求
JSONObject json = HttpClientUtils.doPost(sqlToyContext, noSqlModel, esConfig, 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());
// 不支持指定查询集合的行列转换
boolean changedCols = ResultUtils.calculate(sqlToyContext.getDesensitizeProvider(), sqlToyConfig, resultSet, null, null);
// 将结果数据映射到具体对象类型中
resultSet.setRows(ResultUtils.wrapQueryResult(sqlToyContext, resultSet.getRows(), StringUtil.humpFieldNames(resultSet.getLabelNames()), resultClass, changedCols, humpMapLabel, false, null, null));
return resultSet;
}
use of org.sagacity.sqltoy.model.inner.DataSetResult in project sagacity-sqltoy by chenrenfei.
the class ElasticSearchPlugin method findPage.
/**
* @todo 基于es的分页查询
* @param sqlToyContext
* @param sqlToyConfig
* @param pageModel
* @param queryExecutor
* @return
* @throws Exception
*/
public static Page findPage(SqlToyContext sqlToyContext, SqlToyConfig sqlToyConfig, Page pageModel, QueryExecutor queryExecutor) throws Exception {
String realMql = "";
JSONObject jsonQuery = null;
QueryExecutorExtend extend = queryExecutor.getInnerModel();
try {
realMql = MongoElasticUtils.wrapES(sqlToyConfig, extend.getParamsName(sqlToyConfig), extend.getParamsValue(sqlToyContext, sqlToyConfig)).trim();
jsonQuery = JSON.parseObject(realMql);
jsonQuery.fluentRemove("from");
jsonQuery.fluentRemove("FROM");
jsonQuery.fluentRemove("size");
jsonQuery.fluentRemove("SIZE");
jsonQuery.fluentPut("from", (pageModel.getPageNo() - 1) * pageModel.getPageSize());
jsonQuery.fluentPut("size", pageModel.getPageSize());
} catch (Exception e) {
logger.error("分页解析es原生json错误,请检查json串格式是否正确!错误信息:{},json={}", e.getMessage(), realMql);
throw e;
}
Page page = new Page();
page.setPageNo(pageModel.getPageNo());
page.setPageSize(pageModel.getPageSize());
DataSetResult result = executeQuery(sqlToyContext, sqlToyConfig, jsonQuery, (Class) extend.resultType, extend.humpMapLabel);
page.setRows(result.getRows());
page.setRecordCount(result.getRecordCount());
return page;
}
Aggregations