Search in sources :

Example 1 with DataSetResult

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)));
    }
}
Also used : UnpivotModel(org.sagacity.sqltoy.config.model.UnpivotModel) UnpivotList(org.sagacity.sqltoy.plugins.calculator.UnpivotList) List(java.util.List) LabelIndexModel(org.sagacity.sqltoy.config.model.LabelIndexModel) DataSetResult(org.sagacity.sqltoy.model.inner.DataSetResult) Test(org.junit.jupiter.api.Test)

Example 2 with DataSetResult

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)));
    }
}
Also used : UnpivotModel(org.sagacity.sqltoy.config.model.UnpivotModel) UnpivotList(org.sagacity.sqltoy.plugins.calculator.UnpivotList) List(java.util.List) LabelIndexModel(org.sagacity.sqltoy.config.model.LabelIndexModel) DataSetResult(org.sagacity.sqltoy.model.inner.DataSetResult) Test(org.junit.jupiter.api.Test)

Example 3 with DataSetResult

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)));
    }
}
Also used : UnpivotModel(org.sagacity.sqltoy.config.model.UnpivotModel) UnpivotList(org.sagacity.sqltoy.plugins.calculator.UnpivotList) List(java.util.List) LabelIndexModel(org.sagacity.sqltoy.config.model.LabelIndexModel) DataSetResult(org.sagacity.sqltoy.model.inner.DataSetResult) Test(org.junit.jupiter.api.Test)

Example 4 with DataSetResult

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);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) NoSqlFieldsModel(org.sagacity.sqltoy.config.model.NoSqlFieldsModel) DataSetResult(org.sagacity.sqltoy.model.inner.DataSetResult) Document(org.bson.Document)

Example 5 with DataSetResult

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;
}
Also used : NoSqlConfigModel(org.sagacity.sqltoy.config.model.NoSqlConfigModel) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) JSONArray(com.alibaba.fastjson.JSONArray) ArrayList(java.util.ArrayList) ElasticEndpoint(org.sagacity.sqltoy.config.model.ElasticEndpoint) DataSetResult(org.sagacity.sqltoy.model.inner.DataSetResult) ElasticEndpoint(org.sagacity.sqltoy.config.model.ElasticEndpoint) JSONObject(com.alibaba.fastjson.JSONObject) JSONObject(com.alibaba.fastjson.JSONObject) ArrayList(java.util.ArrayList) List(java.util.List) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

DataSetResult (org.sagacity.sqltoy.model.inner.DataSetResult)14 List (java.util.List)9 JSONObject (com.alibaba.fastjson.JSONObject)8 JSONArray (com.alibaba.fastjson.JSONArray)6 ArrayList (java.util.ArrayList)6 NoSqlFieldsModel (org.sagacity.sqltoy.config.model.NoSqlFieldsModel)5 ElasticEndpoint (org.sagacity.sqltoy.config.model.ElasticEndpoint)4 QueryExecutorExtend (org.sagacity.sqltoy.model.inner.QueryExecutorExtend)4 HashMap (java.util.HashMap)3 Test (org.junit.jupiter.api.Test)3 LabelIndexModel (org.sagacity.sqltoy.config.model.LabelIndexModel)3 NoSqlConfigModel (org.sagacity.sqltoy.config.model.NoSqlConfigModel)3 UnpivotModel (org.sagacity.sqltoy.config.model.UnpivotModel)3 UnpivotList (org.sagacity.sqltoy.plugins.calculator.UnpivotList)3 LinkedHashMap (java.util.LinkedHashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Page (org.sagacity.sqltoy.model.Page)2 Array (java.lang.reflect.Array)1 Map (java.util.Map)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1