日本熟妇hd丰满老熟妇,中文字幕一区二区三区在线不卡 ,亚洲成片在线观看,免费女同在线一区二区

本文介紹如何基于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