根据JsonSchema生成数据库表名

生成方式

根据JsonSchema的各层级的key,用下划线拼接

    private static final String SCHEMA_TYPE = "type";
    private static final String SCHEMA_TYPE_OBJECT = "object";
    private static final String SCHEMA_TYPE_ARRAY = "array";
    private static final String SCHEMA_TYPE_NULL = "null";
    private static final String SCHEMA_TYPE_INTEGER = "integer";
    private static final String SCHEMA_TYPE_NUMBER = "number";
    private static final String SCHEMA_PROPERTIES = "properties";
    private static final String SCHEMA_ITEMS = "items";
    private static final String SCHEMA_REQUIRED = "required";
    private static final Map typeConversion = new HashMap<>(16);
    static {
        typeConversion.put("string", " String ");
        typeConversion.put("integer", " Int64 ");
        typeConversion.put("boolean", " String ");
        typeConversion.put("number", " Decimal64(8) ");
    }


 /**
     * 查询JSON文件需要创建的所有的表
     * @param source 数据源命名 用于拼接根节点表名
     * @param scene 数据来源场景 用于拼接根节点表名
     * @param schema jsonSchema
     * @return
     */
    public static Set jsonSchema2CreateTableName(String source, String scene, String schema) {
        JSONObject schemaNode = JSON.parseObject(schema);
        String nodeType = schemaNode.getString(SCHEMA_TYPE);
        String rootTableName = source + "_" + scene;
        if (SCHEMA_TYPE_OBJECT.equals(nodeType)) {
            return jsonObj2CreateTableName(rootTableName, schemaNode.getJSONObject(SCHEMA_PROPERTIES));
        } else if (SCHEMA_TYPE_ARRAY.equals(nodeType)) {
            return jsonArray2CreateTableName(rootTableName, schemaNode.getJSONObject(SCHEMA_ITEMS));
        }
        throw new BusinessErrException("Unable to resolve JSONSchema!");
    }

    private static Set jsonObj2CreateTableName(String tableName, JSONObject properties) {
        if (properties == null) {
            return new HashSet<>();
        }
        Set result = new HashSet<>();
        Set keySet = properties.keySet();
        for (String key : keySet) {
            JSONObject typeObj = properties.getJSONObject(key);
            String type = typeObj.getString(SCHEMA_TYPE);
            if (StringUtils.isEmpty(type)) {
                continue;
            }
            switch (type) {
                case SCHEMA_TYPE_OBJECT:
                    result.addAll(jsonObj2CreateTableName(tableName + "_" + key, typeObj.getJSONObject(SCHEMA_PROPERTIES)));
                    continue;
                case SCHEMA_TYPE_ARRAY:
                    result.addAll(jsonArray2CreateTableName(tableName + "_" + key, typeObj.getJSONObject(SCHEMA_ITEMS)));
            }
        }
        result.add(tableName);
        return result;
    }


    private static Set jsonArray2CreateTableName(String tableName, JSONObject items) {
        if (items == null) {
            return new HashSet<>();
        }
        String type = items.getString(SCHEMA_TYPE);
        if (StringUtils.isEmpty(type)) {
            return new HashSet<>();
        }
        switch (type) {
            case SCHEMA_TYPE_ARRAY:
                return jsonArray2CreateTableName(tableName, items.getJSONObject(SCHEMA_ITEMS));
            case SCHEMA_TYPE_OBJECT:
                return jsonObj2CreateTableName(tableName, items.getJSONObject(SCHEMA_PROPERTIES));
            case SCHEMA_TYPE_NULL:
                return new HashSet<>();
            default:
                throw new BusinessErrException("Unrecognized type:" + type);
        }
    }

 

你可能感兴趣的:(JAVA,数据库,数据库,java,大数据,json)