Search in sources :

Example 1 with TypedResult

use of com.baidu.hugegraph.entity.query.TypedResult in project incubator-hugegraph-toolchain by apache.

the class GremlinQueryService method parseResults.

private TypedResult parseResults(ResultSet resultSet) {
    if (resultSet == null) {
        return new TypedResult(Type.EMPTY, null);
    }
    Iterator<Result> iter = resultSet.iterator();
    if (!iter.hasNext()) {
        return new TypedResult(Type.EMPTY, null);
    }
    Map<Type, Integer> typeVotes = new HashMap<>();
    List<Object> typedData = new ArrayList<>(resultSet.size());
    while (iter.hasNext()) {
        Result result = iter.next();
        if (result == null) {
            // NOTE: null value doesn't vote
            continue;
        }
        Object object = result.getObject();
        Type type;
        if (object instanceof Vertex) {
            type = Type.VERTEX;
        } else if (object instanceof Edge) {
            type = Type.EDGE;
        } else if (object instanceof Path) {
            type = Type.PATH;
        } else {
            type = Type.GENERAL;
        }
        typeVotes.compute(type, (k, v) -> v == null ? 1 : v + 1);
        typedData.add(object);
    }
    Type type;
    if (typeVotes.isEmpty()) {
        type = Type.EMPTY;
    } else {
        // Find the key with max value
        type = Collections.max(typeVotes.entrySet(), Comparator.comparingInt(Map.Entry::getValue)).getKey();
    }
    return new TypedResult(type, typedData);
}
Also used : Path(com.baidu.hugegraph.structure.graph.Path) Vertex(com.baidu.hugegraph.structure.graph.Vertex) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TypedResult(com.baidu.hugegraph.entity.query.TypedResult) Result(com.baidu.hugegraph.structure.gremlin.Result) TypedResult(com.baidu.hugegraph.entity.query.TypedResult) GremlinResult(com.baidu.hugegraph.entity.query.GremlinResult) Type(com.baidu.hugegraph.entity.query.GremlinResult.Type) Edge(com.baidu.hugegraph.structure.graph.Edge) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 2 with TypedResult

use of com.baidu.hugegraph.entity.query.TypedResult in project incubator-hugegraph-toolchain by apache.

the class GremlinQueryService method verticesOfEdge.

private Map<Object, Vertex> verticesOfEdge(TypedResult result, Map<String, Edge> edges, HugeClient client) {
    int batchSize = this.config.get(HubbleOptions.GREMLIN_BATCH_QUERY_IDS);
    Set<Object> vertexIds = new HashSet<>(edges.size() * 2);
    edges.values().forEach(edge -> {
        vertexIds.add(edge.sourceId());
        vertexIds.add(edge.targetId());
    });
    Map<Object, Vertex> vertices = new HashMap<>(vertexIds.size());
    Iterables.partition(vertexIds, batchSize).forEach(batch -> {
        List<String> escapedIds = batch.stream().map(GremlinUtil::escapeId).collect(Collectors.toList());
        String ids = StringUtils.join(escapedIds, ",");
        String gremlin = String.format("g.V(%s)", ids);
        ResultSet resultSet = client.gremlin().gremlin(gremlin).execute();
        for (Iterator<Result> iter = resultSet.iterator(); iter.hasNext(); ) {
            Vertex vertex = iter.next().getVertex();
            vertices.put(vertex.id(), vertex);
        }
    });
    return vertices;
}
Also used : Vertex(com.baidu.hugegraph.structure.graph.Vertex) HashMap(java.util.HashMap) ResultSet(com.baidu.hugegraph.structure.gremlin.ResultSet) HashSet(java.util.HashSet) Result(com.baidu.hugegraph.structure.gremlin.Result) TypedResult(com.baidu.hugegraph.entity.query.TypedResult) GremlinResult(com.baidu.hugegraph.entity.query.GremlinResult)

Example 3 with TypedResult

use of com.baidu.hugegraph.entity.query.TypedResult in project incubator-hugegraph-toolchain by apache.

the class GremlinQueryService method edgesOfVertex.

