Arangodb 图操作--Java Driver

上一篇介绍了Arangodb Java driver 基本操作:Arangodb-Java-driver 使用

建立连接

ArangoDB arangoDB = new ArangoDB.Builder()
                .host("127.0.0.1", 8529)
                .user("myuser")
                .password("mypassword")
                .useProtocol(Protocol.VST)
                .build();

创建顶点集合

 //创建顶点集合v_user
arangoDB.db("database").createCollection("v_user");

创建边集合

//创建边集合e_user_relation
        CollectionCreateOptions coptions = new CollectionCreateOptions();
        coptions.type(CollectionType.EDGES);
        arangoDB.db("database").createCollection("e_user_relation", coptions);


批量插入顶点数据

//批量插入顶点数据
        Collection<String> data = new ArrayList<String>();
        data.add("{" +
                "  \"_key\": \"u3\"," +
                "  \"name\":\"zhangsan\"" +
                "}");
        data.add("{" +
                "  \"_key\": \"u4\"," +
                "  \"name\":\"lisi\"" +
                "}");
        data.add("{" +
                "  \"_key\": \"u5\"," +
                "  \"name\":\"wangwu\"" +
                "}");
        data.add("{" +
                "  \"_key\": \"u6\"," +
                "  \"name\":\"zhaoliu\"" +
                "}");

        arangoDB.db("database").collection("v_user").insertDocuments(data);


批量插入边数据

//批量插入边数据
        data = new ArrayList<String>();
        data.add("{" +
                "  \"_key\": \"u3_u4\"," +
                "  \"_from\": \"v_user/u3\"," +
                "  \"_to\": \"v_user/u4\"," +
                "  \"relation\": \"is_father\"" +
                "}");
        data.add("{" +
                "  \"_key\": \"u4_u5\"," +
                "  \"_from\": \"v_user/u4\"," +
                "  \"_to\": \"v_user/u5\"," +
                "  \"relation\": \"is_father\"" +
                "}");

        arangoDB.db("database").collection("e_user_relation").insertDocuments(data);

创建图

//创建图
        Collection<EdgeDefinition> edgeDefinitions = new ArrayList<EdgeDefinition>();
        EdgeDefinition edgeDefinition = new EdgeDefinition();

        edgeDefinition.collection("e_user_relation");

        edgeDefinition.from("v_user");

        edgeDefinition.to("v_user");

        edgeDefinitions.add(edgeDefinition);

        GraphCreateOptions goptions = new GraphCreateOptions();
        goptions.orphanCollections("myGraph");

        arangoDB.db("database").createGraph("myGraph", edgeDefinitions, goptions);


使用AQL查询图

//使用AQL查询, 结果 (u3)-[is_father]->(u4)-[is_father]->(u5)
        String query = "FOR v,e,p IN 2 OUTBOUND 'v_user/u3' GRAPH 'myGraph' " +
                "return p";

        ArangoCursor<Map> result = arangoDB.db("database").query(query, null, null, Map.class);

        while (result.hasNext()) {
            Map path = result.next();
            System.out.println(path);
        }


使用图接口插入顶点和边

//新增一个顶点
        String newVertex = "{" +
                "  \"_key\": \"u7\"," +
                "  \"name\":\"chen7\"" +
                "}";
        arangoDB.db("database").graph("myGraph").vertexCollection("v_user").insertVertex(newVertex);

        //新增加一条表 (u4)-[is_father]->(u6)
        String newEdge = "{" +
                "  \"_key\": \"u4_u7\"," +
                "  \"_from\": \"v_user/u4\"," +
                "  \"_to\": \"v_user/u7\"," +
                "  \"relation\": \"is_father\"" +
                "}";
        arangoDB.db("database").graph("myGraph").edgeCollection("e_user_relation").insertEdge(newEdge);


使用AQL查询图

//使用AQL查询, 将有两条结果 (u3)-[is_father]->(u4)-[is_father]->(u5),(u3)-[is_father]->(u4)-[is_father]->(u7)
        query = "FOR v,e,p IN 2 OUTBOUND 'v_user/u3' GRAPH 'myGraph' " +
                "return p";

        result = arangoDB.db("database").query(query, null, null, Map.class);

        while (result.hasNext()) {
            Map path = result.next();
            System.out.println(path);
        }


完整示例代码:

import com.arangodb.ArangoCursor;
import com.arangodb.ArangoDB;
import com.arangodb.Protocol;
import com.arangodb.entity.CollectionType;
import com.arangodb.entity.EdgeDefinition;
import com.arangodb.model.CollectionCreateOptions;
import com.arangodb.model.GraphCreateOptions;
import com.fasterxml.jackson.core.JsonProcessingException;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;

