Search in sources :

Example 6 with Result

use of com.baidu.hugegraph.structure.gremlin.Result in project incubator-hugegraph-toolchain by apache.

the class RestResultTest method testReadGremlinEdgeAndNull.

@Test
public void testReadGremlinEdgeAndNull() {
    String json = "{" + "\"requestId\": \"d95ac131-24b5-4140-a3ff-91b0c020764a\"," + "\"status\": {" + "\"message\": \"\"," + "\"code\": 200," + "\"attributes\": {}" + "}," + "\"result\": {" + "\"data\": [" + "{" + "\"id\": \"person:peter>created>>software:lop\"," + "\"label\": \"created\"," + "\"type\": \"edge\"," + "\"inVLabel\": \"software\"," + "\"outVLabel\": \"person\"," + "\"inV\": \"software:lop\"," + "\"outV\": \"person:peter\"," + "\"properties\": {" + "\"date\": 1490284800000," + "\"weight\": 0.2" + "}" + "}," + "null" + "]," + "\"meta\": {}" + "}" + "}";
    Mockito.when(this.mockResponse.getStatus()).thenReturn(200);
    Mockito.when(this.mockResponse.getHeaders()).thenReturn(null);
    Mockito.when(this.mockResponse.readEntity(String.class)).thenReturn(json);
    RestResult restResult = new RestResult(this.mockResponse);
    Assert.assertEquals(200, restResult.status());
    Assert.assertNull(restResult.headers());
    Response response = restResult.readObject(Response.class);
    response.graphManager(graph());
    Assert.assertEquals(200, response.status().code());
    Iterator<Result> results = response.result().iterator();
    Assert.assertTrue(results.hasNext());
    Result result = results.next();
    Assert.assertEquals(Edge.class, result.getObject().getClass());
    Edge created = new Edge("created");
    created.id("person:peter>created>>software:lop");
    created.sourceId("person:peter");
    created.targetId("software:lop");
    created.sourceLabel("person");
    created.targetLabel("software");
    created.property("date", 1490284800000L);
    created.property("weight", 0.2);
    Assert.assertTrue(Utils.contains(ImmutableList.of(created), result.getEdge()));
    Assert.assertTrue(results.hasNext());
    result = results.next();
    Assert.assertNull(result);
}
Also used : Response(com.baidu.hugegraph.structure.gremlin.Response) RestResult(com.baidu.hugegraph.rest.RestResult) Edge(com.baidu.hugegraph.structure.graph.Edge) Result(com.baidu.hugegraph.structure.gremlin.Result) RestResult(com.baidu.hugegraph.rest.RestResult) Test(org.junit.Test)

Example 7 with Result

use of com.baidu.hugegraph.structure.gremlin.Result in project incubator-hugegraph-toolchain by apache.

the class SingleExample method main.

