use of org.sagacity.sqltoy.config.model.NoSqlConfigModel in project sagacity-sqltoy by chenrenfei.
the class TestMain method main.
public static void main(String[] args) throws Exception {
JSONObject json = JSON.parseObject(FileUtil.readAsString(new File("D:/es_result.json")));
String[] path = { "aggregations", "state" };
// System.err.println(json.get("aggregations"));
String[] fields = new String[] { "key:province", "min_balance", "total_balance", "max_balance", "avg_balance" };
NoSqlConfigModel noSqlConfigModel = new NoSqlConfigModel();
noSqlConfigModel.setFields(fields);
List<List> result = parse(json, noSqlConfigModel, fields);
for (List row : result) {
System.err.println(row);
}
}
use of org.sagacity.sqltoy.config.model.NoSqlConfigModel 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.config.model.NoSqlConfigModel in project sagacity-sqltoy by chenrenfei.
the class Mongo method findPage.
/**
* @todo 分页查询
* @param pageModel
* @return
* @throws Exception
*/
public PaginationModel findPage(PaginationModel pageModel) throws Exception {
QueryExecutor queryExecutor = build();
SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(sql);
NoSqlConfigModel noSqlModel = sqlToyConfig.getNoSqlConfigModel();
if (noSqlModel == null || noSqlModel.getCollection() == null || noSqlModel.getFields() == null)
throw new Exception(ERROR_MESSAGE);
// 最后的执行语句
String realMql = MongoElasticUtils.wrapMql(sqlToyConfig, queryExecutor.getParamsName(sqlToyConfig), queryExecutor.getParamsValue(sqlToyConfig));
return findPage(new MongoTemplate(getMongoDbFactory(noSqlModel.getMongoFactory())), sqlToyConfig, pageModel, realMql, queryExecutor.getResultTypeName());
}
use of org.sagacity.sqltoy.config.model.NoSqlConfigModel 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.config.model.NoSqlConfigModel in project sagacity-sqltoy by chenrenfei.
the class Elastic method findPage.
/**
* @todo 分页查询
* @param pageModel
* @return
*/
public Page findPage(Page pageModel) {
QueryExecutor queryExecutor = build();
SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(sql, SqlType.search, "");
NoSqlConfigModel noSqlConfig = sqlToyConfig.getNoSqlConfigModel();
if (noSqlConfig == null) {
throw new IllegalArgumentException(ERROR_MESSAGE);
}
Page pageResult = null;
try {
if (noSqlConfig.isSqlMode()) {
ElasticEndpoint esConfig = sqlToyContext.getElasticEndpoint(noSqlConfig.getEndpoint());
if (esConfig.isNativeSql()) {
throw new UnsupportedOperationException("elastic native sql pagination is not support!");
}
pageResult = ElasticSqlPlugin.findPage(sqlToyContext, sqlToyConfig, pageModel, queryExecutor);
} else {
pageResult = ElasticSearchPlugin.findPage(sqlToyContext, sqlToyConfig, pageModel, queryExecutor);
}
if (pageResult.getRecordCount() == 0 && sqlToyContext.isPageOverToFirst()) {
pageResult.setPageNo(1L);
}
return pageResult;
} catch (Exception e) {
e.printStackTrace();
throw new DataAccessException(e);
}
}
Aggregations