您好,欢迎来到爱玩科技网。
搜索
您的当前位置:首页从零开始Gremlin学习

从零开始Gremlin学习

来源:爱玩科技网

从零开始Gremlin学习

创建schema和图

以下面这个ThinkerPop关系图为例

// PropertyKey
graph.schema().propertyKey("name").asText().ifNotExist().create()
graph.schema().propertyKey("age").asInt().ifNotExist().create()
graph.schema().propertyKey("addr").asText().ifNotExist().create()
graph.schema().propertyKey("lang").asText().ifNotExist().create()
graph.schema().propertyKey("tag").asText().ifNotExist().create()
graph.schema().propertyKey("weight").asFloat().ifNotExist().create()

// VertexLabel
graph.schema().vertexLabel("person").properties("name", "age", "addr", "weight").useCustomizeStringId().ifNotExist().create()
graph.schema().vertexLabel("software").properties("name", "lang", "tag", "weight").primaryKeys("name").ifNotExist().create()
graph.schema().vertexLabel("language").properties("name", "lang", "weight").primaryKeys("name").ifNotExist().create()

// EdgeLabel
graph.schema().edgeLabel("knows").sourceLabel("person").targetLabel("person").properties("weight").ifNotExist().create()
graph.schema().edgeLabel("created").sourceLabel("person").targetLabel("software").properties("weight").ifNotExist().create()
graph.schema().edgeLabel("contains").sourceLabel("software").targetLabel("software").properties("weight").ifNotExist().create()
graph.schema().edgeLabel("define").sourceLabel("software").targetLabel("language").properties("weight").ifNotExist().create()
graph.schema().edgeLabel("implements").sourceLabel("software").targetLabel("software").properties("weight").ifNotExist().create()
graph.schema().edgeLabel("supports").sourceLabel("software").targetLabel("language").properties("weight").ifNotExist().create()

// TinkerPop
okram = graph.addVertex(T.label, "person", T.id, "okram", "name", "Marko A. Rodriguez", "age", 29, "addr", "Santa Fe, New Mexico", "weight", 1)
spmallette = graph.addVertex(T.label, "person", T.id, "spmallette", "name", "Stephen Mallette", "age", 0, "addr", "", "weight", 1)

tinkerpop = graph.addVertex(T.label, "software", "name", "TinkerPop", "lang", "java", "tag", "Graph computing framework", "weight", 1)
tinkergraph = graph.addVertex(T.label, "software", "name", "TinkerGraph", "lang", "java", "tag", "In-memory property graph", "weight", 1)
gremlin = graph.addVertex(T.label, "language", "name", "Gremlin", "lang", "groovy/python/javascript", "weight", 1)

okram.addEdge("created", tinkerpop, "weight", 1)
spmallette.addEdge("created", tinkerpop, "weight", 1)

okram.addEdge("knows", spmallette, "weight", 1)

tinkerpop.addEdge("define", gremlin, "weight", 1)
tinkerpop.addEdge("contains", tinkergraph, "weight", 1)
tinkergraph.addEdge("supports", gremlin, "weight", 1)

// Titan
dalaro = graph.addVertex(T.label, "person", T.id, "dalaro", "name", "Dan LaRocque ", "age", 0, "addr", "", "weight", 1)
mbroecheler = graph.addVertex(T.label, "person", T.id, "mbroecheler", "name", "Matthias Broecheler", "age", 29, "addr", "San Francisco", "weight", 1)

titan = graph.addVertex(T.label, "software", "name", "Titan", "lang", "java", "tag", "Graph Database", "weight", 1)

dalaro.addEdge("created", titan, "weight", 1)
mbroecheler.addEdge("created", titan, "weight", 1)
okram.addEdge("created", titan, "weight", 1)

dalaro.addEdge("knows", mbroecheler, "weight", 1)

titan.addEdge("implements", tinkerpop, "weight", 1)
titan.addEdge("supports", gremlin, "weight", 1)

// HugeGraph
javeme = graph.addVertex(T.label, "person", T.id, "javeme", "name", "Jermy Li", "age", 29, "addr", "Beijing", "weight", 1)
zhoney = graph.addVertex(T.label, "person", T.id, "zhoney", "name", "Zhoney Zhang", "age", 29, "addr", "Beijing", "weight", 1)
linary = graph.addVertex(T.label, "person", T.id, "linary", "name", "Linary Li", "age", 28, "addr", "Wuhan. Hubei", "weight", 1)

