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;
}
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;
}
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;
}
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());
}
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());
}
Aggregations