tinkerpop

felix.shao2025-05-25

tinkerpop

tinkerpop 官网open in new window

1 安装

资源包备注链接
gremlin-server服务端服务端下载open in new window
gremlin-console客户端客户端下载open in new window
gremlin-src源码源码下载open in new window
air-routes航线图示例air-routes 资料下载open in new window

2 文档说明

3 quick-start

unzip apache-tinkerpop-gremlin-console-3.7.3-bin.zip
cd apache-tinkerpop-gremlin-console-3.7.3
# JDK 要求 1.8,实际用 JDK11 是可以的,window 启动会报错,注意用 Linux
bin/gremlin.sh

 实际语法参考 getting-startedopen in new window

 注意,load 数据命令示例如下。

# 数据不大,直接下
curl -L -O http://snap.stanford.edu/data/wiki-Vote.txt.gz
gunzip wiki-Vote.txt.gz

# 进入 gremlin 窗口
bin/gremlin.sh

# 输入以下 gremlin 脚本
graph = TinkerGraph.open()
graph.createIndex('userId', Vertex.class) 

g = traversal().withEmbedded(graph)

getOrCreate = { id ->
  g.V().has('user','userId', id).
    fold().
    coalesce(unfold(),
             addV('user').property('userId', id)).next()  
}

new File('/mnt/hgfs/vmshare/gremlin/data/wiki-Vote.txt').eachLine {
  if (!it.startsWith("#")){
    (fromVertex, toVertex) = it.split('\t').collect(getOrCreate) 
    g.addE('votesFor').from(fromVertex).to(toVertex).iterate()
  }
}

# 测试
g.V().limit(1).outE('votesFor').path()

4 机场航线图用例

 参考 Practical Gremlin 项目教程open in new window

4.1 使用示例 1

 以下是使用步骤。

  1. 安装 Gremlin Console。
  2. 加载示例数据。使用 Gremlin Console 加载 sample-data 目录中的 air-routes.graphml 文件。你可以使用以下命令。
     注意 air-routes.graphml 文件可以看到顶点和边的定义。
graph = TinkerGraph.open()
g = graph.traversal()
graph.io(graphml()).readGraph('/mnt/hgfs/vmshare/gremlin/air_routes_data/graph-main/sample-data/air-routes.graphml')
  1. 运行代码,如下类似
g.V().hasLabel('airport').
   has('region',within('US-TX','US-LA','US-AZ','US-OK')).
   order().
   by('region',asc).
   valueMap().
   select('code','region')

4.2 使用示例 2

# 1 启动 bat
gremlin.bat 

# 2 准备工作
# 2.1 graph-master.zip 解压后放到和 gremlin 同盘根目录, 即 /gremlinData/graph-master
# 2.2 复制 /gremlinData/graph-master/sample-data/air-routes.graphml 到 /gremlinData/mydata/air-routes.graphml
# 2.3 修改 /gremlinData/graph-master/sample-data/load-air-routes-graph.groovy 中的文本内容: 即 /mydata/air-routes.graphml 改为 /gremlinData/mydata/air-routes.graphml
# 2.4 执行以下命令启动

# 3 加载文件
:load /gremlinData/graph-master/sample-data/load-air-routes-graph.groovy

# 4 测试,具体使用示例见 graph-master.zip 里面的源码
graph.toString()
==>tinkergraph[vertices:3619 edges:50148]
Last Updated 5/28/2025, 9:13:03 AM