从零开始Gremlin学习

从零开始Gremlin学习

创建schema和图

以下面这个ThinkerPop关系图为例
Thinkerpop关系图
在建图之前,首先需要创建schema,创建的schema有属性、节点、和边缘,正常情况下是按照属性->节点->边缘,因为在定义及节点label的时候需要有属性,在定义边缘label的时候需要原节点,目标节点和属性的。

// 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()将一个顶点或一条边的属性作为一组,每一组由若干属性的键值对组成
    valueMap

properties.value

遍历操作

以顶点为基准

  • 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'的入度边的伙伴顶点
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值