Search in sources :

Example 21 with HugeClient

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

the class InsertTask method insertBatch.

@SuppressWarnings("unchecked")
protected void insertBatch(List<Record> batch, boolean checkVertex) {
    HugeClient client = this.context.client();
    List<GraphElement> elements = new ArrayList<>(batch.size());
    batch.forEach(r -> elements.add(r.element()));
    if (this.type().isVertex()) {
        client.graph().addVertices((List<Vertex>) (Object) elements);
    } else {
        client.graph().addEdges((List<Edge>) (Object) elements, checkVertex);
    }
}
Also used : Vertex(com.baidu.hugegraph.structure.graph.Vertex) HugeClient(com.baidu.hugegraph.driver.HugeClient) GraphElement(com.baidu.hugegraph.structure.GraphElement) ArrayList(java.util.ArrayList) Edge(com.baidu.hugegraph.structure.graph.Edge)

Example 22 with HugeClient

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

the class AuthManagerTest method testAuth.

@Test
public void testAuth() {
    User user = new User();
    user.name("bob");
    user.password("123456");
    user = auth().createUser(user);
    Group group = new Group();
    group.name("managers");
    group = auth().createGroup(group);
    Target gremlin = new Target();
    gremlin.name("gremlin");
    gremlin.graph("hugegraph");
    gremlin.url("127.0.0.1:8080");
    gremlin.resources(new HugeResource(HugeResourceType.GREMLIN));
    gremlin = auth().createTarget(gremlin);
    Target task = new Target();
    task.name("task");
    task.graph("hugegraph");
    task.url("127.0.0.1:8080");
    task.resources(new HugeResource(HugeResourceType.TASK));
    task = auth().createTarget(task);
    Belong belong = new Belong();
    belong.user(user);
    belong.group(group);
    belong = auth().createBelong(belong);
    Access access1 = new Access();
    access1.group(group);
    access1.target(gremlin);
    access1.permission(HugePermission.EXECUTE);
    access1 = auth().createAccess(access1);
    Access access2 = new Access();
    access2.group(group);
    access2.target(task);
    access2.permission(HugePermission.READ);
    access2 = auth().createAccess(access2);
    Project project1 = new Project("test");
    project1 = auth().createProject(project1);
    Assert.assertEquals("test", project1.name());
    Project project2 = new Project("test2");
    project2 = auth().createProject(project2);
    Assert.assertEquals("test2", project2.name());
    Project newProject1 = auth().getProject(project1);
    Assert.assertEquals(newProject1.id(), project1.id());
    Assert.assertTrue(CollectionUtils.isEmpty(newProject1.graphs()));
    List<Project> projects = auth().listProjects();
    Assert.assertNotNull(projects);
    Assert.assertEquals(2, projects.size());
    Set<String> graphs = ImmutableSet.of("graph1", "graph2");
    newProject1 = auth().projectAddGraphs(project1, graphs);
    Assert.assertNotNull(newProject1);
    Assert.assertEquals(graphs, newProject1.graphs());
    graphs = ImmutableSet.of("graph2");
    newProject1 = auth().projectRemoveGraphs(project1, ImmutableSet.of("graph1"));
    Assert.assertNotNull(newProject1);
    Assert.assertEquals(graphs, newProject1.graphs());
    Object project1Id = project1.id();
    project1 = new Project(project1Id);
    project1.description("test description");
    newProject1 = auth().updateProject(project1);
    Assert.assertEquals(newProject1.description(), project1.description());
    auth().deleteProject(project2);
    projects.remove(project2);
    List<Project> newProjects = auth().listProjects();
    Assert.assertEquals(newProjects, projects);
    UserRole role = auth().getUserRole(user);
    String r = "{\"roles\":{\"hugegraph\":" + "{\"READ\":[{\"type\":\"TASK\",\"label\":\"*\",\"properties\":null}]," + "\"EXECUTE\":[{\"type\":\"GREMLIN\",\"label\":\"*\",\"properties\":null}]}}}";
    Assert.assertEquals(r, role.toString());
    Login login = new Login();
    login.name("bob");
    login.password("123456");
    LoginResult result = auth().login(login);
    String token = result.token();
    HugeClient client = baseClient();
    client.setAuthContext("Bearer " + token);
    TokenPayload payload = auth().verifyToken();
    Assert.assertEquals("bob", payload.username());
    Assert.assertEquals(user.id(), payload.userId());
    auth().logout();
    client.resetAuthContext();
}
Also used : Group(com.baidu.hugegraph.structure.auth.Group) HugeClient(com.baidu.hugegraph.driver.HugeClient) User(com.baidu.hugegraph.structure.auth.User) LoginResult(com.baidu.hugegraph.structure.auth.LoginResult) Access(com.baidu.hugegraph.structure.auth.Access) Login(com.baidu.hugegraph.structure.auth.Login) TokenPayload(com.baidu.hugegraph.structure.auth.TokenPayload) Project(com.baidu.hugegraph.structure.auth.Project) Target(com.baidu.hugegraph.structure.auth.Target) UserRole(com.baidu.hugegraph.structure.auth.User.UserRole) HugeResource(com.baidu.hugegraph.structure.auth.HugeResource) Belong(com.baidu.hugegraph.structure.auth.Belong) Test(org.junit.Test)

Example 23 with HugeClient

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

the class GraphConnectionController method get.

@GetMapping("{id}")
public GraphConnection get(@PathVariable("id") int id) {
    GraphConnection entity = this.connService.get(id);
    if (entity == null) {
        throw new ExternalException("graph-connection.not-exist.id", id);
    }
    if (!this.poolService.containsKey(id)) {
        this.sslService.configSSL(this.config, entity);
        HugeClient client = HugeClientUtil.tryConnect(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) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 24 with HugeClient

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

the class HugeClientTest method testContext.

@Test
public void testContext() {
    HugeClient client = HugeClient.builder(BASE_URL, GRAPH).configUser(USERNAME, PASSWORD).build();
    String token = "Bearer token";
    client.setAuthContext(token);
    Assert.assertEquals(token, client.getAuthContext());
    client.resetAuthContext();
    Assert.assertNull(client.getAuthContext());
}
Also used : HugeClient(com.baidu.hugegraph.driver.HugeClient) Test(org.junit.Test)

Example 25 with HugeClient

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

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