Search in sources :

Example 16 with ResultSet

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

the class VertexApiTest method testOlapPropertyWrite.

@Test
public void testOlapPropertyWrite() {
    List<Vertex> vertices = super.create100PersonBatch();
    List<Object> ids = vertexAPI.create(vertices);
    // Create olap property key
    PropertyKey pagerank = schema().propertyKey("pagerank").asDouble().writeType(WriteType.OLAP_RANGE).build();
    PropertyKey.PropertyKeyWithTask propertyKeyWithTask;
    propertyKeyWithTask = propertyKeyAPI.create(pagerank);
    long taskId1 = propertyKeyWithTask.taskId();
    Assert.assertNotEquals(0L, taskId1);
    PropertyKey wcc = schema().propertyKey("wcc").asText().writeType(WriteType.OLAP_SECONDARY).build();
    propertyKeyWithTask = propertyKeyAPI.create(wcc);
    long taskId2 = propertyKeyWithTask.taskId();
    Assert.assertNotEquals(0L, taskId2);
    PropertyKey none = schema().propertyKey("none").asText().writeType(WriteType.OLAP_COMMON).build();
    propertyKeyWithTask = propertyKeyAPI.create(none);
    long taskId3 = propertyKeyWithTask.taskId();
    Assert.assertNotEquals(0L, taskId3);
    waitUntilTaskCompleted(taskId1);
    waitUntilTaskCompleted(taskId2);
    waitUntilTaskCompleted(taskId3);
    // Add olap properties
    vertices = new ArrayList<>(100);
    for (int i = 0; i < 100; i++) {
        Vertex vertex = new Vertex(null);
        vertex.id(ids.get(i));
        vertex.property("pagerank", 0.1D * i);
        vertices.add(vertex);
    }
    ids = vertexAPI.create(vertices);
    Assert.assertEquals(100, ids.size());
    vertices = new ArrayList<>(100);
    for (int i = 0; i < 100; i++) {
        Vertex vertex = new Vertex(null);
        vertex.id(ids.get(i));
        vertex.property("wcc", "wcc" + i);
        vertices.add(vertex);
    }
    ids = vertexAPI.create(vertices);
    Assert.assertEquals(100, ids.size());
    vertices = new ArrayList<>(100);
    for (int i = 0; i < 100; i++) {
        Vertex vertex = new Vertex(null);
        vertex.id(ids.get(i));
        vertex.property("none", "none" + i);
        vertices.add(vertex);
    }
    ids = vertexAPI.create(vertices);
    Assert.assertEquals(100, ids.size());
    // Query vertices by id before set graph read mode to 'ALL'
    for (int i = 0; i < 100; i++) {
        Vertex person = vertexAPI.get(ids.get(i));
        Assert.assertEquals("person", person.label());
        Map<String, Object> props = ImmutableMap.of("name", "Person-" + i, "city", "Beijing", "age", 30);
        Assert.assertEquals(props, person.properties());
    }
    // Set graph read mode to 'ALL'
    graphsAPI.readMode("hugegraph", GraphReadMode.ALL);
    // Query vertices by id after set graph read mode to 'ALL'
    for (int i = 0; i < 100; i++) {
        Vertex person = vertexAPI.get(ids.get(i));
        Assert.assertEquals("person", person.label());
        Map<String, Object> props = ImmutableMap.<String, Object>builder().put("name", "Person-" + i).put("city", "Beijing").put("age", 30).put("pagerank", 0.1D * i).put("wcc", "wcc" + i).put("none", "none" + i).build();
        Assert.assertEquals(props, person.properties());
    }
    // Query vertices by olap properties
    GremlinRequest request = new GremlinRequest("g.V().has(\"pagerank\", P.gte(5))");
    ResultSet resultSet = gremlin().execute(request);
    Assert.assertEquals(50, resultSet.size());
    request = new GremlinRequest("g.V().has(\"wcc\", P.within(\"wcc10\", \"wcc20\"))");
    resultSet = gremlin().execute(request);
    Assert.assertEquals(2, resultSet.size());
    // Clear olap property key
    propertyKeyWithTask = propertyKeyAPI.clear(pagerank);
    taskId1 = propertyKeyWithTask.taskId();
    Assert.assertNotEquals(0L, taskId1);
    propertyKeyWithTask = propertyKeyAPI.clear(wcc);
    taskId2 = propertyKeyWithTask.taskId();
    Assert.assertNotEquals(0L, taskId2);
    propertyKeyWithTask = propertyKeyAPI.clear(none);
    taskId3 = propertyKeyWithTask.taskId();
    Assert.assertNotEquals(0L, taskId3);
    waitUntilTaskCompleted(taskId1);
    waitUntilTaskCompleted(taskId2);
    waitUntilTaskCompleted(taskId3);
    // Query after clear olap property key
    request = new GremlinRequest("g.V().has(\"pagerank\", P.gte(5))");
    resultSet = gremlin().execute(request);
    Assert.assertEquals(0, resultSet.size());
    request = new GremlinRequest("g.V().has(\"wcc\", P.within(\"wcc10\", \"wcc20\"))");
    resultSet = gremlin().execute(request);
    Assert.assertEquals(0, resultSet.size());
    // Delete olap property key
    taskId1 = propertyKeyAPI.delete(pagerank.name());
    Assert.assertNotEquals(0L, taskId1);
    taskId2 = propertyKeyAPI.delete(wcc.name());
    Assert.assertNotEquals(0L, taskId2);
    taskId3 = propertyKeyAPI.delete(none.name());
    Assert.assertNotEquals(0L, taskId3);
    waitUntilTaskCompleted(taskId1);
    waitUntilTaskCompleted(taskId2);
    waitUntilTaskCompleted(taskId3);
    // Query after delete olap property key
    Assert.assertThrows(ServerException.class, () -> {
        gremlin().execute(new GremlinRequest("g.V().has(\"pagerank\", P.gte(5))"));
    }, e -> {
        Assert.assertContains("Undefined property key: 'pagerank'", e.getMessage());
    });
    Assert.assertThrows(ServerException.class, () -> {
        gremlin().execute(new GremlinRequest("g.V().has(\"wcc\", P.within(\"wcc10\", \"wcc20\"))"));
    }, e -> {
        Assert.assertContains("Undefined property key: 'wcc'", e.getMessage());
    });
    // Resume graph read mode to 'OLTP_ONLY'
    graphsAPI.readMode("hugegraph", GraphReadMode.OLTP_ONLY);
}
Also used : Vertex(com.baidu.hugegraph.structure.graph.Vertex) ResultSet(com.baidu.hugegraph.structure.gremlin.ResultSet) GremlinRequest(com.baidu.hugegraph.api.gremlin.GremlinRequest) PropertyKey(com.baidu.hugegraph.structure.schema.PropertyKey) Test(org.junit.Test)

