Browse Source

1.使用python连接nebula图形数据库
2.测试nebula图形数据库连接成功

Air 4 weeks ago
parent
commit
76b88dc895
2 changed files with 39 additions and 0 deletions
  1. 24 0
      src/kg_construction/nebula_kg.py
  2. 15 0
      test/nebula_test.py

+ 24 - 0
src/kg_construction/nebula_kg.py

@@ -0,0 +1,24 @@
+from nebula3.gclient.net import ConnectionPool
+from nebula3.Config import Config
+
+
+class NebulaConn:
+
+    def initConnect(self, host, port, username, password, graphSpace):
+        # 定义配置
+        config = Config()
+        config.max_connection_pool_size = 10
+        # 初始化连接池
+        connection_pool = ConnectionPool()
+        # 如果给定的服务器正常,则返回true,否则返回false。
+        status = connection_pool.init([(host, port)], config)
+        # 用户名与密码
+        session = connection_pool.get_session(username, password)
+        session.execute('USE %s' %(graphSpace))
+        return session, connection_pool
+
+    def releaseConnectionPool(self, connection_pool):
+        connection_pool.close()
+
+    def releaseSession(self, session):
+        session.release()

+ 15 - 0
test/nebula_test.py

@@ -0,0 +1,15 @@
+from src.kg_construction.nebula_kg import NebulaConn
+
+if __name__ == "__main__":
+    host = '116.204.126.213'
+    port = '9669'
+    username = 'root'
+    password = 'nebula'
+    graphSpace = 'knowledgeGraph'
+
+    nebulaDBConn = NebulaConn()
+    session, connection_pool = nebulaDBConn.initConnect(host=host, port=port, username=username, password=password, graphSpace=graphSpace)
+    print(session.execute('SHOW TAGS'))
+
+    nebulaDBConn.releaseSession(session=session)
+    nebulaDBConn.releaseConnectionPool(connection_pool=connection_pool)