文档

Python

更新时间:

本文介绍如何基于Python编程环境连接和操作图数据库GDB。

前提条件

  • 进行以下操作时,请确保图数据库GDB的实例与您的ECS虚拟机处于同一个VPC网络环境。

  • 在使用Python连接GDB实例之前,需要准备以下环境:

  • 如果您的Python为2.7版本,则需要额外安装以下module:

    pip install futures --user

使用Python连接到GDB实例

  1. 输入以下命令以安装gremlinpython程序包:

    pip install gremlinpython ‑‑user
  2. 创建test.py文件,编辑并输入以下内容,并替换其中的配置信息:

    • ${your-gdb-endpoint}改为您的图数据库GDB实例的域名。

    • ${username}改为您的图数据库GDB实例的用户名。

    • ${password}改为您的图数据库GDB实例的密码。

    from __future__ import print_function  # Python 2/3 compatibility
    from gremlin_python.driver import client
    client = client.Client('ws://${your-gdb-endpoint}:8182/gremlin', 'g', username="${username}", password="${password}")
    callback = client.submit("g.V().limit(1)").all().result()
    for result in callback:
        print(result)
    client.close()
    说明

    8182为图数据库GDB的内网端口,如果您使用外网地址进行连接请求改为3734。

  3. 执行以下命令运行示例。

    python test.py

    回显如下:

    [v[3a63cc90-d957-4324-9ffc-16a8e4c1c1f4]]

    以上回显实例是使用g.V().limit(1)遍历返回GDB中的一个点,要查询其他内容,需替换成对应的DSL。

  4. (可选)常见异常处理。

    • Cannot run the event loop while another loop is running

      在jupyter等环境使用gremlin_python可能会报该错误,嵌套event loop问题,可以添加下面程序包解决

      # 安装程序包
      !pip install nest_asyncio
      
      # 代码文件中导入并使用
      import nest_asyncio
      nest_asyncio.apply()
    • Windows下gremlin driver报NotImplementError

      Python 3.8以后asyncio库修改了Windows系统下默认的event loop实现方式,gremlin driver用来处理网络通信的tornado依赖于asyncio,因此会抛出NotImplementError异常。解决方法是添加如下代码修改event loop策略

      import sys
      import asyncio
      
      if sys.platform == 'win32':
          asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

其他DSL范例如下

  1. 删除指定label的点和边。

    g.E().hasLabel('gdb_sample_knows').drop()
    g.E().hasLabel('gdb_sample_created').drop()
    g.V().hasLabel('gdb_sample_person').drop()
    g.V().hasLabel('gdb_sample_software').drop()
  2. 添加顶点为其设置id、property。

    g.addV('gdb_sample_person').property(id, 'gdb_sample_marko').property('age', 28).property('name', 'marko')
    g.addV('gdb_sample_person').property(id, 'gdb_sample_vadas').property('age', 27).property('name', 'vadas')
    g.addV('gdb_sample_person').property(id, 'gdb_sample_josh').property('age', 32).property('name', 'josh')
    g.addV('gdb_sample_person').property(id, 'gdb_sample_peter').property('age', 35).property('name', 'peter')
    g.addV('gdb_sample_software').property(id, 'gdb_sample_lop').property('lang', 'java').property('name', 'lop')
    g.addV('gdb_sample_software').property(id, 'gdb_sample_ripple').property('lang', 'java').property('name', 'ripple')
  3. 修改或新增age属性。

    g.V('gdb_sample_marko').property('age', 29)
  4. 建立关系,设置属性 weight。

    g.addE('gdb_sample_knows').from(V('gdb_sample_marko')).to(V('gdb_sample_vadas')).property('weight', 0.5f)
    g.addE('gdb_sample_knows').from(V('gdb_sample_marko')).to(V('gdb_sample_josh')).property('weight', 1.0f)
    g.addE('gdb_sample_created').from(V('gdb_sample_marko')).to(V('gdb_sample_lop')).property('weight', 0.4f)
    g.addE('gdb_sample_created').from(V('gdb_sample_josh')).to(V('gdb_sample_lop')).property('weight', 0.4f)
    g.addE('gdb_sample_created').from(V('gdb_sample_josh')).to(V('gdb_sample_ripple')).property('weight', 1.0f)
    g.addE('gdb_sample_created').from(V('gdb_sample_peter')).to(V('gdb_sample_lop')).property('weight', 0.2f)
  5. 查询所有点或指定label的点数量。

    g.V().count()
    g.V().hasLabel('gdb_sample_person').count()
  6. 查询指定条件的顶点(>29岁的人,按name降序排列所有人)。

    g.V().hasLabel('gdb_sample_person').has('age', gt(29))
    g.V().hasLabel('gdb_sample_person').order().by('name', decr)
  7. 关联查询(获取marko认识的人,marko认识的人created的software)。

    g.V('gdb_sample_marko').outE('gdb_sample_knows').inV().hasLabel('gdb_sample_person')
    g.V('gdb_sample_marko').outE('gdb_sample_knows').inV().hasLabel('gdb_sample_person').outE('gdb_sample_created').inV().hasLabel('gdb_sample_software')
  8. 删除关系、顶点。

    g.V('gdb_sample_marko').outE('gdb_sample_knows').where(inV().has(id, 'gdb_sample_josh')).drop()
    g.V('gdb_sample_marko').drop()

相关资源

有关Gremlin Python接口的更多信息,请参阅Apache TinkerPop3文档中的Gremlin-Python

  • 本页导读 (1)
文档反馈