public static void main(String[] args) throws IOException {
    // If connect failed will throw a exception.
    HugeClient hugeClient = HugeClient.builder("http://localhost:8080", "hugegraph").build();
    SchemaManager schema = hugeClient.schema();
    schema.propertyKey("name").asText().ifNotExist().create();
    schema.propertyKey("age").asInt().ifNotExist().create();
    schema.propertyKey("city").asText().ifNotExist().create();
    schema.propertyKey("weight").asDouble().ifNotExist().create();
    schema.propertyKey("lang").asText().ifNotExist().create();
    schema.propertyKey("date").asDate().ifNotExist().create();
    schema.propertyKey("price").asInt().ifNotExist().create();
    schema.vertexLabel("person").properties("name", "age", "city").primaryKeys("name").ifNotExist().create();
    schema.vertexLabel("software").properties("name", "lang", "price").primaryKeys("name").ifNotExist().create();
    schema.indexLabel("personByCity").onV("person").by("city").secondary().ifNotExist().create();
    schema.indexLabel("personByAgeAndCity").onV("person").by("age", "city").secondary().ifNotExist().create();
    schema.indexLabel("softwareByPrice").onV("software").by("price").range().ifNotExist().create();
    schema.edgeLabel("knows").sourceLabel("person").targetLabel("person").properties("date", "weight").ifNotExist().create();
    schema.edgeLabel("created").sourceLabel("person").targetLabel("software").properties("date", "weight").ifNotExist().create();
    schema.indexLabel("createdByDate").onE("created").by("date").secondary().ifNotExist().create();
    schema.indexLabel("createdByWeight").onE("created").by("weight").range().ifNotExist().create();
    schema.indexLabel("knowsByWeight").onE("knows").by("weight").range().ifNotExist().create();
    GraphManager graph = hugeClient.graph();
    Vertex marko = graph.addVertex(T.label, "person", "name", "marko", "age", 29, "city", "Beijing");
    Vertex vadas = graph.addVertex(T.label, "person", "name", "vadas", "age", 27, "city", "Hongkong");
    Vertex lop = graph.addVertex(T.label, "software", "name", "lop", "lang", "java", "price", 328);
    Vertex josh = graph.addVertex(T.label, "person", "name", "josh", "age", 32, "city", "Beijing");
    Vertex ripple = graph.addVertex(T.label, "software", "name", "ripple", "lang", "java", "price", 199);
    Vertex peter = graph.addVertex(T.label, "person", "name", "peter", "age", 35, "city", "Shanghai");
    marko.addEdge("knows", vadas, "date", "2016-01-10", "weight", 0.5);
    marko.addEdge("knows", josh, "date", "2013-02-20", "weight", 1.0);
    marko.addEdge("created", lop, "date", "2017-12-10", "weight", 0.4);
    josh.addEdge("created", lop, "date", "2009-11-11", "weight", 0.4);
    josh.addEdge("created", ripple, "date", "2017-12-10", "weight", 1.0);
    peter.addEdge("created", lop, "date", "2017-03-24", "weight", 0.2);
    GremlinManager gremlin = hugeClient.gremlin();
    System.out.println("==== Path ====");
    ResultSet resultSet = gremlin.gremlin("g.V().outE().path()").execute();
    Iterator<Result> results = resultSet.iterator();
    results.forEachRemaining(result -> {
        System.out.println(result.getObject().getClass());
        Object object = result.getObject();
        if (object instanceof Vertex) {
            System.out.println(((Vertex) object).id());
        } else if (object instanceof Edge) {
            System.out.println(((Edge) object).id());
        } else if (object instanceof Path) {
            List<Object> elements = ((Path) object).objects();
            elements.forEach(element -> {
                System.out.println(element.getClass());
                System.out.println(element);
            });
        } else {
            System.out.println(object);
        }
    });
    hugeClient.close();
}
Also used : Path(com.baidu.hugegraph.structure.graph.Path) Vertex(com.baidu.hugegraph.structure.graph.Vertex) HugeClient(com.baidu.hugegraph.driver.HugeClient) GraphManager(com.baidu.hugegraph.driver.GraphManager) ResultSet(com.baidu.hugegraph.structure.gremlin.ResultSet) SchemaManager(com.baidu.hugegraph.driver.SchemaManager) GremlinManager(com.baidu.hugegraph.driver.GremlinManager) Edge(com.baidu.hugegraph.structure.graph.Edge) Result(com.baidu.hugegraph.structure.gremlin.Result)

Example 8 with Result

use of com.baidu.hugegraph.structure.gremlin.Result 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 9 with Result

use of com.baidu.hugegraph.structure.gremlin.Result 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 10 with Result

use of com.baidu.hugegraph.structure.gremlin.Result 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)

Aggregations

Result (com.baidu.hugegraph.structure.gremlin.Result)16 Vertex (com.baidu.hugegraph.structure.graph.Vertex)10 ResultSet (com.baidu.hugegraph.structure.gremlin.ResultSet)9 Test (org.junit.Test)9 Edge (com.baidu.hugegraph.structure.graph.Edge)8 RestResult (com.baidu.hugegraph.rest.RestResult)6 Path (com.baidu.hugegraph.structure.graph.Path)6 Response (com.baidu.hugegraph.structure.gremlin.Response)6 ArrayList (java.util.ArrayList)5 HugeClient (com.baidu.hugegraph.driver.HugeClient)4 GremlinResult (com.baidu.hugegraph.entity.query.GremlinResult)4 TypedResult (com.baidu.hugegraph.entity.query.TypedResult)4 GremlinRequest (com.baidu.hugegraph.api.gremlin.GremlinRequest)3 HashMap (java.util.HashMap)3 SchemaManager (com.baidu.hugegraph.driver.SchemaManager)2 GraphView (com.baidu.hugegraph.entity.query.GraphView)2 Type (com.baidu.hugegraph.entity.query.GremlinResult.Type)2 ExternalException (com.baidu.hugegraph.exception.ExternalException)2 ServerException (com.baidu.hugegraph.exception.ServerException)2 ImmutableMap (com.google.common.collect.ImmutableMap)2