gremlin-server 是tinkerpop3提供的远程API.
他的具体配置可见:
https://github.com/aws/graph-notebook

最终是要在jupyter中完成可视化效果,需要:

  1. 安装并配置gremlin-server
  2. 安装并配置jupyter
  3. 安装并配置graph-notebook

1. gremlin-server

https://github.com/aws/graph-notebook/tree/main/additional-databases/gremlin-server

安装最新版gremlinserver, 根据上述连接配置就可以了。这里主要是打开HTTP服务, 注意gremlin-server默认的图数据库是tinkergraph。
开启后你的gremlin-server就可以访问了, 默认是localhost的8182端口。

连接服务端的话我用的是python,需要安装gremlinpython库:
https://pypi.org/project/gremlinpython/

  1. from gremlin_python.process.anonymous_traversal import traversal
  2. from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection
  3. g = traversal().withRemote(DriverRemoteConnection('ws://localhost:8182/gremlin','g'))

这样就可以连接了(这里是ws连接)。

jupyter+graph-notebook

jupyter安装就按个anaconda就行了
主要是如何安装graph-notebook并配置连接gremlin。

查询

gremlin有两种方式,对于jvm语言可以直接在jvm上使用。 其他情况可以开一个gremlin-server通过网络交互使用。
使用python连接gremlin-server,有两种查询方式,一种是”原生”利用即python即gremlin-client。另一种是,python写client查询语句然后使用gremlin.client()将查询发送出到远程client:

  1. # basic-client.py
  2. #
  3. # Connect to a Gremlin Server using a client connection.
  4. #
  5. # Note that in this case the Gremlin queries are sent to the server as text strings.
  6. # To see an example of using Gremlin byte code instead see the glv-client.py example.
  7. #
  8. # This example code assumes that the GremlinPython library has been installed using:
  9. #
  10. # pip install gremlinpython
  11. #
  12. # Import some classes we will need to talk to our graph
  13. from gremlin_python.driver.client import Client
  14. # Path to our graph (this assumes a locally running Gremlin Server)
  15. # Note how the path is a Web Socket (ws) connection.
  16. client = Client('ws://localhost:8182/gremlin','g')
  17. query = """
  18. g.V().hasLabel('airport').
  19. sample(30).
  20. order().by('code').
  21. local(__.values('code','city').fold()).
  22. toList()
  23. """
  24. result = client.submit(query).all().result()
  25. client.close()

reference: https://github.com/krlawrence/graph/blob/master/sample-code/basic-client.py

image.png

如果出现了以上错误(Cannot run the event loop while another loop is running),有效解决:

  1. !pip install nest_asyncio
  2. import nest_asyncio
  3. nest_asyncio.apply()

reference: https://blog.csdn.net/weixin_35757704/article/details/108812809

使用 graph-notebook

使用jupyter哈市主要为了辅助做一些小笔记,那么在里边能嵌入图就更好啦:
https://github.com/aws/graph-notebook
现在我们要在jupyter中安装graph-notebook, 有效的解决方案就是按着github说明在终端一步一步做就好啦:

  1. # pin specific versions of Jupyter, Tornado, and RDFLib dependencies
  2. pip install notebook==5.7.13
  3. pip install tornado==4.5.3
  4. pip install rdflib==5.0.0
  5. # install the package
  6. pip install graph-notebook
  7. # install and enable the visualization widget
  8. jupyter nbextension install --py --sys-prefix graph_notebook.widgets
  9. jupyter nbextension enable --py --sys-prefix graph_notebook.widgets
  10. # copy static html resources
  11. python -m graph_notebook.static_resources.install
  12. python -m graph_notebook.nbextensions.install
  13. # copy premade starter notebooks
  14. python -m graph_notebook.notebooks.install --destination ~/notebook/destination/dir
  15. # start jupyter
  16. jupyter notebook ~/notebook/destination/dir

注意版本,一把梭安装之后,再开启jupyter我们就有graph_notebook_config的magic cell使用了,开启我们的gremlin-server, 连接:

  1. %%graph_notebook_config
  2. {
  3. "host": "localhost",
  4. "port": 8182,
  5. "ssl": false
  6. }

导张图进去:

  1. %%gremlin
  2. graph.io(graphml()).readGraph('/Your_path/air-routes.graphml') // TinkerPop 3

查一下:

  1. %%gremlin -p v,oute,inv,oute,inv,oute,inv
  2. g.V().has('airport', 'code', 'AUS').
  3. repeat(outE().inV().simplePath()).
  4. until(has('code', 'WLG')).
  5. limit(5).
  6. path().
  7. by('code').
  8. by('dist')

image.png
灰常的棒