Search in sources :

Example 11 with Result

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

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

the class RestResultTest method testReadGremlinVertices.

@Test
public void testReadGremlinVertices() {
    String json = "{" + "\"requestId\": \"b0fd8ead-333f-43ac-97b0-4d78784726ae\"," + "\"status\": {" + "\"message\": \"\"," + "\"code\": 200," + "\"attributes\": {}" + "}," + "\"result\": {" + "\"data\": [" + "{" + "\"id\": \"person:marko\"," + "\"label\": \"person\"," + "\"type\": \"vertex\"," + "\"properties\": {" + "\"city\": [\"Beijing\",\"Wuhan\",\"Beijing\"]," + "\"name\": \"marko\"," + "\"age\": 29" + "}" + "}," + "{" + "\"id\": \"software:lop\"," + "\"label\": \"software\"," + "\"type\": \"vertex\"," + "\"properties\": {" + "\"price\": 328," + "\"name\": \"lop\"," + "\"lang\": [\"java\",\"python\",\"c++\"]" + "}" + "}," + "{" + "\"id\": \"person:peter\"," + "\"label\": \"person\"," + "\"type\": \"vertex\"," + "\"properties\": {" + "\"city\": [\"Shanghai\"]," + "\"name\": \"peter\"," + "\"age\": 35" + "}" + "}" + "]," + "\"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("b0fd8ead-333f-43ac-97b0-4d78784726ae", response.requestId());
    Assert.assertEquals(200, response.status().code());
    Vertex marko = new Vertex("person");
    marko.id("person:marko");
    marko.property("name", "marko");
    marko.property("city", ImmutableList.of("Beijing", "Wuhan", "Beijing"));
    marko.property("age", 29);
    Vertex lop = new Vertex("software");
    lop.id("software:lop");
    lop.property("name", "lop");
    lop.property("lang", ImmutableList.of("java", "python", "c++"));
    lop.property("price", 328);
    Vertex peter = new Vertex("person");
    peter.id("person:peter");
    peter.property("name", "peter");
    peter.property("city", ImmutableList.of("Shanghai"));
    peter.property("age", 35);
    List<Vertex> vertices = new ArrayList<>(3);
    vertices.add(peter);
    vertices.add(marko);
    vertices.add(lop);
    Iterator<Result> results = response.result().iterator();
    while (results.hasNext()) {
        Result result = results.next();
        Assert.assertEquals(Vertex.class, result.getObject().getClass());
        Vertex vertex = result.getVertex();
        Assert.assertTrue(Utils.contains(vertices, vertex));
    }
}
Also used : Response(com.baidu.hugegraph.structure.gremlin.Response) Vertex(com.baidu.hugegraph.structure.graph.Vertex) RestResult(com.baidu.hugegraph.rest.RestResult) ArrayList(java.util.ArrayList) Result(com.baidu.hugegraph.structure.gremlin.Result) RestResult(com.baidu.hugegraph.rest.RestResult) Test(org.junit.Test)

Example 13 with Result

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

the class EdgeTest method testQueryByPagingAndFiltering.

@Test
public void testQueryByPagingAndFiltering() {
    SchemaManager schema = schema();
    schema.propertyKey("no").asText().create();
    schema.propertyKey("location").asText().create();
    schema.propertyKey("callType").asText().create();
    schema.propertyKey("calltime").asDate().create();
    schema.propertyKey("duration").asInt().create();
    schema.vertexLabel("phone").properties("no").primaryKeys("no").enableLabelIndex(false).create();
    schema.edgeLabel("call").multiTimes().properties("location", "callType", "duration", "calltime").sourceLabel("phone").targetLabel("phone").sortKeys("location", "callType", "duration", "calltime").create();
    Vertex v1 = graph().addVertex(T.label, "phone", "no", "13812345678");
    Vertex v2 = graph().addVertex(T.label, "phone", "no", "13866668888");
    Vertex v10086 = graph().addVertex(T.label, "phone", "no", "10086");
    v1.addEdge("call", v2, "location", "Beijing", "callType", "work", "duration", 3, "calltime", "2017-5-1 23:00:00");
    v1.addEdge("call", v2, "location", "Beijing", "callType", "work", "duration", 3, "calltime", "2017-5-2 12:00:01");
    v1.addEdge("call", v2, "location", "Beijing", "callType", "work", "duration", 3, "calltime", "2017-5-3 12:08:02");
    v1.addEdge("call", v2, "location", "Beijing", "callType", "work", "duration", 8, "calltime", "2017-5-3 22:22:03");
    v1.addEdge("call", v2, "location", "Beijing", "callType", "fun", "duration", 10, "calltime", "2017-5-4 20:33:04");
    v1.addEdge("call", v10086, "location", "Nanjing", "callType", "work", "duration", 12, "calltime", "2017-5-2 15:30:05");
    v1.addEdge("call", v10086, "location", "Nanjing", "callType", "work", "duration", 14, "calltime", "2017-5-3 14:56:06");
    v2.addEdge("call", v10086, "location", "Nanjing", "callType", "fun", "duration", 15, "calltime", "2017-5-3 17:28:07");
    ResultSet resultSet = gremlin().gremlin("g.V(vid).outE('call')" + ".has('location', 'Beijing')" + ".has('callType', 'work')" + ".has('duration', 3)" + ".has('calltime', " + "P.between('2017-5-2', " + "'2017-5-4'))" + ".toList()").binding("vid", v1.id()).execute();
    Iterator<Result> results = resultSet.iterator();
    Assert.assertEquals(2, Iterators.size(results));
    Assert.assertThrows(ServerException.class, () -> {
        // no location
        gremlin().gremlin("g.V(vid).outE('call').has('callType', 'work')" + ".has('duration', 3).has('calltime', " + "P.between('2017-5-2', '2017-5-4'))" + ".has('~page', '')").binding("vid", v1.id()).execute();
    }, e -> {
        Assert.assertContains("Can't query by paging and filtering", e.getMessage());
    });
}
Also used : Vertex(com.baidu.hugegraph.structure.graph.Vertex) ResultSet(com.baidu.hugegraph.structure.gremlin.ResultSet) SchemaManager(com.baidu.hugegraph.driver.SchemaManager) Result(com.baidu.hugegraph.structure.gremlin.Result) Test(org.junit.Test) BaseClientTest(com.baidu.hugegraph.BaseClientTest)

