Search in sources :

Example 1 with Result

use of com.baidu.hugegraph.structure.gremlin.Result 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));
    }
}
Also used : Task(com.baidu.hugegraph.structure.Task) GraphMode(com.baidu.hugegraph.structure.constant.GraphMode) AuthBackupRestoreManager(com.baidu.hugegraph.manager.AuthBackupRestoreManager) RestoreManager(com.baidu.hugegraph.manager.RestoreManager) Result(com.baidu.hugegraph.structure.gremlin.Result) DumpGraphManager(com.baidu.hugegraph.manager.DumpGraphManager) ResultSet(com.baidu.hugegraph.structure.gremlin.ResultSet) GraphsManager(com.baidu.hugegraph.manager.GraphsManager) ParameterException(com.beust.jcommander.ParameterException) TasksManager(com.baidu.hugegraph.manager.TasksManager) BackupManager(com.baidu.hugegraph.manager.BackupManager) GremlinManager(com.baidu.hugegraph.manager.GremlinManager) AuthBackupRestoreManager(com.baidu.hugegraph.manager.AuthBackupRestoreManager)

Example 2 with Result

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

the class RestResultTest method testReadGremlinPathWithVertexAndEdge.

@Test
public void testReadGremlinPathWithVertexAndEdge() {
    String json = "{" + "\"requestId\": \"238c74ca-18f7-4377-b8e1-2bb3b165e5d6\"," + "\"status\":{" + "\"message\": \"\"," + "\"code\": 200," + "\"attributes\":{}" + "}," + "\"result\":{" + "\"data\":[" + "{" + "\"labels\":[[], []]," + "\"objects\":[" + "{" + "\"id\": \"person:marko\"," + "\"label\": \"person\"," + "\"type\": \"vertex\"," + "\"properties\":{" + "\"city\":\"Beijing\"," + "\"name\":\"marko\"," + "\"age\":29" + "}" + "}," + "{" + "\"id\": \"person:marko>knows>>person:vadas\"," + "\"label\": \"knows\"," + "\"type\": \"edge\"," + "\"inVLabel\": \"person\"," + "\"outVLabel\": \"person\"," + "\"inV\": \"person:vadas\"," + "\"outV\": \"person:marko\"," + "\"properties\":{" + "\"date\": 1452355200000," + "\"weight\": 0.5" + "}" + "}" + "]" + "}" + "]," + "\"meta\":{" + "}" + "}" + "}";
    Mockito.when(this.mockResponse.getStatus()).thenReturn(200);
    Mockito.when(this.mockResponse.getHeaders()).thenReturn(null);
    Mockito.when(this.mockResponse.readEntity(String.class)).thenReturn(json);
    RestResult restResult = new RestResult(this.mockResponse);
    Assert.assertEquals(200, restResult.status());
    Assert.assertNull(restResult.headers());
    Response response = restResult.readObject(Response.class);
    response.graphManager(graph());
    Assert.assertEquals(200, response.status().code());
    Iterator<Result> results = response.result().iterator();
    Assert.assertTrue(results.hasNext());
    Result result = results.next();
    Object object = result.getObject();
    Assert.assertEquals(Path.class, object.getClass());
    Path path = (Path) object;
    Assert.assertEquals(2, path.labels().size());
    Assert.assertEquals(ImmutableList.of(), path.labels().get(0));
    Assert.assertEquals(ImmutableList.of(), path.labels().get(1));
    Vertex vertex = new Vertex("person");
    vertex.id("person:marko");
    vertex.property("name", "marko");
    vertex.property("age", 29);
    vertex.property("city", "Beijing");
    Edge edge = new Edge("knows");
    edge.id("person:marko>knows>>person:vadas");
    edge.sourceId("person:marko");
    edge.sourceLabel("person");
    edge.targetId("person:vadas");
    edge.targetLabel("person");
    edge.property("date", 1452355200000L);
    edge.property("weight", 0.5);
    Assert.assertEquals(2, path.objects().size());
    Utils.assertGraphEqual(ImmutableList.of(vertex), ImmutableList.of(edge), path.objects());
}
Also used : Response(com.baidu.hugegraph.structure.gremlin.Response) Path(com.baidu.hugegraph.structure.graph.Path) Vertex(com.baidu.hugegraph.structure.graph.Vertex) RestResult(com.baidu.hugegraph.rest.RestResult) Edge(com.baidu.hugegraph.structure.graph.Edge) Result(com.baidu.hugegraph.structure.gremlin.Result) RestResult(com.baidu.hugegraph.rest.RestResult) Test(org.junit.Test)