private Map<String, Edge> edgesOfVertex(TypedResult result, Map<Object, Vertex> vertices, HugeClient client) {
    final HugeConfig config = this.config;
    int batchSize = config.get(HubbleOptions.GREMLIN_BATCH_QUERY_IDS);
    int edgeLimit = config.get(HubbleOptions.GREMLIN_EDGES_TOTAL_LIMIT);
    int degreeLimit = config.get(HubbleOptions.GREMLIN_VERTEX_DEGREE_LIMIT);
    Set<Object> vertexIds = vertices.keySet();
    Map<String, Edge> edges = new HashMap<>(vertexIds.size());
    Iterables.partition(vertexIds, batchSize).forEach(batch -> {
        List<String> escapedIds = batch.stream().map(GremlinUtil::escapeId).collect(Collectors.toList());
        String ids = StringUtils.join(escapedIds, ",");
        // Any better way to find two vertices has linked?
        String gremlin;
        if (result.getType().isPath()) {
            // If result type is path, the vertices count not too much in theory
            gremlin = String.format("g.V(%s).bothE().local(limit(%s)).dedup()", ids, degreeLimit);
        } else {
            gremlin = String.format("g.V(%s).bothE().dedup().limit(%s)", ids, edgeLimit);
        }
        ResultSet resultSet = client.gremlin().gremlin(gremlin).execute();
        // The edges count for per vertex
        Map<Object, Integer> degrees = new HashMap<>(resultSet.size());
        for (Iterator<Result> iter = resultSet.iterator(); iter.hasNext(); ) {
            Edge edge = iter.next().getEdge();
            Object source = edge.sourceId();
            Object target = edge.targetId();
            // only add the interconnected edges of the found vertices
            if (!vertexIds.contains(source) || !vertexIds.contains(target)) {
                continue;
            }
            edges.put(edge.id(), edge);
            if (edges.size() >= edgeLimit) {
                break;
            }
            int deg = degrees.compute(source, (k, v) -> v == null ? 1 : v + 1);
            if (deg >= degreeLimit) {
                break;
            }
            deg = degrees.compute(target, (k, v) -> v == null ? 1 : v + 1);
            if (deg >= degreeLimit) {
                break;
            }
        }
    });
    return edges;
}
Also used : Direction(com.baidu.hugegraph.structure.constant.Direction) Iterables(com.google.common.collect.Iterables) Type(com.baidu.hugegraph.entity.query.GremlinResult.Type) ServerException(com.baidu.hugegraph.exception.ServerException) ExternalException(com.baidu.hugegraph.exception.ExternalException) AdjacentQuery(com.baidu.hugegraph.entity.query.AdjacentQuery) IdStrategy(com.baidu.hugegraph.structure.constant.IdStrategy) VertexLabelEntity(com.baidu.hugegraph.entity.schema.VertexLabelEntity) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) Ex(com.baidu.hugegraph.util.Ex) StringUtils(org.apache.commons.lang3.StringUtils) ResultSet(com.baidu.hugegraph.structure.gremlin.ResultSet) ArrayList(java.util.ArrayList) JsonView(com.baidu.hugegraph.entity.query.JsonView) HashSet(java.util.HashSet) GremlinUtil(com.baidu.hugegraph.util.GremlinUtil) TableView(com.baidu.hugegraph.entity.query.TableView) Service(org.springframework.stereotype.Service) Map(java.util.Map) ClientException(com.baidu.hugegraph.rest.ClientException) Path(com.baidu.hugegraph.structure.graph.Path) HugeClient(com.baidu.hugegraph.driver.HugeClient) GremlinRequest(com.baidu.hugegraph.api.gremlin.GremlinRequest) ImmutableSet(com.google.common.collect.ImmutableSet) Iterator(java.util.Iterator) ImmutableMap(com.google.common.collect.ImmutableMap) GraphView(com.baidu.hugegraph.entity.query.GraphView) Set(java.util.Set) Result(com.baidu.hugegraph.structure.gremlin.Result) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) TypedResult(com.baidu.hugegraph.entity.query.TypedResult) Edge(com.baidu.hugegraph.structure.graph.Edge) IllegalGremlinException(com.baidu.hugegraph.exception.IllegalGremlinException) Vertex(com.baidu.hugegraph.structure.graph.Vertex) HubbleOptions(com.baidu.hugegraph.options.HubbleOptions) VertexLabelService(com.baidu.hugegraph.service.schema.VertexLabelService) List(java.util.List) CollectionUtils(org.springframework.util.CollectionUtils) HugeClientPoolService(com.baidu.hugegraph.service.HugeClientPoolService) Log4j2(lombok.extern.log4j.Log4j2) HugeConfig(com.baidu.hugegraph.config.HugeConfig) Comparator(java.util.Comparator) InternalException(com.baidu.hugegraph.exception.InternalException) Collections(java.util.Collections) GremlinQuery(com.baidu.hugegraph.entity.query.GremlinQuery) GremlinResult(com.baidu.hugegraph.entity.query.GremlinResult) HashMap(java.util.HashMap) HugeConfig(com.baidu.hugegraph.config.HugeConfig) Result(com.baidu.hugegraph.structure.gremlin.Result) TypedResult(com.baidu.hugegraph.entity.query.TypedResult) GremlinResult(com.baidu.hugegraph.entity.query.GremlinResult) ResultSet(com.baidu.hugegraph.structure.gremlin.ResultSet) Edge(com.baidu.hugegraph.structure.graph.Edge)

