Search in sources :

Example 1 with GremlinResult

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

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

the class GremlinQueryController method execute.

@PostMapping
public GremlinResult execute(@PathVariable("connId") int connId, @RequestBody GremlinQuery query) {
    this.checkParamsValid(query);
    Date createTime = HubbleUtil.nowDate();
    // Insert execute history
    ExecuteStatus status = ExecuteStatus.RUNNING;
    ExecuteHistory history;
    history = new ExecuteHistory(null, connId, 0L, ExecuteType.GREMLIN, query.getContent(), status, AsyncTaskStatus.UNKNOWN, -1L, createTime);
    this.historyService.save(history);
    StopWatch timer = StopWatch.createStarted();
    try {
        GremlinResult result = this.queryService.executeQuery(connId, query);
        status = ExecuteStatus.SUCCESS;
        return result;
    } catch (Throwable e) {
        status = ExecuteStatus.FAILED;
        throw e;
    } finally {
        timer.stop();
        long duration = timer.getTime(TimeUnit.MILLISECONDS);
        history.setStatus(status);
        history.setDuration(duration);
        this.historyService.update(history);
    }
}
Also used : ExecuteHistory(com.baidu.hugegraph.entity.query.ExecuteHistory) GremlinResult(com.baidu.hugegraph.entity.query.GremlinResult) ExecuteStatus(com.baidu.hugegraph.entity.enums.ExecuteStatus) Date(java.util.Date) StopWatch(org.apache.commons.lang3.time.StopWatch) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Aggregations

GremlinResult (com.baidu.hugegraph.entity.query.GremlinResult)2 HugeClient (com.baidu.hugegraph.driver.HugeClient)1 ExecuteStatus (com.baidu.hugegraph.entity.enums.ExecuteStatus)1 ExecuteHistory (com.baidu.hugegraph.entity.query.ExecuteHistory)1 GraphView (com.baidu.hugegraph.entity.query.GraphView)1 TypedResult (com.baidu.hugegraph.entity.query.TypedResult)1 Edge (com.baidu.hugegraph.structure.graph.Edge)1 Path (com.baidu.hugegraph.structure.graph.Path)1 Vertex (com.baidu.hugegraph.structure.graph.Vertex)1 Result (com.baidu.hugegraph.structure.gremlin.Result)1 ResultSet (com.baidu.hugegraph.structure.gremlin.ResultSet)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 StopWatch (org.apache.commons.lang3.time.StopWatch)1 PostMapping (org.springframework.web.bind.annotation.PostMapping)1