Example 3 with Result

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

the class RestResultTest method testReadGremlinNullAndVertex.

@Test
public void testReadGremlinNullAndVertex() {
    String json = "{" + "\"requestId\": \"d95ac131-24b5-4140-a3ff-91b0c020764a\"," + "\"status\": {" + "\"message\": \"\"," + "\"code\": 200," + "\"attributes\": {}" + "}," + "\"result\": {" + "\"data\": [" + "null," + "{" + "\"id\": \"person:marko\"," + "\"label\": \"person\"," + "\"type\": \"vertex\"," + "\"properties\": {" + "\"city\": \"Beijing\"," + "\"name\": \"marko\"," + "\"age\": 29" + "}" + "}" + "]," + "\"meta\": {}" + "}" + "}";
    Mockito.when(this.mockResponse.getStatus()).thenReturn(200);
    Mockito.when(this.mockResponse.getHeaders()).thenReturn(null);
    Mockito.when(this.mockResponse.readEntity(String.class)).thenReturn(json);
    RestResult restResult = new RestResult(this.mockResponse);
    Assert.assertEquals(200, restResult.status());
    Assert.assertNull(restResult.headers());
    Response response = restResult.readObject(Response.class);
    response.graphManager(graph());
    Assert.assertEquals(200, response.status().code());
    Iterator<Result> results = response.result().iterator();
    Assert.assertTrue(results.hasNext());
    Result result = results.next();
    Assert.assertNull(result);
    Assert.assertTrue(results.hasNext());
    result = results.next();
    Assert.assertEquals(Vertex.class, result.getObject().getClass());
    Vertex marko = new Vertex("person");
    marko.id("person:marko");
    marko.property("name", "marko");
    marko.property("city", "Beijing");
    marko.property("age", 29);
    Vertex vertex = result.getVertex();
    Assert.assertTrue(Utils.contains(ImmutableList.of(marko), vertex));
}
Also used : Response(com.baidu.hugegraph.structure.gremlin.Response) Vertex(com.baidu.hugegraph.structure.graph.Vertex) RestResult(com.baidu.hugegraph.rest.RestResult) Result(com.baidu.hugegraph.structure.gremlin.Result) RestResult(com.baidu.hugegraph.rest.RestResult) Test(org.junit.Test)

Example 4 with Result

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

the class RestResultTest method testReadGremlinNullData.

@Test
public void testReadGremlinNullData() {
    String json = "{" + "\"requestId\": \"d95ac131-24b5-4140-a3ff-91b0c020764a\"," + "\"status\": {" + "\"message\": \"\"," + "\"code\": 200," + "\"attributes\": {}" + "}," + "\"result\": {" + "\"data\": [null]," + "\"meta\": {}" + "}" + "}";
    Mockito.when(this.mockResponse.getStatus()).thenReturn(200);
    Mockito.when(this.mockResponse.getHeaders()).thenReturn(null);
    Mockito.when(this.mockResponse.readEntity(String.class)).thenReturn(json);
    RestResult restResult = new RestResult(this.mockResponse);
    Assert.assertEquals(200, restResult.status());
    Assert.assertNull(restResult.headers());
    Response response = restResult.readObject(Response.class);
    response.graphManager(graph());
    Assert.assertEquals(200, response.status().code());
    Iterator<Result> results = response.result().iterator();
    Assert.assertTrue(results.hasNext());
    Object object = results.next();
    Assert.assertNull(object);
}
Also used : Response(com.baidu.hugegraph.structure.gremlin.Response) RestResult(com.baidu.hugegraph.rest.RestResult) Result(com.baidu.hugegraph.structure.gremlin.Result) RestResult(com.baidu.hugegraph.rest.RestResult) Test(org.junit.Test)

Example 5 with Result

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

the class RestResultTest method testReadGremlinEdges.

