Search in sources :

Example 1 with ClientException

use of com.baidu.hugegraph.rest.ClientException in project incubator-hugegraph-toolchain by apache.

the class LocalDirectory method zipInputStream.

private ZipInputStream zipInputStream(String file) {
    String path = Paths.get(this.directory(), file).toString();
    InputStream is = null;
    ZipInputStream zis;
    try {
        is = new FileInputStream(path);
        zis = new ZipInputStream(is);
        E.checkState(zis.getNextEntry() != null, "Invalid zip file '%s'", file);
    } catch (IOException | IllegalStateException e) {
        closeAndIgnoreException(is);
        throw new ClientException("Failed to read from local file: %s", e, path);
    }
    return zis;
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) ClientException(com.baidu.hugegraph.rest.ClientException) FileInputStream(java.io.FileInputStream)

Example 2 with ClientException

use of com.baidu.hugegraph.rest.ClientException in project incubator-hugegraph-toolchain by apache.

the class HdfsDirectory method inputStream.

@Override
public InputStream inputStream(String file) {
    String path = this.path(file);
    FileSystem fs = this.fileSystem();
    FSDataInputStream is = null;
    ZipInputStream zis;
    Path source = new Path(path);
    try {
        is = fs.open(source);
        zis = new ZipInputStream(is);
        E.checkState(zis.getNextEntry() != null, "Invalid zip file '%s'", file);
    } catch (IOException e) {
        closeAndIgnoreException(is);
        throw new ClientException("Failed to read from %s", e, path);
    }
    return zis;
}
Also used : Path(org.apache.hadoop.fs.Path) ZipInputStream(java.util.zip.ZipInputStream) FileSystem(org.apache.hadoop.fs.FileSystem) FSDataInputStream(org.apache.hadoop.fs.FSDataInputStream) IOException(java.io.IOException) ClientException(com.baidu.hugegraph.rest.ClientException)

Example 3 with ClientException

use of com.baidu.hugegraph.rest.ClientException in project incubator-hugegraph-toolchain by apache.

the class HdfsDirectory method outputStream.

@Override
public OutputStream outputStream(String file, boolean compress, boolean override) {
    String path = this.path(file + this.suffix(compress));
    FileSystem fs = this.fileSystem();
    FSDataOutputStream os = null;
    ZipOutputStream zos = null;
    Path dest = new Path(path);
    try {
        if (override) {
            os = fs.create(dest, true);
        } else {
            os = fs.append(dest);
        }
        if (!compress) {
            return os;
        }
        zos = new ZipOutputStream(os);
        ZipEntry entry = new ZipEntry(file);
        zos.putNextEntry(entry);
    } catch (IOException e) {
        closeAndIgnoreException(zos);
        closeAndIgnoreException(os);
        throw new ClientException("Failed to write to %s", e, path);
    }
    return zos;
}
Also used : Path(org.apache.hadoop.fs.Path) ZipOutputStream(java.util.zip.ZipOutputStream) FileSystem(org.apache.hadoop.fs.FileSystem) ZipEntry(java.util.zip.ZipEntry) FSDataOutputStream(org.apache.hadoop.fs.FSDataOutputStream) IOException(java.io.IOException) ClientException(com.baidu.hugegraph.rest.ClientException)

Example 4 with ClientException

use of com.baidu.hugegraph.rest.ClientException in project incubator-hugegraph-toolchain by apache.

the class GraphsApiTest method testCreateAndDropGraph.

@Test
public void testCreateAndDropGraph() {
    int initialGraphNumber = graphsAPI.list().size();
    // Create new graph dynamically
    String config;
    try {
        config = FileUtils.readFileToString(new File(CONFIG2_PATH), StandardCharsets.UTF_8);
    } catch (IOException e) {
        throw new ClientException("Failed to read config file: %s", CONFIG2_PATH);
    }
    Map<String, String> result = graphsAPI.create(GRAPH2, null, config);
    Assert.assertEquals(2, result.size());
    Assert.assertEquals(GRAPH2, result.get("name"));
    Assert.assertEquals("rocksdb", result.get("backend"));
    Assert.assertEquals(initialGraphNumber + 1, graphsAPI.list().size());
    HugeClient client = new HugeClient(baseClient(), GRAPH2);
    // 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(GRAPH2, "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(GRAPH2, "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 5 with ClientException

use of com.baidu.hugegraph.rest.ClientException 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)

Aggregations

ClientException (com.baidu.hugegraph.rest.ClientException)8 IOException (java.io.IOException)6 HugeClient (com.baidu.hugegraph.driver.HugeClient)3 ResultSet (com.baidu.hugegraph.structure.gremlin.ResultSet)3 ServerException (com.baidu.hugegraph.exception.ServerException)2 Edge (com.baidu.hugegraph.structure.graph.Edge)2 Vertex (com.baidu.hugegraph.structure.graph.Vertex)2 File (java.io.File)2 ArrayList (java.util.ArrayList)2 ZipEntry (java.util.zip.ZipEntry)2 ZipInputStream (java.util.zip.ZipInputStream)2 ZipOutputStream (java.util.zip.ZipOutputStream)2 FileSystem (org.apache.hadoop.fs.FileSystem)2 Path (org.apache.hadoop.fs.Path)2 Test (org.junit.Test)2 HugeClientBuilder (com.baidu.hugegraph.driver.HugeClientBuilder)1 ExternalException (com.baidu.hugegraph.exception.ExternalException)1 LoadException (com.baidu.hugegraph.loader.exception.LoadException)1 Result (com.baidu.hugegraph.structure.gremlin.Result)1 FileInputStream (java.io.FileInputStream)1