Search in sources :

Example 11 with Path

use of com.baidu.hugegraph.structure.graph.Path in project incubator-hugegraph-toolchain by apache.

the class RestResultTest method testReadGremlinPathWithVertexAndEdge.

@Test
public void testReadGremlinPathWithVertexAndEdge() {
    String json = "{" + "\"requestId\": \"238c74ca-18f7-4377-b8e1-2bb3b165e5d6\"," + "\"status\":{" + "\"message\": \"\"," + "\"code\": 200," + "\"attributes\":{}" + "}," + "\"result\":{" + "\"data\":[" + "{" + "\"labels\":[[], []]," + "\"objects\":[" + "{" + "\"id\": \"person:marko\"," + "\"label\": \"person\"," + "\"type\": \"vertex\"," + "\"properties\":{" + "\"city\":\"Beijing\"," + "\"name\":\"marko\"," + "\"age\":29" + "}" + "}," + "{" + "\"id\": \"person:marko>knows>>person:vadas\"," + "\"label\": \"knows\"," + "\"type\": \"edge\"," + "\"inVLabel\": \"person\"," + "\"outVLabel\": \"person\"," + "\"inV\": \"person:vadas\"," + "\"outV\": \"person:marko\"," + "\"properties\":{" + "\"date\": 1452355200000," + "\"weight\": 0.5" + "}" + "}" + "]" + "}" + "]," + "\"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();
    Object object = result.getObject();
    Assert.assertEquals(Path.class, object.getClass());
    Path path = (Path) object;
    Assert.assertEquals(2, path.labels().size());
    Assert.assertEquals(ImmutableList.of(), path.labels().get(0));
    Assert.assertEquals(ImmutableList.of(), path.labels().get(1));
    Vertex vertex = new Vertex("person");
    vertex.id("person:marko");
    vertex.property("name", "marko");
    vertex.property("age", 29);
    vertex.property("city", "Beijing");
    Edge edge = new Edge("knows");
    edge.id("person:marko>knows>>person:vadas");
    edge.sourceId("person:marko");
    edge.sourceLabel("person");
    edge.targetId("person:vadas");
    edge.targetLabel("person");
    edge.property("date", 1452355200000L);
    edge.property("weight", 0.5);
    Assert.assertEquals(2, path.objects().size());
    Utils.assertGraphEqual(ImmutableList.of(vertex), ImmutableList.of(edge), path.objects());
}
Also used : Response(com.baidu.hugegraph.structure.gremlin.Response) Path(com.baidu.hugegraph.structure.graph.Path) Vertex(com.baidu.hugegraph.structure.graph.Vertex) 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 12 with Path

use of com.baidu.hugegraph.structure.graph.Path in project incubator-hugegraph-toolchain by apache.

the class PathSerializerTest method testDeserializePathWithListType.

@Test
public void testDeserializePathWithListType() {
    String json = "{" + "\"labels\":[" + "[]," + "[]" + "]," + "\"objects\":[" + "[\"Beijing\", \"Beijing\"]," + "[\"Wuhan\", \"Hongkong\"]" + "]" + "}";
    Path path = BaseUnitTest.deserialize(json, Path.class);
    Assert.assertEquals(2, path.labels().size());
    Assert.assertEquals(ImmutableList.of(), path.labels().get(0));
    Assert.assertEquals(ImmutableList.of(), path.labels().get(1));
    Assert.assertEquals(2, path.objects().size());
    Assert.assertArrayEquals(new Object[] { ImmutableList.of("Beijing", "Beijing"), ImmutableList.of("Wuhan", "Hongkong") }, path.objects().toArray());
}
Also used : Path(com.baidu.hugegraph.structure.graph.Path) Test(org.junit.Test)

Example 13 with Path

use of com.baidu.hugegraph.structure.graph.Path 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 14 with Path

use of com.baidu.hugegraph.structure.graph.Path 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 15 with Path

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

Aggregations

Path (com.baidu.hugegraph.structure.graph.Path)30 Test (org.junit.Test)21 Vertex (com.baidu.hugegraph.structure.graph.Vertex)11 Edge (com.baidu.hugegraph.structure.graph.Edge)9 List (java.util.List)9 ImmutableList (com.google.common.collect.ImmutableList)7 BaseApiTest (com.baidu.hugegraph.api.BaseApiTest)6 Result (com.baidu.hugegraph.structure.gremlin.Result)6 HugeClient (com.baidu.hugegraph.driver.HugeClient)4 GraphView (com.baidu.hugegraph.entity.query.GraphView)4 ResultSet (com.baidu.hugegraph.structure.gremlin.ResultSet)4 ArrayList (java.util.ArrayList)4 GremlinResult (com.baidu.hugegraph.entity.query.GremlinResult)3 TableView (com.baidu.hugegraph.entity.query.TableView)3 TypedResult (com.baidu.hugegraph.entity.query.TypedResult)3 HashMap (java.util.HashMap)3 GremlinRequest (com.baidu.hugegraph.api.gremlin.GremlinRequest)2 Type (com.baidu.hugegraph.entity.query.GremlinResult.Type)2 JsonView (com.baidu.hugegraph.entity.query.JsonView)2 RestResult (com.baidu.hugegraph.rest.RestResult)2