@Test
public void testReadGremlinEdges() {
    String json = "{" + "\"requestId\": \"cd4cfc17-1ee4-4e9e-af40-cb18b115a8dc\"," + "\"status\": {" + "\"message\": \"\"," + "\"code\": 200," + "\"attributes\": {}" + "}," + "\"result\": {" + "\"data\": [" + "{" + "\"id\": \"person:peter>created>>software:lop\"," + "\"label\": \"created\"," + "\"type\": \"edge\"," + "\"inVLabel\": \"software\"," + "\"outVLabel\": \"person\"," + "\"inV\": \"software:lop\"," + "\"outV\": \"person:peter\"," + "\"properties\": {" + "\"date\": 1490284800000," + "\"weight\": 0.2" + "}" + "}," + "{" + "\"id\": \"person:peter>knows>>person:marko\"," + "\"label\": \"knows\"," + "\"type\": \"edge\"," + "\"inVLabel\": \"person\"," + "\"outVLabel\": \"person\"," + "\"inV\": \"person:marko\"," + "\"outV\": \"person:peter\"," + "\"properties\": {" + "\"date\": 1452355200000," + "\"weight\": 0.5" + "}" + "}" + "]," + "\"meta\": {}" + "}" + "}";
    Mockito.when(this.mockResponse.getStatus()).thenReturn(200);
    Mockito.when(this.mockResponse.getHeaders()).thenReturn(null);
    Mockito.when(this.mockResponse.readEntity(String.class)).thenReturn(json);
    RestResult restResult = new RestResult(this.mockResponse);
    Assert.assertEquals(200, restResult.status());
    Assert.assertNull(restResult.headers());
    Response response = restResult.readObject(Response.class);
    response.graphManager(graph());
    Assert.assertEquals(200, response.status().code());
    Edge created = new Edge("created");
    created.id("person:peter>created>>software:lop");
    created.sourceId("person:peter");
    created.targetId("software:lop");
    created.sourceLabel("person");
    created.targetLabel("software");
    created.property("date", 1490284800000L);
    created.property("weight", 0.2);
    Edge knows = new Edge("knows");
    knows.id("person:peter>knows>>person:marko");
    knows.sourceId("person:peter");
    knows.targetId("person:marko");
    knows.sourceLabel("person");
    knows.targetLabel("person");
    knows.property("date", 1452355200000L);
    knows.property("weight", 0.5);
    List<Edge> edges = new ArrayList<>(2);
    edges.add(created);
    edges.add(knows);
    Iterator<Result> results = response.result().iterator();
    while (results.hasNext()) {
        Result result = results.next();
        Assert.assertEquals(Edge.class, result.getObject().getClass());
        Edge edge = result.getEdge();
        Assert.assertTrue(Utils.contains(edges, edge));
    }
}
Also used : Response(com.baidu.hugegraph.structure.gremlin.Response) RestResult(com.baidu.hugegraph.rest.RestResult) ArrayList(java.util.ArrayList) Edge(com.baidu.hugegraph.structure.graph.Edge) Result(com.baidu.hugegraph.structure.gremlin.Result) RestResult(com.baidu.hugegraph.rest.RestResult) Test(org.junit.Test)

Aggregations

Result (com.baidu.hugegraph.structure.gremlin.Result)16 Vertex (com.baidu.hugegraph.structure.graph.Vertex)10 ResultSet (com.baidu.hugegraph.structure.gremlin.ResultSet)9 Test (org.junit.Test)9 Edge (com.baidu.hugegraph.structure.graph.Edge)8 RestResult (com.baidu.hugegraph.rest.RestResult)6 Path (com.baidu.hugegraph.structure.graph.Path)6 Response (com.baidu.hugegraph.structure.gremlin.Response)6 ArrayList (java.util.ArrayList)5 HugeClient (com.baidu.hugegraph.driver.HugeClient)4 GremlinResult (com.baidu.hugegraph.entity.query.GremlinResult)4 TypedResult (com.baidu.hugegraph.entity.query.TypedResult)4 GremlinRequest (com.baidu.hugegraph.api.gremlin.GremlinRequest)3 HashMap (java.util.HashMap)3 SchemaManager (com.baidu.hugegraph.driver.SchemaManager)2 GraphView (com.baidu.hugegraph.entity.query.GraphView)2 Type (com.baidu.hugegraph.entity.query.GremlinResult.Type)2 ExternalException (com.baidu.hugegraph.exception.ExternalException)2 ServerException (com.baidu.hugegraph.exception.ServerException)2 ImmutableMap (com.google.common.collect.ImmutableMap)2