gremlin数据加载与图遍历语言简介(增删改查)

Gremlin 语言 tinkergraph(一)1.加载与存储tinkergraphGraph = TinkerGraph.open()①将graph存储为graphSON或者graphML格式graph.io(graphml()).writeGraph('my-graph.graphml')graph.io(graphson()).writeGraph("my-grap...
摘要由CSDN通过智能技术生成

Gremlin 语言 tinkergraph

(一)

1.加载与存储tinkergraph

Graph = TinkerGraph.open()

①将graph存储为graphSON或者graphML格式

graph.io(graphml()).writeGraph('my-graph.graphml')

graph.io(graphson()).writeGraph("my-graph.json")  //unwrapped 适合数据量大

 

fos = new FileOutputStream("my-graph.json")

GraphSONWriter.build().wrapAdjacencyList(true).create().writeGraph(fos,graph)   //wrapped

②加载存储在graphSON和graphML中的图

graph.io(IoCore.graphml()).readGraph('my-graph.graphml')

graph.io(IoCore.graphson()).readGraph('my-graph.json')

③将查询结果存储为json格式

首先一个mapper,可以指定版本

json_mapper = GraphSONMapper.

                build().

                version(GraphSONVersion.V1_0).

                create().

                createMapper()

查询

lax = g.V().has('code','LAX').next()

存储

json_mapper.writeValueAsString(lax)

④从groovy文件中加载

:load graph-master/sample-data/load-air-routes-graph.groovy

2.

在gremlin中,可以定义变量,可以定义函数,可以使用循环

variables

austin=g.V().has('code','AUS').next()

g.V(austin).out()

若不希望结果马上显示,则可以通过在结尾处添加 ;[]

austin=g.V().has('code','AUS').next();[]

Functions

打印两地之间距离

def dist(g,from,to) {

  d=g.V().has('code',from).outE().as('a').inV().has('code',to)

         .select('a').values('dist').next()

  return d }

dist(g,'AUS','MEX')

Loop

for (a in g.V().hasLabel('airport').limit(10).toList()) {println(a.values('code').next()+""+a.values('icao').next())}

3.增adding vertex,edges and properties

①增加addV(),addE()

xyz = graph.addVertex(label,'airport',

                      'code','XYZ',

                      'icao','KXYZ',

                      'desc','This is not a real airport')

dfw = g.V().has('code','DFW').next()

xyz.addEdge('route',dfw)

在增加节点时并不推荐使用graph,推荐使用g

xyz = g.addV('airport').property('code','XYZ').

                        property('icao','KXYZ').

                        property('desc','This is not a real airport').next()

g.V().has('code','DFW').addE('route').to(xyz)

②新增属性,property()

新增一个名为places的属性,其中放置AUS中所有地点

g.V().has('code','AUS').property('places',out().values('code').fold())

多值属性 list

g.addV().property('code','AUS

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值