Search in sources :

Example 1 with GraphView

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

the class GremlinQueryService method expandVertex.

public GremlinResult expandVertex(int connId, AdjacentQuery query) {
    HugeClient client = this.getClient(connId);
    // Build gremlin query
    String gremlin = this.buildGremlinQuery(connId, query);
    log.debug("expand vertex gremlin ==> {}", gremlin);
    // Execute gremlin query
    ResultSet resultSet = this.executeGremlin(gremlin, client);
    List<Vertex> vertices = new ArrayList<>(resultSet.size());
    List<Edge> edges = new ArrayList<>(resultSet.size());
    for (Iterator<Result> iter = resultSet.iterator(); iter.hasNext(); ) {
        Path path = iter.next().getPath();
        List<Object> objects = path.objects();
        assert objects.size() == 3;
        Edge edge = (Edge) objects.get(1);
        Vertex vertex = (Vertex) objects.get(2);
        // Filter vertices and edges that existed in query
        if (query.retainEdge(edge)) {
            edges.add(edge);
        }
        if (query.retainVertex(vertex)) {
            vertices.add(vertex);
        }
    }
    // Build graph view
    GraphView graphView = new GraphView(vertices, edges);
    return GremlinResult.builder().type(Type.PATH).graphView(graphView).build();
}
Also used : Path(com.baidu.hugegraph.structure.graph.Path) Vertex(com.baidu.hugegraph.structure.graph.Vertex) HugeClient(com.baidu.hugegraph.driver.HugeClient) ArrayList(java.util.ArrayList) GraphView(com.baidu.hugegraph.entity.query.GraphView) 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 2 with GraphView

use of com.baidu.hugegraph.entity.query.GraphView 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)

Example 3 with GraphView

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

the class GremlinQueryService method buildGraphView.

private GraphView buildGraphView(TypedResult result, HugeClient client) {
    List<Object> data = result.getData();
    if (!result.getType().isGraph() || CollectionUtils.isEmpty(data)) {
        return GraphView.EMPTY;
    }
    Map<Object, Vertex> vertices = new HashMap<>();
    Map<String, Edge> edges = new HashMap<>();
    for (Object object : data) {
        if (object instanceof Vertex) {
            Vertex vertex = (Vertex) object;
            vertices.put(vertex.id(), vertex);
        } else if (object instanceof Edge) {
            Edge edge = (Edge) object;
            edges.put(edge.id(), edge);
        } else if (object instanceof Path) {
            List<Object> elements = ((Path) object).objects();
            for (Object element : elements) {
                if (element instanceof Vertex) {
                    Vertex vertex = (Vertex) element;
                    vertices.put(vertex.id(), vertex);
                } else if (element instanceof Edge) {
                    Edge edge = (Edge) element;
                    edges.put(edge.id(), edge);
                } else {
                    return GraphView.EMPTY;
                }
            }
        }
    }
    if (!edges.isEmpty()) {
        if (vertices.isEmpty()) {
            vertices = this.verticesOfEdge(result, edges, client);
        } else {
            // TODO: reduce the number of requests
            vertices.putAll(this.verticesOfEdge(result, edges, client));
        }
    } else {
        if (!vertices.isEmpty()) {
            edges = this.edgesOfVertex(result, vertices, client);
        }
    }
    if (!edges.isEmpty()) {
        Ex.check(!vertices.isEmpty(), "gremlin.edges.linked-vertex.not-exist");
    }
    return new GraphView(vertices.values(), edges.values());
}
Also used : Path(com.baidu.hugegraph.structure.graph.Path) Vertex(com.baidu.hugegraph.structure.graph.Vertex) HashMap(java.util.HashMap) Edge(com.baidu.hugegraph.structure.graph.Edge) GraphView(com.baidu.hugegraph.entity.query.GraphView)

Example 4 with GraphView

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

the class OltpAlgoService method shortestPath.

