Search in sources :

Example 1 with TableView

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

the class GremlinQueryService method buildTableView.

private TableView buildTableView(TypedResult typedResult) {
    List<Object> data = typedResult.getData();
    if (CollectionUtils.isEmpty(data)) {
        return TableView.EMPTY;
    }
    switch(typedResult.getType()) {
        case EMPTY:
            return TableView.EMPTY;
        case GENERAL:
            // result
            List<Object> results = new ArrayList<>(data.size());
            data.forEach(object -> {
                results.add(ImmutableMap.of("result", object));
            });
            return new TableView(TableView.GENERAL_HEADER, results);
        case VERTEX:
            // id, label, properties
            List<Object> vertices = new ArrayList<>(data.size());
            data.forEach(object -> {
                if (object instanceof Vertex) {
                    vertices.add(object);
                }
            });
            return new TableView(TableView.VERTEX_HEADER, vertices);
        case EDGE:
            // id, label, source, target, properties
            List<Object> edges = new ArrayList<>(data.size());
            data.forEach(object -> {
                if (object instanceof Edge) {
                    edges.add(object);
                }
            });
            return new TableView(TableView.EDGE_HEADER, edges);
        case PATH:
            // path, only fill vertex/edge id
            List<Object> paths = new ArrayList<>(data.size());
            data.forEach(object -> {
                if (object instanceof Path) {
                    Path path = (Path) object;
                    List<Object> ids = new ArrayList<>();
                    path.objects().forEach(element -> {
                        if (element instanceof Vertex) {
                            ids.add(((Vertex) element).id());
                        } else if (element instanceof Edge) {
                            ids.add(((Edge) element).id());
                        } else {
                            ids.add(element);
                        }
                    });
                    paths.add(ImmutableMap.of("path", ids));
                }
            });
            return new TableView(TableView.PATH_HEADER, paths);
        default:
            throw new AssertionError(String.format("Unknown result type '%s'", typedResult.getType()));
    }
}
Also used : Path(com.baidu.hugegraph.structure.graph.Path) Vertex(com.baidu.hugegraph.structure.graph.Vertex) ArrayList(java.util.ArrayList) Edge(com.baidu.hugegraph.structure.graph.Edge) TableView(com.baidu.hugegraph.entity.query.TableView)

Example 2 with TableView

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

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

Aggregations

TableView (com.baidu.hugegraph.entity.query.TableView)3 HugeClient (com.baidu.hugegraph.driver.HugeClient)2 GraphView (com.baidu.hugegraph.entity.query.GraphView)2 JsonView (com.baidu.hugegraph.entity.query.JsonView)2 Path (com.baidu.hugegraph.structure.graph.Path)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 TypedResult (com.baidu.hugegraph.entity.query.TypedResult)1 Edge (com.baidu.hugegraph.structure.graph.Edge)1 Vertex (com.baidu.hugegraph.structure.graph.Vertex)1 ResultSet (com.baidu.hugegraph.structure.gremlin.ResultSet)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1