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