Python SDK 指南¶
本文介绍如何使用 Python SDK 连接悦数图数据库开发版并执行简单查询。
需要从安装依赖到验证应用接口的完整流程,请参见使用 Python SDK 构建查询服务。
前提条件¶
- 已安装 Python 3.10 或更高版本。
- 对于本地部署用户,悦数图数据库开发版服务组正在运行中。更多信息,请参见快速入门。
操作步骤¶
-
通过以下任一方式安装 SDK:
-
从 PyPI 安装:
pip install nebula5_python==5.3.0
-
从源码安装:
git clone -b dev https://github.com/vesoft-inc/nebula-python.git cd nebula-python pip install -e .
-
-
使用 SDK 连接悦数图数据库开发版并执行查询:
from nebulagraph_python.client import NebulaClient # Create client client = NebulaClient( addresses="<host>:<port>", user_name="<username>", password="<password>", ) query = """ RETURN 1 AS a, 2 AS b """ # Execute query try: result = client.execute(query) finally: client.close()参数
<host>:<port>、<username>和<password>须替换为实际的连接信息。 -
若要检查查询结果,可参考以下方法:
-
以表格形式打印结果
result.print()Return:
╔═══╤═══╗ ║ │ ║ ║ a │ b ║ ║ │ ║ ╟───┼───╢ ║ │ ║ ║ 1 │ 2 ║ ║ │ ║ ╚═══╧═══╝ Summary ├── Rows: 1 └── Latency: 1450μs
-
从查询集中检索特定值
# Get one row row = result.one() # Get one value cell = row["a"].cast_primitive() # Print its value print(cell, type(cell))Return:
1 <class 'int'>
-
从结果集中检索原始值
print(result.as_primitive_by_column()) print(list(result.as_primitive_by_row()))Return:
{'a': [1], 'b': [2]} [{'a': 1, 'b': 2}]
-
从查询集中检索 Pandas DataFrame
result = client.execute(query) df = result.as_pandas_df()Return:
a b 0 1 2
-