Example 4 with TypedResult

use of com.baidu.hugegraph.entity.query.TypedResult in project incubator-hugegraph-toolchain by apache.

the class GremlinQueryService method executeQuery.

public GremlinResult executeQuery(int connId, GremlinQuery query) {
    HugeClient client = this.getClient(connId);
    log.debug("The original gremlin ==> {}", query.getContent());
    String gremlin = this.optimize(query.getContent());
    log.debug("The optimized gremlin ==> {}", gremlin);
    // Execute gremlin query
    ResultSet resultSet = this.executeGremlin(gremlin, client);
    // Scan data, vote the result type
    TypedResult typedResult = this.parseResults(resultSet);
    // Build json view
    JsonView jsonView = new JsonView(typedResult.getData());
    // Build table view
    TableView tableView = this.buildTableView(typedResult);
    // Build graph view
    GraphView graphView = this.buildGraphView(typedResult, client);
    return GremlinResult.builder().type(typedResult.getType()).jsonView(jsonView).tableView(tableView).graphView(graphView).build();
}
Also used : HugeClient(com.baidu.hugegraph.driver.HugeClient) ResultSet(com.baidu.hugegraph.structure.gremlin.ResultSet) JsonView(com.baidu.hugegraph.entity.query.JsonView) TypedResult(com.baidu.hugegraph.entity.query.TypedResult) GraphView(com.baidu.hugegraph.entity.query.GraphView) TableView(com.baidu.hugegraph.entity.query.TableView)

Aggregations

TypedResult (com.baidu.hugegraph.entity.query.TypedResult)4 GremlinResult (com.baidu.hugegraph.entity.query.GremlinResult)3 Vertex (com.baidu.hugegraph.structure.graph.Vertex)3 Result (com.baidu.hugegraph.structure.gremlin.Result)3 ResultSet (com.baidu.hugegraph.structure.gremlin.ResultSet)3 HashMap (java.util.HashMap)3 HugeClient (com.baidu.hugegraph.driver.HugeClient)2 GraphView (com.baidu.hugegraph.entity.query.GraphView)2 Type (com.baidu.hugegraph.entity.query.GremlinResult.Type)2 JsonView (com.baidu.hugegraph.entity.query.JsonView)2 TableView (com.baidu.hugegraph.entity.query.TableView)2 Edge (com.baidu.hugegraph.structure.graph.Edge)2 Path (com.baidu.hugegraph.structure.graph.Path)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 GremlinRequest (com.baidu.hugegraph.api.gremlin.GremlinRequest)1 HugeConfig (com.baidu.hugegraph.config.HugeConfig)1 AdjacentQuery (com.baidu.hugegraph.entity.query.AdjacentQuery)1