美文网首页数客联盟
Apache TinkerPop快速体验

Apache TinkerPop快速体验

作者: Woople | 来源:发表于2019-11-28 18:48 被阅读0次

    什么是TinkerPop

    Apache TinkerPop is a graph computing framework for both graph databases (OLTP) and graph analytic systems (OLAP).

    下面开始快速体验一下TinkerPop是如何进行图查询的。

    基本概念

    在TinkerPop的定义中,一个图就是许多点和边的集合,一个点代表了某个领域对象,而边表示了连接的两个点之间的关系。

    A graph is a collection of vertices (i.e., nodes, dots) and edges (i.e., relationships, lines), where a vertex is an entity which represents some domain object (e.g., a person or a place) and an edge represents the relationship between two vertices.

    点和边都是由很多attributes构成的。而其中两个属性“label”和“id”算是系统预留的。

    The "label" and the "id" are reserved attributes of vertices and edges

    样例图

    下面是TinkerPop内部自带的一个图

    • 每个顶点和边都具有唯一标识的id,例如顶点1,边9
    • 每个顶点和边都有label,顶点有两类label,personsoftware;而边也有两类label,createdknows
    • 边是有方向的,例如边9,方向是从顶点1出发进入到顶点3
    • 每个边还有一个属性weight
    • person类型的顶点还有name和age两个属性;software类型的顶点有name和lang两个属性
    tinkerpop-modern

    简单的遍历

    基于上面的图实现What software has Marko created?

    首先需要在pom文件中引入相关依赖

    <dependency>
        <groupId>org.apache.tinkerpop</groupId>
        <artifactId>gremlin-core</artifactId>
        <version>3.4.4</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tinkerpop</groupId>
        <artifactId>tinkergraph-gremlin</artifactId>
        <version>3.4.4</version>
    </dependency>
    

    具体实现

    public class StayingSimple {
        public static void main(String[] args) {
            TinkerGraph tg = TinkerFactory.createModern(); //1
            GraphTraversalSource g = tg.traversal(); //2
            //What software has Marko created?
            GraphTraversal graphTraversal = g.V() //3
                    .has("person","name","marko") //4
                    .out("created") //5
                    .values("name"); //6
          
            while (graphTraversal.hasNext()){ //7
                Object object = graphTraversal.next();
                System.out.println(object);
            }
        }
    }
    
    1. 创建一个内置的图
    2. 获取一个图遍历器用来执行各种遍历查询
    3. 获取图的所有顶点
    4. 找到label是person,且name为marko的顶点
    5. 找到所有从marko顶点出去的边连接的顶点,且边的label为created
    6. 返回所有符合条件的顶点的name属性值
    7. 遍历所有结果并输出,输出结果为lop,即回答了上面的问题Marko created software lop,这个结果与上图显示的一致。

    进一步查找

    现在进一步分析Who are the people that marko develops software with?

    具体实现

    public class StayingSimplePlus {
        public static void main(String[] args) {
            TinkerGraph tg = TinkerFactory.createModern();
            GraphTraversalSource g = tg.traversal();
            //Who are the people that marko develops software with?
    
            GraphTraversal graphTraversal = g.V()
                    .has("person", "name", "marko")
                    .as("exclude")
                    .out("created") //1
                    .in("created") //2
                    .where(P.neq("exclude")) //3
                    .values("name");
    
            while (graphTraversal.hasNext()) { //4
                Object object = graphTraversal.next();
                System.out.println(object);
            }
        }
    }
    
    1. 和上面的例子一样可以找到marko开发软件的顶点

    2. 找到所有进入这个顶点的边连接的顶点,且边的label为created


    3. 过滤掉marko这个顶点

    4. 输出结果为josh和peter,至此找到了和marko一起开发lop这个软件的人是josh和peter。

    总结

    通过上面两个例子快速体验了在TinkerPop中是如何进行图查找的。

    注:本文基于TinkerPop 3.4.4

    相关文章

      网友评论

        本文标题:Apache TinkerPop快速体验

        本文链接:https://www.haomeiwen.com/subject/reobwctx.html