public class GraphTest {
    public static void main(String[] args) throws JsonProcessingException {
        ArangoDB arangoDB = new ArangoDB.Builder()
                .host("127.0.0.1", 8529)
                .user("myuser")
                .password("mypassword")
                .useProtocol(Protocol.VST)
                .build();

        arangoDB.db("database").graph("myGraph").drop();
        arangoDB.db("database").collection("v_user").drop();
        arangoDB.db("database").collection("e_user_relation").drop();

        //创建顶点集合v_user
        arangoDB.db("database").createCollection("v_user");

        //创建边集合e_user_relation
        CollectionCreateOptions coptions = new CollectionCreateOptions();
        coptions.type(CollectionType.EDGES);
        arangoDB.db("database").createCollection("e_user_relation", coptions);

        //批量插入顶点数据
        Collection<String> data = new ArrayList<String>();
        data.add("{" +
                "  \"_key\": \"u3\"," +
                "  \"name\":\"zhangsan\"" +
                "}");
        data.add("{" +
                "  \"_key\": \"u4\"," +
                "  \"name\":\"lisi\"" +
                "}");
        data.add("{" +
                "  \"_key\": \"u5\"," +
                "  \"name\":\"wangwu\"" +
                "}");
        data.add("{" +
                "  \"_key\": \"u6\"," +
                "  \"name\":\"zhaoliu\"" +
                "}");

        arangoDB.db("database").collection("v_user").insertDocuments(data);

        //批量插入边数据
        data = new ArrayList<String>();
        data.add("{" +
                "  \"_key\": \"u3_u4\"," +
                "  \"_from\": \"v_user/u3\"," +
                "  \"_to\": \"v_user/u4\"," +
                "  \"relation\": \"is_father\"" +
                "}");
        data.add("{" +
                "  \"_key\": \"u4_u5\"," +
                "  \"_from\": \"v_user/u4\"," +
                "  \"_to\": \"v_user/u5\"," +
                "  \"relation\": \"is_father\"" +
                "}");

        arangoDB.db("database").collection("e_user_relation").insertDocuments(data);

        //创建图
        Collection<EdgeDefinition> edgeDefinitions = new ArrayList<EdgeDefinition>();
        EdgeDefinition edgeDefinition = new EdgeDefinition();

        edgeDefinition.collection("e_user_relation");

        edgeDefinition.from("v_user");

        edgeDefinition.to("v_user");

        edgeDefinitions.add(edgeDefinition);

        GraphCreateOptions goptions = new GraphCreateOptions();
        goptions.orphanCollections("myGraph");

        arangoDB.db("database").createGraph("myGraph", edgeDefinitions, goptions);

        //使用AQL查询, 结果 (u3)-[is_father]->(u4)-[is_father]->(u5)
        String query = "FOR v,e,p IN 2 OUTBOUND 'v_user/u3' GRAPH 'myGraph' " +
                "return p";

        ArangoCursor<Map> result = arangoDB.db("database").query(query, null, null, Map.class);

        while (result.hasNext()) {
            Map path = result.next();
            System.out.println(path);
        }

        System.out.println("-------------");
        //新增一个顶点
        String newVertex = "{" +
                "  \"_key\": \"u7\"," +
                "  \"name\":\"chen7\"" +
                "}";
        arangoDB.db("database").graph("myGraph").vertexCollection("v_user").insertVertex(newVertex);

        //新增加一条表 (u4)-[is_father]->(u6)
        String newEdge = "{" +
                "  \"_key\": \"u4_u7\"," +
                "  \"_from\": \"v_user/u4\"," +
                "  \"_to\": \"v_user/u7\"," +
                "  \"relation\": \"is_father\"" +
                "}";
        arangoDB.db("database").graph("myGraph").edgeCollection("e_user_relation").insertEdge(newEdge);

        //使用AQL查询, 将有两条结果 (u3)-[is_father]->(u4)-[is_father]->(u5),(u3)-[is_father]->(u4)-[is_father]->(u7)
        query = "FOR v,e,p IN 2 OUTBOUND 'v_user/u3' GRAPH 'myGraph' " +
                "return p";

        result = arangoDB.db("database").query(query, null, null, Map.class);

        while (result.hasNext()) {
            Map path = result.next();
            System.out.println(path);
        }

    }

}

个人资料
时海
等级:8
文章:272篇
访问:16.0w
排名: 2
上一篇: Arangodb基本操作--Java Driver
下一篇:CCKS 2017-全国知识图谱与语义计算大会
猜你感兴趣的圈子:
图存储于图计算
标签: arangodb、u4、edgedefinition、father、user、面试题
隐藏