public GremlinResult shortestPath(int connId, ShortestPath body) {
    HugeClient client = this.getClient(connId);
    TraverserManager traverser = client.traverser();
    Path result = traverser.shortestPath(body.getSource(), body.getTarget(), body.getDirection(), body.getLabel(), body.getMaxDepth(), body.getMaxDegree(), body.getSkipDegree(), body.getCapacity());
    JsonView jsonView = new JsonView();
    jsonView.setData(result.objects());
    Date createTime = HubbleUtil.nowDate();
    TableView tableView = this.buildPathTableView(result);
    GraphView graphView = this.buildPathGraphView(result);
    // Insert execute history
    ExecuteStatus status = ExecuteStatus.SUCCESS;
    ExecuteHistory history;
    history = new ExecuteHistory(null, connId, 0L, ExecuteType.ALGORITHM, body.toString(), status, AsyncTaskStatus.UNKNOWN, -1L, createTime);
    this.historyService.save(history);
    return GremlinResult.builder().type(GremlinResult.Type.PATH).jsonView(jsonView).tableView(tableView).graphView(graphView).build();
}
Also used : ShortestPath(com.baidu.hugegraph.entity.algorithm.ShortestPath) Path(com.baidu.hugegraph.structure.graph.Path) HugeClient(com.baidu.hugegraph.driver.HugeClient) ExecuteHistory(com.baidu.hugegraph.entity.query.ExecuteHistory) JsonView(com.baidu.hugegraph.entity.query.JsonView) ExecuteStatus(com.baidu.hugegraph.entity.enums.ExecuteStatus) GraphView(com.baidu.hugegraph.entity.query.GraphView) TraverserManager(com.baidu.hugegraph.driver.TraverserManager) Date(java.util.Date) TableView(com.baidu.hugegraph.entity.query.TableView)

Example 5 with GraphView

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

the class OltpAlgoService method buildPathGraphView.

private GraphView buildPathGraphView(Path result) {
    Map<Object, Vertex> vertices = new HashMap<>();
    Map<String, Edge> edges = new HashMap<>();
    List<Object> elements = result.objects();
    for (Object element : elements) {
        if (element instanceof Vertex) {
            Vertex vertex = (Vertex) element;
            vertices.put(vertex.id(), vertex);
        } else if (element instanceof Edge) {
            Edge edge = (Edge) element;
            edges.put(edge.id(), edge);
        } else {
            return GraphView.EMPTY;
        }
    }
    return new GraphView(vertices.values(), new ArrayList<>());
}
Also used : Vertex(com.baidu.hugegraph.structure.graph.Vertex) HashMap(java.util.HashMap) Edge(com.baidu.hugegraph.structure.graph.Edge) GraphView(com.baidu.hugegraph.entity.query.GraphView)

Aggregations

GraphView (com.baidu.hugegraph.entity.query.GraphView)5 HugeClient (com.baidu.hugegraph.driver.HugeClient)3 Edge (com.baidu.hugegraph.structure.graph.Edge)3 Path (com.baidu.hugegraph.structure.graph.Path)3 Vertex (com.baidu.hugegraph.structure.graph.Vertex)3 JsonView (com.baidu.hugegraph.entity.query.JsonView)2 TableView (com.baidu.hugegraph.entity.query.TableView)2 TypedResult (com.baidu.hugegraph.entity.query.TypedResult)2 ResultSet (com.baidu.hugegraph.structure.gremlin.ResultSet)2 HashMap (java.util.HashMap)2 TraverserManager (com.baidu.hugegraph.driver.TraverserManager)1 ShortestPath (com.baidu.hugegraph.entity.algorithm.ShortestPath)1 ExecuteStatus (com.baidu.hugegraph.entity.enums.ExecuteStatus)1 ExecuteHistory (com.baidu.hugegraph.entity.query.ExecuteHistory)1 GremlinResult (com.baidu.hugegraph.entity.query.GremlinResult)1 Result (com.baidu.hugegraph.structure.gremlin.Result)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1