Search in sources :

Example 56 with HugeClient

use of com.baidu.hugegraph.driver.HugeClient in project incubator-hugegraph-toolchain by apache.

the class GraphConnectionController method create.

@PostMapping
public GraphConnection create(@RequestBody GraphConnection newEntity) {
    this.checkParamsValid(newEntity, true);
    this.checkAddressSecurity(newEntity);
    // Make sure the new entity doesn't conflict with exists
    this.checkEntityUnique(newEntity, true);
    newEntity.setTimeout(this.config.get(HubbleOptions.CLIENT_REQUEST_TIMEOUT));
    // Do connect test, failure will throw an exception
    this.sslService.configSSL(this.config, newEntity);
    HugeClient client = HugeClientUtil.tryConnect(newEntity);
    newEntity.setCreateTime(HubbleUtil.nowDate());
    this.connService.save(newEntity);
    this.poolService.put(newEntity, client);
    return newEntity;
}
Also used : HugeClient(com.baidu.hugegraph.driver.HugeClient) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 57 with HugeClient

use of com.baidu.hugegraph.driver.HugeClient in project incubator-hugegraph-toolchain by apache.

the class GraphConnectionController method update.

@PutMapping("{id}")
public GraphConnection update(@PathVariable("id") int id, @RequestBody GraphConnection newEntity) {
    this.checkIdSameAsBody(id, newEntity);
    this.checkParamsValid(newEntity, false);
    this.checkAddressSecurity(newEntity);
    // Check exist connection with this id
    GraphConnection oldEntity = this.connService.get(id);
    if (oldEntity == null) {
        throw new ExternalException("graph-connection.not-exist.id", id);
    }
    GraphConnection entity = this.mergeEntity(oldEntity, newEntity);
    // Make sure the updated connection doesn't conflict with exists
    this.checkEntityUnique(entity, false);
    this.sslService.configSSL(this.config, entity);
    HugeClient client = HugeClientUtil.tryConnect(entity);
    this.connService.update(entity);
    this.poolService.put(entity, client);
    return entity;
}
Also used : HugeClient(com.baidu.hugegraph.driver.HugeClient) GraphConnection(com.baidu.hugegraph.entity.GraphConnection) ExternalException(com.baidu.hugegraph.exception.ExternalException) PutMapping(org.springframework.web.bind.annotation.PutMapping)

Example 58 with HugeClient

use of com.baidu.hugegraph.driver.HugeClient in project incubator-hugegraph-toolchain by apache.

the class GraphsApiTest method testCloneAndDropGraph.

@Test
public void testCloneAndDropGraph() {
    int initialGraphNumber = graphsAPI.list().size();
    // Clone a new graph from exist a graph dynamically
    String config;
    try {
        config = FileUtils.readFileToString(new File(CONFIG3_PATH), StandardCharsets.UTF_8);
    } catch (IOException e) {
        throw new ClientException("Failed to read config file: %s", CONFIG3_PATH);
    }
    Map<String, String> result = graphsAPI.create(GRAPH3, "hugegraph", config);
    Assert.assertEquals(2, result.size());
    Assert.assertEquals(GRAPH3, result.get("name"));
    Assert.assertEquals("rocksdb", result.get("backend"));
    Assert.assertEquals(initialGraphNumber + 1, graphsAPI.list().size());
    HugeClient client = new HugeClient(baseClient(), GRAPH3);
    // Insert graph schema and data
    initPropertyKey(client);
    initVertexLabel(client);
    initEdgeLabel(client);
    List<Vertex> vertices = new ArrayList<>(100);
    for (int i = 0; i < 100; i++) {
        Vertex vertex = new Vertex("person").property("name", "person" + i).property("city", "Beijing").property("age", 19);
        vertices.add(vertex);
    }
    vertices = client.graph().addVertices(vertices);
    List<Edge> edges = new ArrayList<>(100);
    for (int i = 0; i < 100; i++) {
        Edge edge = new Edge("knows").source(vertices.get(i)).target(vertices.get((i + 1) % 100)).property("date", "2016-01-10");
        edges.add(edge);
    }
    client.graph().addEdges(edges, false);
    // Query vertices and edges count from new created graph
    ResultSet resultSet = client.gremlin().gremlin("g.V().count()").execute();
    Assert.assertEquals(100, resultSet.iterator().next().getInt());
    resultSet = client.gremlin().gremlin("g.E().count()").execute();
    Assert.assertEquals(100, resultSet.iterator().next().getInt());
    // Clear graph schema and data from new created graph
    graphsAPI.clear(GRAPH3, "I'm sure to delete all data");
    resultSet = client.gremlin().gremlin("g.V().count()").execute();
    Assert.assertEquals(0, resultSet.iterator().next().getInt());
    resultSet = client.gremlin().gremlin("g.E().count()").execute();
    Assert.assertEquals(0, resultSet.iterator().next().getInt());
    Assert.assertTrue(client.schema().getPropertyKeys().isEmpty());
    Assert.assertEquals(initialGraphNumber + 1, graphsAPI.list().size());
    // Remove new created graph dynamically
    graphsAPI.drop(GRAPH3, "I'm sure to drop the graph");
    Assert.assertEquals(initialGraphNumber, graphsAPI.list().size());
}
Also used : Vertex(com.baidu.hugegraph.structure.graph.Vertex) HugeClient(com.baidu.hugegraph.driver.HugeClient) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ResultSet(com.baidu.hugegraph.structure.gremlin.ResultSet) ClientException(com.baidu.hugegraph.rest.ClientException) File(java.io.File) Edge(com.baidu.hugegraph.structure.graph.Edge) Test(org.junit.Test)

