Search in sources :

Example 1 with MaxGraphException

use of com.alibaba.maxgraph.sdkcommon.exception.MaxGraphException in project GraphScope by alibaba.

the class DataTypeDeserializer method deserialize.

@Override
public DataType deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    // todo: only support List<T> and Set<T> currently.
    String data = p.getValueAsString();
    try {
        if (data.startsWith("LIST<")) {
            DataType ret = new DataType(InternalDataType.LIST);
            ret.setExpression(data.substring("LIST<".length(), data.length() - 1));
            return ret;
        } else if (data.startsWith("S<")) {
            DataType ret = new DataType(InternalDataType.SET);
            ret.setExpression(data.substring(2, data.length() - 1));
            return ret;
        } else if (data.startsWith("M<")) {
        } else {
            DataType ret = new DataType(InternalDataType.valueOf(data.toUpperCase()));
            return ret;
        }
    } catch (MaxGraphException ex) {
        throw new IOException(ex);
    }
    return null;
}
Also used : IOException(java.io.IOException) MaxGraphException(com.alibaba.maxgraph.sdkcommon.exception.MaxGraphException)

Example 2 with MaxGraphException

use of com.alibaba.maxgraph.sdkcommon.exception.MaxGraphException in project GraphScope by alibaba.

the class DefaultGraphSchema method buildSchemaFromJson.

public static GraphSchema buildSchemaFromJson(String schemaJson) {
    JSONObject jsonObject = JSONObject.parseObject(schemaJson);
    Map<String, GraphVertex> vertexList = Maps.newHashMap();
    Map<String, GraphEdge> edgeList = Maps.newHashMap();
    Map<String, Integer> propNameToIdList = Maps.newHashMap();
    JSONArray typeList = jsonObject.getJSONArray("types");
    if (null != typeList) {
        int propId = 1;
        for (int i = 0; i < typeList.size(); i++) {
            JSONObject typeObject = typeList.getJSONObject(i);
            int labelId = typeObject.getInteger("id");
            String label = typeObject.getString("label");
            String type = typeObject.getString("type");
            Map<String, GraphProperty> namePropertyList = Maps.newHashMap();
            List<GraphProperty> propertyList = Lists.newArrayList();
            JSONArray propArray = typeObject.getJSONArray("propertyDefList");
            if (null != propArray) {
                for (int j = 0; j < propArray.size(); j++) {
                    JSONObject propObject = propArray.getJSONObject(j);
                    String propName = propObject.getString("name");
                    Integer currPropId = propObject.getInteger("id");
                    if (null == currPropId) {
                        currPropId = propId++;
                    }
                    String propDataTypeString = propObject.getString("data_type");
                    com.alibaba.maxgraph.sdkcommon.meta.DataType dataType;
                    if (StringUtils.startsWith(propDataTypeString, "LIST")) {
                        dataType = new com.alibaba.maxgraph.sdkcommon.meta.DataType(InternalDataType.LIST);
                        try {
                            dataType.setExpression(StringUtils.removeEnd(StringUtils.removeStart(propDataTypeString, "LIST<"), ">"));
                        } catch (MaxGraphException e) {
                            throw new RuntimeException(e);
                        }
                    } else {
                        dataType = com.alibaba.maxgraph.sdkcommon.meta.DataType.valueOf(propDataTypeString);
                    }
                    GraphProperty property = new DefaultGraphProperty(currPropId, propName, DataType.parseFromDataType(dataType));
                    propertyList.add(property);
                    namePropertyList.put(propName, property);
                    propNameToIdList.put(propName, currPropId);
                }
            } else {
                logger.warn("There's no property def list in " + label);
            }
            if (StringUtils.equals(type, "VERTEX")) {
                List<GraphProperty> primaryPropertyList = Lists.newArrayList();
                JSONArray indexArray = typeObject.getJSONArray("indexes");
                if (indexArray != null) {
                    for (int k = 0; k < indexArray.size(); k++) {
                        JSONObject indexObject = indexArray.getJSONObject(k);
                        JSONArray priNameList = indexObject.getJSONArray("propertyNames");
                        for (int j = 0; j < priNameList.size(); j++) {
                            primaryPropertyList.add(namePropertyList.get(priNameList.getString(j)));
                        }
                    }
                }
                DefaultGraphVertex graphVertex = new DefaultGraphVertex(labelId, label, propertyList, primaryPropertyList);
                vertexList.put(label, graphVertex);
            } else {
                List<EdgeRelation> relationList = Lists.newArrayList();
                JSONArray relationArray = typeObject.getJSONArray("rawRelationShips");
                if (null != relationArray) {
                    for (int k = 0; k < relationArray.size(); k++) {
                        JSONObject relationObject = relationArray.getJSONObject(k);
                        String sourceLabel = relationObject.getString("srcVertexLabel");
                        String targetLabel = relationObject.getString("dstVertexLabel");
                        relationList.add(new DefaultEdgeRelation(vertexList.get(sourceLabel), vertexList.get(targetLabel)));
                    }
                } else {
                    logger.warn("There's no relation def in edge " + label);
                }
                DefaultGraphEdge graphEdge = new DefaultGraphEdge(labelId, label, propertyList, relationList);
                edgeList.put(label, graphEdge);
            }
        }
    } else {
        logger.error("Cant get types field in json[" + schemaJson + "]");
    }
    return new DefaultGraphSchema(vertexList, edgeList, propNameToIdList);
}
Also used : MaxGraphException(com.alibaba.maxgraph.sdkcommon.exception.MaxGraphException) GraphProperty(com.alibaba.maxgraph.compiler.api.schema.GraphProperty) GraphVertex(com.alibaba.maxgraph.compiler.api.schema.GraphVertex) JSONArray(com.alibaba.fastjson.JSONArray) EdgeRelation(com.alibaba.maxgraph.compiler.api.schema.EdgeRelation) JSONObject(com.alibaba.fastjson.JSONObject) GraphEdge(com.alibaba.maxgraph.compiler.api.schema.GraphEdge)

Aggregations

MaxGraphException (com.alibaba.maxgraph.sdkcommon.exception.MaxGraphException)2 JSONArray (com.alibaba.fastjson.JSONArray)1 JSONObject (com.alibaba.fastjson.JSONObject)1 EdgeRelation (com.alibaba.maxgraph.compiler.api.schema.EdgeRelation)1 GraphEdge (com.alibaba.maxgraph.compiler.api.schema.GraphEdge)1 GraphProperty (com.alibaba.maxgraph.compiler.api.schema.GraphProperty)1 GraphVertex (com.alibaba.maxgraph.compiler.api.schema.GraphVertex)1 IOException (java.io.IOException)1