Search in sources :

Example 6 with NoSqlConfigModel

use of org.sagacity.sqltoy.config.model.NoSqlConfigModel in project sagacity-sqltoy by chenrenfei.

the class Mongo method find.

/**
 * @todo 集合记录查询
 * @return
 */
public List<?> find() {
    QueryExecutor queryExecutor = build();
    SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(sql, SqlType.search, "");
    NoSqlConfigModel noSqlModel = sqlToyConfig.getNoSqlConfigModel();
    if (noSqlModel == null || noSqlModel.getCollection() == null || noSqlModel.getFields() == null) {
        throw new IllegalArgumentException(ERROR_MESSAGE);
    }
    try {
        QueryExecutorExtend extend = queryExecutor.getInnerModel();
        // 最后的执行语句
        String realMql = MongoElasticUtils.wrapMql(sqlToyConfig, extend.getParamsName(sqlToyConfig), extend.getParamsValue(sqlToyContext, sqlToyConfig));
        // 聚合查询
        if (noSqlModel.isHasAggs()) {
            return aggregate(getMongoTemplate(), sqlToyConfig, realMql, (Class) extend.resultType, extend.humpMapLabel);
        }
        return findTop(getMongoTemplate(), sqlToyConfig, null, realMql, (Class) extend.resultType, extend.humpMapLabel);
    } catch (Exception e) {
        e.printStackTrace();
        throw new DataAccessException(e);
    }
}
Also used : NoSqlConfigModel(org.sagacity.sqltoy.config.model.NoSqlConfigModel) QueryExecutor(org.sagacity.sqltoy.model.QueryExecutor) SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig) QueryExecutorExtend(org.sagacity.sqltoy.model.inner.QueryExecutorExtend) DataAccessException(org.sagacity.sqltoy.exception.DataAccessException) DataAccessException(org.sagacity.sqltoy.exception.DataAccessException)

Example 7 with NoSqlConfigModel

use of org.sagacity.sqltoy.config.model.NoSqlConfigModel 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)

Example 8 with NoSqlConfigModel

use of org.sagacity.sqltoy.config.model.NoSqlConfigModel in project sagacity-sqltoy by chenrenfei.

the class ElasticSearchUtils method executeQuery.

/**
 * @todo 执行实际查询处理
 * @param sqlToyContext
 * @param sqlToyConfig
 * @param noSqlModel
 * @param jsonQuery
 * @param resultClass
 * @return
 * @throws Exception
 */
public static DataSetResult executeQuery(SqlToyContext sqlToyContext, SqlToyConfig sqlToyConfig, String sql, String resultClass) throws Exception {
    NoSqlConfigModel noSqlModel = sqlToyConfig.getNoSqlConfigModel();
    // 执行请求并返回json结果
    JSONObject json = HttpClientUtils.doPost(sqlToyContext, noSqlModel, sql);
    if (json == null || json.isEmpty()) {
        return new DataSetResult();
    }
    String[] fields = noSqlModel.getFields();
    if (fields == null && resultClass != null) {
        if (!CollectionUtil.any(resultClass.toLowerCase(), new String[] { "map", "hashmap", "linkedhashmap", "linkedmap" }, false)) {
            fields = BeanUtil.matchSetMethodNames(Class.forName(resultClass));
        }
    }
    DataSetResult resultSet = 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;
}
Also used : NoSqlConfigModel(org.sagacity.sqltoy.config.model.NoSqlConfigModel) JSONObject(com.alibaba.fastjson.JSONObject) DataSetResult(org.sagacity.sqltoy.model.DataSetResult)

Example 9 with NoSqlConfigModel

use of org.sagacity.sqltoy.config.model.NoSqlConfigModel in project sagacity-sqltoy by chenrenfei.

the class SqlXMLConfigParse method parseNoSql.

/**
 * @todo 解析nosql的相关配置
 * @param sqlToyConfig
 * @param sqlElt
 */
