use of com.baidu.hugegraph.structure.gremlin.ResultSet in project incubator-hugegraph-toolchain by apache.
the class HugeGraphCommand method execute.
private void execute(String subCmd, JCommander jCommander) {
this.checkMainParams();
switch(subCmd) {
case "backup":
if (this.timeout() < BACKUP_DEFAULT_TIMEOUT) {
this.timeout(BACKUP_DEFAULT_TIMEOUT);
}
Printer.print("Graph '%s' start backup!", this.graph());
SubCommands.Backup backup = this.subCommand(subCmd);
BackupManager backupManager = manager(BackupManager.class);
backupManager.init(backup);
backupManager.backup(backup.types());
break;
case "restore":
GraphsManager graphsManager = manager(GraphsManager.class);
GraphMode mode = graphsManager.mode(this.graph());
E.checkState(mode.maintaining(), "Invalid mode '%s' of graph '%s' for restore " + "sub-command", mode, this.graph());
Printer.print("Graph '%s' start restore in mode '%s'!", this.graph(), mode);
SubCommands.Restore restore = this.subCommand(subCmd);
RestoreManager restoreManager = manager(RestoreManager.class);
restoreManager.init(restore);
restoreManager.mode(mode);
restoreManager.restore(restore.types());
break;
case "migrate":
SubCommands.Migrate migrate = this.subCommand(subCmd);
Printer.print("Migrate graph '%s' from '%s' to '%s' as '%s'", this.graph(), this.url(), migrate.targetUrl(), migrate.targetGraph());
// Backup source graph
if (this.timeout() < BACKUP_DEFAULT_TIMEOUT) {
this.timeout(BACKUP_DEFAULT_TIMEOUT);
}
backup = convMigrate2Backup(migrate);
backupManager = manager(BackupManager.class);
backupManager.init(backup);
backupManager.backup(backup.types());
// Restore source graph to target graph
this.url(migrate.targetUrl());
this.graph(migrate.targetGraph());
this.username(migrate.targetUsername());
this.password(migrate.targetPassword());
this.timeout(migrate.targetTimeout());
this.trustStoreFile(migrate.targetTrustStoreFile());
this.trustStorePassword(migrate.targetTrustStorePassword());
graphsManager = manager(GraphsManager.class);
GraphMode origin = graphsManager.mode(migrate.targetGraph());
// Set target graph mode
mode = migrate.mode();
E.checkState(mode.maintaining(), "Invalid mode '%s' of graph '%s' for restore", mode, migrate.targetGraph());
graphsManager.mode(migrate.targetGraph(), mode);
// Restore
Printer.print("Graph '%s' start restore in mode '%s'!", migrate.targetGraph(), migrate.mode());
String directory = backupManager.directory().directory();
restore = convMigrate2Restore(migrate, directory);
restoreManager = manager(RestoreManager.class);
restoreManager.init(restore);
restoreManager.mode(mode);
restoreManager.restore(restore.types());
// Restore target graph mode
graphsManager.mode(migrate.targetGraph(), origin);
break;
case "dump":
Printer.print("Graph '%s' start dump!", this.graph());
SubCommands.DumpGraph dump = this.subCommand(subCmd);
DumpGraphManager dumpManager = manager(DumpGraphManager.class);
dumpManager.init(dump);
dumpManager.dumpFormatter(dump.formatter());
dumpManager.dump();
break;
case "graph-create":
SubCommands.GraphCreate graphCreate = this.subCommand(subCmd);
if (timeout() < DEFAULT_GRAPH_CREATE_TIMEOUT) {
this.timeout(DEFAULT_GRAPH_CREATE_TIMEOUT);
}
graphsManager = manager(GraphsManager.class);
graphsManager.create(graphCreate.name(), graphCreate.config());
Printer.print("Graph '%s' is created", graphCreate.name());
break;
case "graph-clone":
SubCommands.GraphClone graphClone = this.subCommand(subCmd);
if (timeout() < DEFAULT_GRAPH_CREATE_TIMEOUT) {
this.timeout(DEFAULT_GRAPH_CREATE_TIMEOUT);
}
graphsManager = manager(GraphsManager.class);
graphsManager.clone(graphClone.name(), graphClone.cloneGraphName());
Printer.print("Graph '%s' is created(cloned from '%s')", graphClone.name(), graphClone.cloneGraphName());
break;
case "graph-list":
graphsManager = manager(GraphsManager.class);
Printer.printList("Graphs", graphsManager.list());
break;
case "graph-get":
graphsManager = manager(GraphsManager.class);
Printer.printMap("Graph info", graphsManager.get(this.graph()));
break;
case "graph-clear":
SubCommands.GraphClear graphClear = this.subCommand(subCmd);
if (timeout() < DEFAULT_GRAPH_CLEAR_TIMEOUT) {
this.timeout(DEFAULT_GRAPH_CLEAR_TIMEOUT);
}
graphsManager = manager(GraphsManager.class);
graphsManager.clear(this.graph(), graphClear.confirmMessage());
Printer.print("Graph '%s' is cleared", this.graph());
break;
case "graph-drop":
SubCommands.GraphDrop graphDrop = this.subCommand(subCmd);
if (timeout() < DEFAULT_GRAPH_CLEAR_TIMEOUT) {
this.timeout(DEFAULT_GRAPH_CLEAR_TIMEOUT);
}
graphsManager = manager(GraphsManager.class);
graphsManager.drop(this.graph(), graphDrop.confirmMessage());
Printer.print("Graph '%s' is dropped", this.graph());
break;
case "graph-mode-set":
SubCommands.GraphModeSet graphModeSet = this.subCommand(subCmd);
graphsManager = manager(GraphsManager.class);
graphsManager.mode(this.graph(), graphModeSet.mode());
Printer.print("Set graph '%s' mode to '%s'", this.graph(), graphModeSet.mode());
break;
case "graph-mode-get":
graphsManager = manager(GraphsManager.class);
Printer.printKV("Graph mode", graphsManager.mode(this.graph()));
break;
case "gremlin-execute":
SubCommands.Gremlin gremlin = this.subCommand(subCmd);
GremlinManager gremlinManager = manager(GremlinManager.class);
Printer.print("Run gremlin script");
ResultSet result = gremlinManager.execute(gremlin.script(), gremlin.bindings(), gremlin.language(), gremlin.aliases());
Iterator<Result> iterator = result.iterator();
while (iterator.hasNext()) {
Printer.print(iterator.next().getString());
}
break;
case "gremlin-schedule":
SubCommands.GremlinJob job = this.subCommand(subCmd);
gremlinManager = manager(GremlinManager.class);
Printer.print("Run gremlin script as async job");
long taskId = gremlinManager.executeAsTask(job.script(), job.bindings(), job.language());
Printer.printKV("Task id", taskId);
break;
case "task-list":
SubCommands.TaskList taskList = this.subCommand(subCmd);
TasksManager tasksManager = manager(TasksManager.class);
List<Task> tasks = tasksManager.list(taskList.status(), taskList.limit());
List<Object> results = tasks.stream().map(Task::asMap).collect(Collectors.toList());
Printer.printList("Tasks", results);
break;
case "task-get":
SubCommands.TaskGet taskGet = this.subCommand(subCmd);
tasksManager = manager(TasksManager.class);
Printer.printKV("Task info", tasksManager.get(taskGet.taskId()).asMap());
break;
case "task-delete":
SubCommands.TaskDelete taskDelete = this.subCommand(subCmd);
tasksManager = manager(TasksManager.class);
tasksManager.delete(taskDelete.taskId());
Printer.print("Task '%s' is deleted", taskDelete.taskId());
break;
case "task-cancel":
SubCommands.TaskCancel taskCancel = this.subCommand(subCmd);
tasksManager = manager(TasksManager.class);
tasksManager.cancel(taskCancel.taskId());
Printer.print("Task '%s' is cancelled", taskCancel.taskId());
break;
case "task-clear":
SubCommands.TaskClear taskClear = this.subCommand(subCmd);
tasksManager = manager(TasksManager.class);
tasksManager.clear(taskClear.force());
Printer.print("Tasks are cleared[force=%s]", taskClear.force());
break;
case "auth-backup":
Printer.print("Auth backup start...");
SubCommands.AuthBackup authBackup = this.subCommand(subCmd);
AuthBackupRestoreManager authBackupManager = manager(AuthBackupRestoreManager.class);
authBackupManager.init(authBackup);
authBackupManager.backup(authBackup.types());
break;
case "auth-restore":
Printer.print("Auth restore start...");
SubCommands.AuthRestore authRestore = this.subCommand(subCmd);
AuthBackupRestoreManager authRestoreManager = manager(AuthBackupRestoreManager.class);
authRestoreManager.init(authRestore);
authRestoreManager.restore(authRestore.types());
break;
default:
throw new ParameterException(String.format("Invalid sub-command: %s", subCmd));
}
}
use of com.baidu.hugegraph.structure.gremlin.ResultSet 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.structure.gremlin.ResultSet in project incubator-hugegraph-toolchain by apache.
the class GremlinApiTest method testQueryAllEdges.
@Test
public void testQueryAllEdges() {
GremlinRequest request = new GremlinRequest("g.E()");
ResultSet resultSet = gremlin().execute(request);
Assert.assertEquals(6, resultSet.size());
request = new GremlinRequest("g.E().drop()");
gremlin().execute(request);
request = new GremlinRequest("g.E()");
resultSet = gremlin().execute(request);
Assert.assertEquals(0, resultSet.size());
}
use of com.baidu.hugegraph.structure.gremlin.ResultSet in project incubator-hugegraph-toolchain by apache.
the class GremlinApiTest method testIterateEmptyResultSet.
@Test
public void testIterateEmptyResultSet() {
GremlinRequest request = new GremlinRequest("g.V().limit(0)");
ResultSet resultSet = gremlin().execute(request);
Assert.assertEquals(0, resultSet.size());
Assert.assertThrows(NoSuchElementException.class, () -> {
resultSet.iterator().next();
});
}
use of com.baidu.hugegraph.structure.gremlin.ResultSet in project incubator-hugegraph-toolchain by apache.
the class GremlinApiTest method testQueryAllVertices.
@Test
public void testQueryAllVertices() {
GremlinRequest request = new GremlinRequest("g.V()");
ResultSet resultSet = gremlin().execute(request);
Assert.assertEquals(6, resultSet.size());
request = new GremlinRequest("g.V().drop()");
gremlin().execute(request);
request = new GremlinRequest("g.V()");
resultSet = gremlin().execute(request);
Assert.assertEquals(0, resultSet.size());
}
Aggregations