Example 59 with HugeClient

use of com.baidu.hugegraph.driver.HugeClient in project incubator-hugegraph-toolchain by apache.

the class GraphsApiTest method testCloneAndDropGraphWithoutConfig.

@Test
public void testCloneAndDropGraphWithoutConfig() {
    int initialGraphNumber = graphsAPI.list().size();
    // Clone a new graph from exist a graph dynamically
    String config = null;
    Map<String, String> result = graphsAPI.create(GRAPH3, "hugegraph", config);
    Assert.assertEquals(2, result.size());
    Assert.assertEquals(GRAPH3, result.get("name"));
    Assert.assertEquals("rocksdb", result.get("backend"));
    Assert.assertEquals(initialGraphNumber + 1, graphsAPI.list().size());
    HugeClient client = new HugeClient(baseClient(), GRAPH3);
    // Insert graph schema and data
    initPropertyKey(client);
    initVertexLabel(client);
    initEdgeLabel(client);
    List<Vertex> vertices = new ArrayList<>(100);
    for (int i = 0; i < 100; i++) {
        Vertex vertex = new Vertex("person").property("name", "person" + i).property("city", "Beijing").property("age", 19);
        vertices.add(vertex);
    }
    vertices = client.graph().addVertices(vertices);
    List<Edge> edges = new ArrayList<>(100);
    for (int i = 0; i < 100; i++) {
        Edge edge = new Edge("knows").source(vertices.get(i)).target(vertices.get((i + 1) % 100)).property("date", "2016-01-10");
        edges.add(edge);
    }
    client.graph().addEdges(edges, false);
    // Query vertices and edges count from new created graph
    ResultSet resultSet = client.gremlin().gremlin("g.V().count()").execute();
    Assert.assertEquals(100, resultSet.iterator().next().getInt());
    resultSet = client.gremlin().gremlin("g.E().count()").execute();
    Assert.assertEquals(100, resultSet.iterator().next().getInt());
    // Clear graph schema and data from new created graph
    graphsAPI.clear(GRAPH3, "I'm sure to delete all data");
    resultSet = client.gremlin().gremlin("g.V().count()").execute();
    Assert.assertEquals(0, resultSet.iterator().next().getInt());
    resultSet = client.gremlin().gremlin("g.E().count()").execute();
    Assert.assertEquals(0, resultSet.iterator().next().getInt());
    Assert.assertTrue(client.schema().getPropertyKeys().isEmpty());
    Assert.assertEquals(initialGraphNumber + 1, graphsAPI.list().size());
    // Remove new created graph dynamically
    graphsAPI.drop(GRAPH3, "I'm sure to drop the graph");
    Assert.assertEquals(initialGraphNumber, graphsAPI.list().size());
}
Also used : Vertex(com.baidu.hugegraph.structure.graph.Vertex) HugeClient(com.baidu.hugegraph.driver.HugeClient) ArrayList(java.util.ArrayList) ResultSet(com.baidu.hugegraph.structure.gremlin.ResultSet) Edge(com.baidu.hugegraph.structure.graph.Edge) Test(org.junit.Test)

Example 60 with HugeClient

use of com.baidu.hugegraph.driver.HugeClient 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

HugeClient (com.baidu.hugegraph.driver.HugeClient)66 Vertex (com.baidu.hugegraph.structure.graph.Vertex)21 ArrayList (java.util.ArrayList)16 ExternalException (com.baidu.hugegraph.exception.ExternalException)15 IndexLabel (com.baidu.hugegraph.structure.schema.IndexLabel)14 ServerException (com.baidu.hugegraph.exception.ServerException)12 GraphManager (com.baidu.hugegraph.driver.GraphManager)11 Edge (com.baidu.hugegraph.structure.graph.Edge)10 SchemaManager (com.baidu.hugegraph.driver.SchemaManager)9 ResultSet (com.baidu.hugegraph.structure.gremlin.ResultSet)8 EdgeLabel (com.baidu.hugegraph.structure.schema.EdgeLabel)8 Test (org.junit.Test)8 VertexLabel (com.baidu.hugegraph.structure.schema.VertexLabel)7 PropertyKey (com.baidu.hugegraph.structure.schema.PropertyKey)6 ExecuteHistory (com.baidu.hugegraph.entity.query.ExecuteHistory)4 GraphView (com.baidu.hugegraph.entity.query.GraphView)4 LoadOptions (com.baidu.hugegraph.loader.executor.LoadOptions)4 ClientException (com.baidu.hugegraph.rest.ClientException)4 BeforeClass (org.junit.BeforeClass)4 GraphConnection (com.baidu.hugegraph.entity.GraphConnection)3