Example 17 with ResultSet

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

the class GremlinApiTest method testAsyncRemoveAllVertices.

@Test
public void testAsyncRemoveAllVertices() {
    GremlinRequest request = new GremlinRequest("g.V()");
    ResultSet resultSet = gremlin().execute(request);
    Assert.assertEquals(6, resultSet.size());
    String gremlin = "hugegraph.traversal().V().drop()";
    request = new GremlinRequest(gremlin);
    long id = gremlin().executeAsTask(request);
    waitUntilTaskCompleted(id);
    request = new GremlinRequest("g.V()");
    resultSet = gremlin().execute(request);
    Assert.assertEquals(0, resultSet.size());
}
Also used : ResultSet(com.baidu.hugegraph.structure.gremlin.ResultSet) GremlinRequest(com.baidu.hugegraph.api.gremlin.GremlinRequest) Test(org.junit.Test)

Example 18 with ResultSet

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

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

Example 20 with ResultSet

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

the class HugeClientUtil method tryConnect.

public static HugeClient tryConnect(GraphConnection connection) {
    String graph = connection.getGraph();
    String host = connection.getHost();
    Integer port = connection.getPort();
    String username = connection.getUsername();
    String password = connection.getPassword();
    int timeout = connection.getTimeout();
    String protocol = connection.getProtocol() == null ? DEFAULT_PROTOCOL : connection.getProtocol();
    String trustStoreFile = connection.getTrustStoreFile();
    String trustStorePassword = connection.getTrustStorePassword();
    String url = UriComponentsBuilder.newInstance().scheme(protocol).host(host).port(port).toUriString();
    if (username == null) {
        username = "";
        password = "";
    }
    HugeClient client;
    try {
        client = HugeClient.builder(url, graph).configUser(username, password).configTimeout(timeout).configSSL(trustStoreFile, trustStorePassword).build();
    } catch (IllegalStateException e) {
        String message = e.getMessage();
        if (message != null && message.startsWith("The version")) {
            throw new ExternalException("client-server.version.unmatched", e);
        }
        if (message != null && (message.startsWith("Error loading trust store from") || message.startsWith("Cannot find trust store file"))) {
            throw new ExternalException("https.load.truststore.error", e);
        }
        throw e;
    } catch (ServerException e) {
        String message = e.getMessage();
        if (Constant.STATUS_UNAUTHORIZED == e.status() || (message != null && message.startsWith("Authentication"))) {
            throw new ExternalException("graph-connection.username-or-password.incorrect", e);
        }
        if (message != null && message.contains("Invalid syntax for " + "username and password")) {
            throw new ExternalException("graph-connection.missing-username-password", e);
        }
        throw e;
    } catch (ClientException e) {
        Throwable cause = e.getCause();
        if (cause == null || cause.getMessage() == null) {
            throw e;
        }
        String message = cause.getMessage();
        if (message.contains("Connection refused")) {
            throw new ExternalException("service.unavailable", e, host, port);
        } else if (message.contains("java.net.UnknownHostException") || message.contains("Host name may not be null")) {
            throw new ExternalException("service.unknown-host", e, host);
        } else if (message.contains("<!doctype html>")) {
            throw new ExternalException("service.suspected-web", e, host, port);
        }
        throw e;
    }
    try {
        ResultSet rs = client.gremlin().gremlin("g.V().limit(1)").execute();
        rs.iterator().forEachRemaining(Result::getObject);
    } catch (ServerException e) {
        if (Constant.STATUS_UNAUTHORIZED == e.status()) {
            throw new ExternalException("graph-connection.username-or-password.incorrect", e);
        }
        String message = e.message();
        if (message != null && message.contains("Could not rebind [g]")) {
            throw new ExternalException("graph-connection.graph.unexist", e, graph, host, port);
        }
        if (!isAcceptable(message)) {
            throw e;
        }
    } catch (Exception e) {
        client.close();
        throw e;
    }
    return client;
}
Also used : HugeClient(com.baidu.hugegraph.driver.HugeClient) ServerException(com.baidu.hugegraph.exception.ServerException) ResultSet(com.baidu.hugegraph.structure.gremlin.ResultSet) ClientException(com.baidu.hugegraph.rest.ClientException) ExternalException(com.baidu.hugegraph.exception.ExternalException) ServerException(com.baidu.hugegraph.exception.ServerException) ExternalException(com.baidu.hugegraph.exception.ExternalException) ClientException(com.baidu.hugegraph.rest.ClientException) Result(com.baidu.hugegraph.structure.gremlin.Result)

