use of org.sagacity.sqltoy.config.model.NoSqlFieldsModel in project sagacity-sqltoy by chenrenfei.
the class Mongo method extractFieldValues.
private List extractFieldValues(SqlToyConfig sqlToyConfig, Iterator<Document> iter, Class resultClass, boolean humpMapLabel) throws Exception {
List resultSet = new ArrayList();
Document row;
HashMap<String, String[]> linkMap = new HashMap<String, String[]>();
NoSqlFieldsModel fieldModel = MongoElasticUtils.processFields(sqlToyConfig.getNoSqlConfigModel().getFields(), linkMap);
// 解决field采用id.name:aliasName 或 id.name 形式
String[] realFields = fieldModel.getFields();
String[] translateFields = fieldModel.getAliasLabels();
String[] keys;
int size;
String key;
Document val;
List rowData;
while (iter.hasNext()) {
row = iter.next();
rowData = new ArrayList();
for (String name : realFields) {
// 存在_id.xxx 模式
keys = linkMap.get(name);
if (null == keys) {
rowData.add(row.get(name));
} else {
val = row;
size = keys.length;
for (int i = 0; i < size; i++) {
key = keys[i];
// 最后一个.xx
if (i == size - 1) {
rowData.add(val.get(key));
} else {
val = (Document) val.get(key);
}
}
}
}
resultSet.add(rowData);
}
MongoElasticUtils.processTranslate(sqlToyContext, sqlToyConfig, resultSet, translateFields);
DataSetResult dataSetResult = new DataSetResult();
dataSetResult.setRows(resultSet);
dataSetResult.setLabelNames(translateFields);
// 不支持指定查询集合的行列转换,对集合进行汇总、行列转换等
boolean changedCols = ResultUtils.calculate(sqlToyContext.getDesensitizeProvider(), sqlToyConfig, dataSetResult, null, null);
return ResultUtils.wrapQueryResult(sqlToyContext, resultSet, StringUtil.humpFieldNames(translateFields), resultClass, changedCols, humpMapLabel, false, null, null);
}
use of org.sagacity.sqltoy.config.model.NoSqlFieldsModel in project sagacity-sqltoy by chenrenfei.
the class ElasticSearchUtils method extractSuggestFieldValue.
/**
* @todo 提取聚合数据
* @param sqlToyContext
* @param sqlToyConfig
* @param json
* @param fields
* @return
*/
private static DataSetResult extractSuggestFieldValue(SqlToyContext sqlToyContext, SqlToyConfig sqlToyConfig, JSONObject json, String[] fields) {
DataSetResult resultModel = new DataSetResult();
// 切取实际字段{field:aliasName}模式,冒号前面的实际字段
NoSqlFieldsModel fieldModel = MongoElasticUtils.processFields(fields, null);
String[] realFields = fieldModel.getFields();
// 获取json对象的根
String[] rootPath = (sqlToyConfig.getNoSqlConfigModel().getValueRoot() == null) ? new String[] { "suggest" } : sqlToyConfig.getNoSqlConfigModel().getValueRoot();
Object root = json;
// 确保第一个路径是聚合统一的名词
if (!rootPath[0].toLowerCase().equals("suggest")) {
root = ((JSONObject) root).get("suggest");
}
for (String str : rootPath) {
root = ((JSONObject) root).get(str);
}
if (root == null) {
logger.error("请正确配置es聚合查询,包括:fields配置是否匹配等!");
return resultModel;
}
List result = new ArrayList();
if (root instanceof JSONObject) {
processRow(result, (JSONObject) root, realFields, true);
} else if (root instanceof JSONArray) {
JSONArray array = (JSONArray) root;
for (Object tmp : array) {
processRow(result, (JSONObject) tmp, realFields, true);
}
}
if (result != null) {
resultModel.setRecordCount(Long.valueOf(result.size()));
}
resultModel.setRows(result);
resultModel.setLabelNames(fieldModel.getAliasLabels());
return resultModel;
}
use of org.sagacity.sqltoy.config.model.NoSqlFieldsModel 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}模式,冒号前面的实际字段
NoSqlFieldsModel fieldModel = MongoElasticUtils.processFields(fields, null);
String[] realFields = fieldModel.getFields();
// 获取json对象的根
String[] rootPath = (sqlToyConfig.getNoSqlConfigModel().getValueRoot() == null) ? new String[] { "aggregations" } : sqlToyConfig.getNoSqlConfigModel().getValueRoot();
Object root = json;
// 确保第一个路径是聚合统一的名词
if (!rootPath[0].toLowerCase().equals("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, false);
} else if (root instanceof JSONArray) {
JSONArray array = (JSONArray) root;
for (Object tmp : array) {
processRow(result, (JSONObject) tmp, realFields, false);
}
}
if (result != null) {
resultModel.setRecordCount(Long.valueOf(result.size()));
}
resultModel.setRows(result);
resultModel.setLabelNames(fieldModel.getAliasLabels());
return resultModel;
}
use of org.sagacity.sqltoy.config.model.NoSqlFieldsModel in project sagacity-sqltoy by chenrenfei.
the class ElasticSearchUtils method extractSqlFieldValue.
/**
* @todo elasticsearch6.3 sql
* @param sqlToyContext
* @param sqlToyConfig
* @param json
* @param fields
* @return
*/
private static DataSetResult extractSqlFieldValue(SqlToyContext sqlToyContext, SqlToyConfig sqlToyConfig, JSONObject json, String[] fields) {
DataSetResult resultModel = new DataSetResult();
Object realRoot = json.get("rows");
if (realRoot == null) {
return resultModel;
}
NoSqlFieldsModel fieldModel = MongoElasticUtils.processFields(fields, null);
JSONArray rows = (JSONArray) realRoot;
JSONArray item;
List<List<Object>> resultSet = new ArrayList<List<Object>>();
for (Object row : rows) {
item = (JSONArray) row;
List<Object> result = new ArrayList<Object>();
for (Object cel : item) {
result.add(cel);
}
resultSet.add(result);
}
resultModel.setRows(resultSet);
resultModel.setLabelNames(fieldModel.getAliasLabels());
return resultModel;
}
use of org.sagacity.sqltoy.config.model.NoSqlFieldsModel 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;
}
Aggregations