private static void parseNoSql(SqlToyConfig sqlToyConfig, Element sqlElt) {
    NoSqlConfigModel noSqlConfig = new NoSqlConfigModel();
    if (sqlElt.attribute("collection") != null)
        noSqlConfig.setCollection(sqlElt.attributeValue("collection"));
    if (sqlElt.attribute("mongo-factory") != null)
        noSqlConfig.setMongoFactory(sqlElt.attributeValue("mongo-factory"));
    // url应该是一个变量如:${es_url}
    if (sqlElt.attribute("url") != null)
        noSqlConfig.setUrl(SqlToyConstants.replaceParams(sqlElt.attributeValue("url")));
    // 索引
    if (sqlElt.attribute("index") != null)
        noSqlConfig.setIndex(sqlElt.attributeValue("index"));
    if (sqlElt.attribute("type") != null)
        noSqlConfig.setType(sqlElt.attributeValue("type"));
    // 请求超时时间(单位毫秒)
    if (sqlElt.attribute("request-timeout") != null)
        noSqlConfig.setRequestTimeout(Integer.parseInt(sqlElt.attributeValue("request-timeout")));
    // 连接超时时间(单位毫秒)
    if (sqlElt.attribute("connection-timeout") != null)
        noSqlConfig.setConnectTimeout(Integer.parseInt(sqlElt.attributeValue("connection-timeout")));
    // 整个请求超时时长(毫秒)
    if (sqlElt.attribute("socket-timeout") != null)
        noSqlConfig.setSocketTimeout(Integer.parseInt(sqlElt.attributeValue("socket-timeout")));
    // url请求字符集类型
    if (sqlElt.attribute("charset") != null)
        noSqlConfig.setCharset(sqlElt.attributeValue("charset"));
    // fields
    if (sqlElt.attribute("fields") != null) {
        if (StringUtil.isNotBlank(sqlElt.attributeValue("fields")))
            noSqlConfig.setFields(trimParams(sqlElt.attributeValue("fields").split(",")));
    } else if (sqlElt.element("fields") != null)
        noSqlConfig.setFields(trimParams(sqlElt.elementTextTrim("fields").split(",")));
    // valueRoot
    if (sqlElt.attribute("value-root") != null)
        noSqlConfig.setValueRoot(trimParams(sqlElt.attributeValue("value-root").split(",")));
    else if (sqlElt.attribute("value-path") != null)
        noSqlConfig.setValueRoot(trimParams(sqlElt.attributeValue("value-path").split(",")));
    // 是否有聚合查询
    if (sqlElt.getName().equalsIgnoreCase("eql")) {
        if (sqlElt.attribute("aggregate") != null) {
            noSqlConfig.setHasAggs(Boolean.parseBoolean(sqlElt.attributeValue("aggregate")));
        } else {
            noSqlConfig.setHasAggs(StringUtil.matches(sqlToyConfig.getSql(), ES_AGGS_PATTERN));
        }
        // 判断查询语句的模式是否sql模式
        if (StringUtil.matches(sqlToyConfig.getSql(), "(?i)\\s*select\\s*") && sqlToyConfig.getSql().toLowerCase().indexOf("from") > 0) {
            noSqlConfig.setSqlMode(true);
            // sql模式下存在group by 则判定为聚合查询
            if (StringUtil.matches(sqlToyConfig.getSql(), GROUP_BY_PATTERN))
                noSqlConfig.setHasAggs(true);
        }
    } else if (sqlElt.getName().equalsIgnoreCase("mql")) {
        if (sqlElt.attribute("aggregate") != null) {
            noSqlConfig.setHasAggs(Boolean.parseBoolean(sqlElt.attributeValue("aggregate")));
        } else
            noSqlConfig.setHasAggs(StringUtil.matches(sqlToyConfig.getSql(), MONGO_AGGS_PATTERN));
    }
    sqlToyConfig.setNoSqlConfigModel(noSqlConfig);
    // nosql参数解析模式不同于sql
    if (!noSqlConfig.isSqlMode())
        sqlToyConfig.setParamsName(SqlConfigParseUtils.getNoSqlParamsName(sqlToyConfig.getSql(), true));
}
Also used : NoSqlConfigModel(org.sagacity.sqltoy.config.model.NoSqlConfigModel)

Example 10 with NoSqlConfigModel

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

Aggregations

NoSqlConfigModel (org.sagacity.sqltoy.config.model.NoSqlConfigModel)14 JSONObject (com.alibaba.fastjson.JSONObject)7 JSONArray (com.alibaba.fastjson.JSONArray)5 SqlToyConfig (org.sagacity.sqltoy.config.model.SqlToyConfig)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 ElasticEndpoint (org.sagacity.sqltoy.config.model.ElasticEndpoint)4 DataAccessException (org.sagacity.sqltoy.exception.DataAccessException)4 QueryExecutor (org.sagacity.sqltoy.model.QueryExecutor)4 DataSetResult (org.sagacity.sqltoy.model.DataSetResult)3 DataSetResult (org.sagacity.sqltoy.model.inner.DataSetResult)3 QueryExecutorExtend (org.sagacity.sqltoy.model.inner.QueryExecutorExtend)3 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 File (java.io.File)1 Array (java.lang.reflect.Array)1 Map (java.util.Map)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 NoSqlFieldsModel (org.sagacity.sqltoy.config.model.NoSqlFieldsModel)1