Aggregations

ResultSet (com.baidu.hugegraph.structure.gremlin.ResultSet)20 Test (org.junit.Test)13 GremlinRequest (com.baidu.hugegraph.api.gremlin.GremlinRequest)10 Vertex (com.baidu.hugegraph.structure.graph.Vertex)10 Result (com.baidu.hugegraph.structure.gremlin.Result)9 HugeClient (com.baidu.hugegraph.driver.HugeClient)8 Edge (com.baidu.hugegraph.structure.graph.Edge)7 ArrayList (java.util.ArrayList)5 TypedResult (com.baidu.hugegraph.entity.query.TypedResult)4 ClientException (com.baidu.hugegraph.rest.ClientException)4 Path (com.baidu.hugegraph.structure.graph.Path)4 GraphView (com.baidu.hugegraph.entity.query.GraphView)3 GremlinResult (com.baidu.hugegraph.entity.query.GremlinResult)3 SchemaManager (com.baidu.hugegraph.driver.SchemaManager)2 JsonView (com.baidu.hugegraph.entity.query.JsonView)2 TableView (com.baidu.hugegraph.entity.query.TableView)2 ExternalException (com.baidu.hugegraph.exception.ExternalException)2 ServerException (com.baidu.hugegraph.exception.ServerException)2 Task (com.baidu.hugegraph.structure.Task)2 HashMap (java.util.HashMap)2