Example 14 with Result

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

the class GremlinApiTest method testPrimitiveObject.

@Test
public void testPrimitiveObject() {
    GremlinRequest request = new GremlinRequest("1 + 2");
    ResultSet resultSet = gremlin().execute(request);
    Assert.assertEquals(1, resultSet.size());
    Iterator<Result> results = resultSet.iterator();
    while (results.hasNext()) {
        Result result = results.next();
        Object object = result.getObject();
        Assert.assertEquals(Integer.class, object.getClass());
        Assert.assertEquals(3, object);
    }
}
Also used : ResultSet(com.baidu.hugegraph.structure.gremlin.ResultSet) GremlinRequest(com.baidu.hugegraph.api.gremlin.GremlinRequest) Result(com.baidu.hugegraph.structure.gremlin.Result) Test(org.junit.Test)

Example 15 with Result

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

the class GremlinApiTest method testAttachedManager.

@Test
public void testAttachedManager() {
    GremlinRequest request = new GremlinRequest("g.V()");
    ResultSet resultSet = gremlin().execute(request);
    Assert.assertEquals(6, resultSet.size());
    Iterator<Result> results = resultSet.iterator();
    while (results.hasNext()) {
        Result result = results.next();
        Object object = result.getObject();
        Assert.assertEquals(Vertex.class, object.getClass());
        Vertex vertex = (Vertex) object;
        Assert.assertNotNull(Whitebox.getInternalState(vertex, "manager"));
    }
    request = new GremlinRequest("g.E()");
    resultSet = gremlin().execute(request);
    Assert.assertEquals(6, resultSet.size());
    results = resultSet.iterator();
    while (results.hasNext()) {
        Result result = results.next();
        Object object = result.getObject();
        Assert.assertEquals(Edge.class, object.getClass());
        Edge edge = (Edge) object;
        Assert.assertNotNull(Whitebox.getInternalState(edge, "manager"));
    }
    request = new GremlinRequest("g.V().outE().path()");
    resultSet = gremlin().execute(request);
    Assert.assertEquals(6, resultSet.size());
    results = resultSet.iterator();
    while (results.hasNext()) {
        Result result = results.next();
        Object object = result.getObject();
        Assert.assertEquals(Path.class, object.getClass());
        Path path = (Path) object;
        Assert.assertNotNull(path.objects());
        for (Object pathObject : path.objects()) {
            Assert.assertTrue(pathObject instanceof GraphAttachable);
            Assert.assertNotNull(Whitebox.getInternalState(pathObject, "manager"));
        }
        Assert.assertNull(path.crosspoint());
    }
}
Also used : Path(com.baidu.hugegraph.structure.graph.Path) Vertex(com.baidu.hugegraph.structure.graph.Vertex) GraphAttachable(com.baidu.hugegraph.structure.constant.GraphAttachable) ResultSet(com.baidu.hugegraph.structure.gremlin.ResultSet) GremlinRequest(com.baidu.hugegraph.api.gremlin.GremlinRequest) Edge(com.baidu.hugegraph.structure.graph.Edge) Result(com.baidu.hugegraph.structure.gremlin.Result) Test(org.junit.Test)

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