hugegraph = graph.addVertex(T.label, "software", "name", "HugeGraph", "lang", "java", "tag", "Graph Database", "weight", 1)

javeme.addEdge("created", hugegraph, "weight", 1)
zhoney.addEdge("created", hugegraph, "weight", 1)
linary.addEdge("created", hugegraph, "weight", 1)

javeme.addEdge("knows", zhoney, "weight", 1)
javeme.addEdge("knows", linary, "weight", 1)

hugegraph.addEdge("implements", tinkerpop, "weight", 1)
hugegraph.addEdge("supports", gremlin, "weight", 1)

图的基本概念与操作

g.V()   # 查询图的所有节点
g.E()   # 查询所有边的信息
g.V().id()  # 查询顶点的ID
g.E().id()  # 查询边的ID
g.V().label()  # 查询所有顶点的label
g.E().label()  # 查询所有边的label
g.V().properties()  # 查询所有顶点的属性
g.E().properties()  # 查询所有边的属性
g.V().properties().value()  # 查询所有顶点的属性值
g.V().valueMap()  # 查询所有顶点的属性
g.V().values()  # 与g.V().properties().value()相同
g.E().values()  # 与g.E().properties().value()相同

p.s. 节点和边的ID其实就是key值
**g.V().properties().value()和g.V().valueMap()**的区别:

  • g.V().properties().value()是将结果扁平化到同一个列表中
  • g.V().valueMap()将一个顶点或一条边的属性作为一组,每一组由若干属性的键值对组成

遍历操作

以顶点为基准

  • out(label): 根据指定的EdgeLabel来访问顶点的OUT方向邻接点(可以是零个EdgeLabel,代表所有类型边;也可以一个或多个EdgeLabel,代表任意给定EdgeLabel的边,下同)
g.V().out()   # 查询所有节点的后继节点
g.V('2:Titan').out()  # 查询ID为'2:Titan'节点的后继节点
  • in(label): 根据指定的EdgeLabel来访问顶点的IN方向邻接点
g.V().out()   # 查询所有节点的前继节点
g.V('2:Titan').out()  # 查询ID为'2:Titan'节点的前继节点
  • both(label): 根据指定的EdgeLabel来访问顶点的双向邻接点
g.V().both()   # 查询所有节点的邻接节点
g.V('2:Titan').both()  # 查询ID为'2:Titan'节点的邻接节点
  • outE(label): 根据指定的EdgeLabel来访问顶点的OUT方向邻接边
g.V().outE()  # 查询所有节点的出度边
g.V('2:Titan').outE()  # 查询ID为'2:Titan'的节点的出度边
g.V('2:Titan').outE('implements')  # # 查询ID为'2:Titan'的节点的label为'implements'的出度边
  • inE(label): 根据指定的EdgeLabel来访问顶点的IN方向邻接边
g.V().inE()  # 查询所有节点的入度边
g.V('2:Titan').inE()  # 查询ID为'2:Titan'的节点的入度边
g.V('2:Titan').inE('created')  # # 查询ID为'2:Titan'的节点的label为'created'的出度边
  • bothE(label): 根据指定的EdgeLabel来访问顶点的双向邻接边
g.V().bothE()  # 查询所有节点的相连的边
g.V('2:Titan').bothE()  # 查询ID为'2:Titan'的节点的相连的边
g.V('2:Titan').bothE('created', 'implements')  # # 查询ID为'2:Titan'的节点的label为'created'或'implements'的相连的边

以边为基准

  • outV(): 访问边的出顶点(注意:这里是以边为基准,上述Step均以顶点为基准),出顶点是指边的起始顶点
g.V('3:Gremlin').inE().outV()  # 查询id为'3:Gremlin'的入度边的出顶点
  • inV(): 访问边的入顶点,入顶点是指边的目标顶点,也就是箭头指向的顶点
  • bothV(): 访问边的双向顶点
  • otherV(): 访问边的伙伴顶点,即相对于基准顶点而言的另一端的顶点
g.V('3:Gremlin').inE().otherV()  # 查询id为'3:Gremlin'的入度边的伙伴顶点

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- aiwanbo.com 版权所有 赣ICP备2024042808号-3

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务