Search in sources :

Example 41 with Vertex

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

the class InsertTask method insertBatch.

@SuppressWarnings("unchecked")
protected void insertBatch(List<Record> batch, boolean checkVertex) {
    HugeClient client = this.context.client();
    List<GraphElement> elements = new ArrayList<>(batch.size());
    batch.forEach(r -> elements.add(r.element()));
    if (this.type().isVertex()) {
        client.graph().addVertices((List<Vertex>) (Object) elements);
    } else {
        client.graph().addEdges((List<Edge>) (Object) elements, checkVertex);
    }
}
Also used : Vertex(com.baidu.hugegraph.structure.graph.Vertex) HugeClient(com.baidu.hugegraph.driver.HugeClient) GraphElement(com.baidu.hugegraph.structure.GraphElement) ArrayList(java.util.ArrayList) Edge(com.baidu.hugegraph.structure.graph.Edge)

Example 42 with Vertex

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

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

the class RestResultTest method testReadGremlinNullAndVertex.

@Test
public void testReadGremlinNullAndVertex() {
    String json = "{" + "\"requestId\": \"d95ac131-24b5-4140-a3ff-91b0c020764a\"," + "\"status\": {" + "\"message\": \"\"," + "\"code\": 200," + "\"attributes\": {}" + "}," + "\"result\": {" + "\"data\": [" + "null," + "{" + "\"id\": \"person:marko\"," + "\"label\": \"person\"," + "\"type\": \"vertex\"," + "\"properties\": {" + "\"city\": \"Beijing\"," + "\"name\": \"marko\"," + "\"age\": 29" + "}" + "}" + "]," + "\"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.assertNull(result);
    Assert.assertTrue(results.hasNext());
    result = results.next();
    Assert.assertEquals(Vertex.class, result.getObject().getClass());
    Vertex marko = new Vertex("person");
    marko.id("person:marko");
    marko.property("name", "marko");
    marko.property("city", "Beijing");
    marko.property("age", 29);
    Vertex vertex = result.getVertex();
    Assert.assertTrue(Utils.contains(ImmutableList.of(marko), vertex));
}
Also used : Response(com.baidu.hugegraph.structure.gremlin.Response) Vertex(com.baidu.hugegraph.structure.graph.Vertex) RestResult(com.baidu.hugegraph.rest.RestResult) Result(com.baidu.hugegraph.structure.gremlin.Result) RestResult(com.baidu.hugegraph.rest.RestResult) Test(org.junit.Test)

Example 44 with Vertex

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

the class BatchInsertTest method testBatchInsertInOneLoopButAddEdgesBeforeVertices.

@Test
public void testBatchInsertInOneLoopButAddEdgesBeforeVertices() {
    List<Vertex> vertices = new ArrayList<>(BATCH_SIZE);
    List<Edge> edges = new ArrayList<>(BATCH_SIZE);
    for (int i = 1; i <= BATCH_SIZE; i++) {
        Vertex vertex1 = new Vertex("person").property("name", "P-" + i).property("age", i);
        Vertex vertex2 = new Vertex("software").property("name", "S-" + i).property("lang", "java");
        Edge edge = new Edge("created").source(vertex1).target(vertex2).property("date", "2018-12-25");
        vertices.add(vertex1);
        vertices.add(vertex2);
        edges.add(edge);
        if (vertices.size() >= BATCH_SIZE) {
            // Must add vertices before edges
            Assert.assertThrows(InvalidOperationException.class, () -> {
                graph().addEdges(edges);
            });
            graph().addVertices(vertices);
            vertices.clear();
            edges.clear();
        }
    }
    Assert.assertEquals(2 * BATCH_SIZE, graph().listVertices(-1).size());
    Assert.assertEquals(0, graph().listEdges(-1).size());
}
Also used : Vertex(com.baidu.hugegraph.structure.graph.Vertex) ArrayList(java.util.ArrayList) Edge(com.baidu.hugegraph.structure.graph.Edge) Test(org.junit.Test) BaseClientTest(com.baidu.hugegraph.BaseClientTest)

Example 45 with Vertex

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

the class BatchInsertTest method testBatchInsertInOneLoop.

@Test
public void testBatchInsertInOneLoop() {
    List<Vertex> vertices = new ArrayList<>(BATCH_SIZE);
    List<Edge> edges = new ArrayList<>(BATCH_SIZE);
    for (int i = 1; i <= BATCH_SIZE; i++) {
        Vertex vertex1 = new Vertex("person").property("name", "P-" + i).property("age", i);
        Vertex vertex2 = new Vertex("software").property("name", "S-" + i).property("lang", "java");
        Edge edge = new Edge("created").source(vertex1).target(vertex2).property("date", "2018-12-25");
        vertices.add(vertex1);
        vertices.add(vertex2);
        edges.add(edge);
        if (vertices.size() >= BATCH_SIZE) {
            graph().addVertices(vertices);
            graph().addEdges(edges);
            vertices.clear();
            edges.clear();
        }
    }
    Assert.assertEquals(2 * BATCH_SIZE, graph().listVertices(-1).size());
    Assert.assertEquals(BATCH_SIZE, graph().listEdges(-1).size());
}
Also used : Vertex(com.baidu.hugegraph.structure.graph.Vertex) ArrayList(java.util.ArrayList) Edge(com.baidu.hugegraph.structure.graph.Edge) Test(org.junit.Test) BaseClientTest(com.baidu.hugegraph.BaseClientTest)

Aggregations

Vertex (com.baidu.hugegraph.structure.graph.Vertex)165 Test (org.junit.Test)110 Edge (com.baidu.hugegraph.structure.graph.Edge)33 HugeClient (com.baidu.hugegraph.driver.HugeClient)22 ArrayList (java.util.ArrayList)21 SchemaManager (com.baidu.hugegraph.driver.SchemaManager)18 BaseClientTest (com.baidu.hugegraph.BaseClientTest)17 BeforeClass (org.junit.BeforeClass)17 GraphManager (com.baidu.hugegraph.driver.GraphManager)14 BatchVertexRequest (com.baidu.hugegraph.structure.graph.BatchVertexRequest)13 Path (com.baidu.hugegraph.structure.graph.Path)11 Result (com.baidu.hugegraph.structure.gremlin.Result)10 ResultSet (com.baidu.hugegraph.structure.gremlin.ResultSet)10 List (java.util.List)10 BaseApiTest (com.baidu.hugegraph.api.BaseApiTest)9 ImmutableList (com.google.common.collect.ImmutableList)6 ImmutableMap (com.google.common.collect.ImmutableMap)6 Map (java.util.Map)6 RestResult (com.baidu.hugegraph.rest.RestResult)5 PathsWithVertices (com.baidu.hugegraph.structure.traverser.PathsWithVertices)5