use of org.sagacity.sqltoy.model.inner.DataSetResult in project sagacity-sqltoy by chenrenfei.
the class UnPivotListTest method testUnpivotSingle.
@Test
public void testUnpivotSingle() {
// |------- 1月-------|------- 2月 ------|------ 3月--------|
// |交易笔 | 金额 | 收入 |交易笔 | 金额 | 收入 |交易笔 | 金额 | 收入 |
Object[][] values = { { "5月", "香蕉", 2000, 20000 } };
List result = CollectionUtil.arrayToDeepList(values);
UnpivotModel unpivotModel = new UnpivotModel();
unpivotModel.setColumnsToRows(new String[] { "quantity:数量,AMT:金额" });
unpivotModel.setNewColumnsLabels(new String[] { "indexName", "indexValue" });
DataSetResult resultModel = new DataSetResult();
resultModel.setLabelNames(new String[] { "month", "fruitName", "quantity", "AMT" });
resultModel.setLabelTypes(new String[] { "string", "string", "decimal", "decimal" });
LabelIndexModel labelIndexMap = new LabelIndexModel();
labelIndexMap.put("month", 0);
labelIndexMap.put("fruitname", 1);
labelIndexMap.put("quantity", 2);
labelIndexMap.put("amt", 3);
List value = UnpivotList.process(unpivotModel, resultModel, labelIndexMap, result);
for (int i = 0; i < value.size(); i++) {
System.out.println(JSON.toJSONString(result.get(i)));
}
}
use of org.sagacity.sqltoy.model.inner.DataSetResult in project sagacity-sqltoy by chenrenfei.
the class UnPivotListTest method testUnpivotMiddle.
@Test
public void testUnpivotMiddle() {
// |------- 1月-------|------- 2月 ------|------ 3月--------|
// |交易笔 | 金额 | 收入 |交易笔 | 金额 | 收入 |交易笔 | 金额 | 收入 |
Object[][] values = { { "5月", "香蕉", 2000, 20000, "好" }, { "5月", "苹果", 1900, 38999, "好" }, { "4月", "香蕉", 1800, 21000, "好" }, { "4月", "苹果", 1800, 400000, "好" } };
List result = CollectionUtil.arrayToDeepList(values);
UnpivotModel unpivotModel = new UnpivotModel();
unpivotModel.setColumnsToRows(new String[] { "quantity:数量,AMT:金额" });
unpivotModel.setNewColumnsLabels(new String[] { "indexName", "indexValue" });
DataSetResult resultModel = new DataSetResult();
resultModel.setLabelNames(new String[] { "month", "fruitName", "quantity", "AMT", "greet" });
resultModel.setLabelTypes(new String[] { "string", "string", "decimal", "decimal", "string" });
LabelIndexModel labelIndexMap = new LabelIndexModel();
labelIndexMap.put("month", 0);
labelIndexMap.put("fruitname", 1);
labelIndexMap.put("quantity", 2);
labelIndexMap.put("amt", 3);
List value = UnpivotList.process(unpivotModel, resultModel, labelIndexMap, result);
for (int i = 0; i < value.size(); i++) {
System.out.println(JSON.toJSONString(result.get(i)));
}
}
use of org.sagacity.sqltoy.model.inner.DataSetResult in project sagacity-sqltoy by chenrenfei.
the class UnPivotListTest method testUnpivot.
@Test
public void testUnpivot() {
// |------- 1月-------|------- 2月 ------|------ 3月--------|
// |交易笔 | 金额 | 收入 |交易笔 | 金额 | 收入 |交易笔 | 金额 | 收入 |
Object[][] values = { { "5月", "香蕉", 2000, 20000 }, { "5月", "苹果", 1900, 38999 }, { "4月", "香蕉", 1800, 21000 }, { "4月", "苹果", 1800, 400000 } };
List result = CollectionUtil.arrayToDeepList(values);
UnpivotModel unpivotModel = new UnpivotModel();
unpivotModel.setColumnsToRows(new String[] { "quantity:数量,AMT:金额" });
unpivotModel.setNewColumnsLabels(new String[] { "indexName", "indexValue" });
DataSetResult resultModel = new DataSetResult();
resultModel.setLabelNames(new String[] { "month", "fruitName", "quantity", "AMT" });
resultModel.setLabelTypes(new String[] { "string", "string", "decimal", "decimal" });
LabelIndexModel labelIndexMap = new LabelIndexModel();
labelIndexMap.put("month", 0);
labelIndexMap.put("fruitname", 1);
labelIndexMap.put("quantity", 2);
labelIndexMap.put("amt", 3);
List value = UnpivotList.process(unpivotModel, resultModel, labelIndexMap, result);
for (int i = 0; i < value.size(); i++) {
System.out.println(JSON.toJSONString(result.get(i)));
}
}
use of org.sagacity.sqltoy.model.inner.DataSetResult 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.model.inner.DataSetResult in project sagacity-sqltoy by chenrenfei.
the class ElasticSearchUtils method executeQuery.
/**
* @todo 执行实际查询处理
* @param sqlToyContext
* @param sqlToyConfig
* @param sql
* @param resultClass
* @param humpMapLabel
* @return
* @throws Exception
*/
public static DataSetResult executeQuery(SqlToyContext sqlToyContext, SqlToyConfig sqlToyConfig, String sql, Class resultClass, boolean humpMapLabel) throws Exception {
NoSqlConfigModel noSqlModel = sqlToyConfig.getNoSqlConfigModel();
ElasticEndpoint esConfig = sqlToyContext.getElasticEndpoint(noSqlModel.getEndpoint());
// 原生sql支持(7.5.1 还未支持分页)
boolean nativeSql = (esConfig.isNativeSql() && noSqlModel.isSqlMode());
// 执行请求并返回json结果
JSONObject json = HttpClientUtils.doPost(sqlToyContext, noSqlModel, esConfig, sql);
if (json == null || json.isEmpty()) {
return new DataSetResult();
}
String[] fields = noSqlModel.getFields();
if (fields == null) {
if (json.containsKey("columns")) {
JSONArray cols = json.getJSONArray("columns");
fields = new String[cols.size()];
int index = 0;
for (Object col : cols) {
fields[index] = ((JSONObject) col).getString("name");
index++;
}
} else if (resultClass != null) {
Class superClass = resultClass.getSuperclass();
if (!resultClass.equals(ArrayList.class) && !resultClass.equals(List.class) && !resultClass.equals(Collection.class) && !resultClass.equals(HashMap.class) && !resultClass.equals(ConcurrentHashMap.class) && !resultClass.equals(Map.class) && !HashMap.class.equals(superClass) && !Map.class.equals(superClass) && !LinkedHashMap.class.equals(superClass) && !ConcurrentHashMap.class.equals(superClass)) {
fields = BeanUtil.matchSetMethodNames(resultClass);
}
}
}
DataSetResult resultSet = null;
if (nativeSql) {
resultSet = extractSqlFieldValue(sqlToyContext, sqlToyConfig, json, fields);
} else {
resultSet = 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, true, false, null, null));
return resultSet;
}
Aggregations