使用 Python 连接

Colab Google Colab 中使用笔记本进行学习

本教程展示了如何使用 图数据科学 (GDS) 客户端Python 驱动程序 与 AuraDS 交互。在以下部分中,您可以通过单击相应的选项卡在客户端和驱动程序代码之间切换。

必须有一个正在运行的 AuraDS 实例以及访问凭据(在 创建 AuraDS 实例 部分中生成)及其连接 URI(在实例详细信息页面中找到,以 neo4j+s:// 开头)。

安装

可以使用 pip 安装 GDS 客户端和 Python 驱动程序。

pip install graphdatascience

可以在 PyPI 上找到客户端的最新稳定版本。

pip install neo4j

可以在 PyPI 上找到驱动程序的最新稳定版本。

如果 pip 不可用,您可以尝试用 python -m pippython3 -m pip 替换它。

导入和设置

GDS 客户端和 Python 驱动程序都需要连接 URI 和凭据,如引言中所示。

客户端作为 GraphDataScience 类导入

# Client import
from graphdatascience import GraphDataScience

aura_ds=True 构造函数参数应用于自动应用 Python 驱动程序的推荐非默认配置设置。

# Replace with the actual URI, username, and password
AURA_CONNECTION_URI = "neo4j+s://xxxxxxxx.databases.neo4j.io"
AURA_USERNAME = "neo4j"
AURA_PASSWORD = "..."

# Client instantiation
gds = GraphDataScience(
    AURA_CONNECTION_URI,
    auth=(AURA_USERNAME, AURA_PASSWORD),
    aura_ds=True
)

驱动程序作为 GraphDatabase 类导入

# Driver import
from neo4j import GraphDatabase
# Replace with the actual URI, username and password
AURA_CONNECTION_URI = "neo4j+s://xxxxxxxx.databases.neo4j.io"
AURA_USERNAME = "neo4j"
AURA_PASSWORD = "..."

# Driver instantiation
driver = GraphDatabase.driver(
    AURA_CONNECTION_URI,
    auth=(AURA_USERNAME, AURA_PASSWORD)
)

运行查询

创建后,客户端(或驱动程序)可用于运行 Cypher 查询并调用 Cypher 过程。在本例中,gds.version 过程可用于检索实例上运行的 GDS 版本。

# Call a GDS method directly
print(gds.version())
# Cypher query
gds_version_query = """
    RETURN gds.version() AS version
"""

# Create a driver session
with driver.session() as session:
    # Use .data() to access the results array
    results = session.run(gds_version_query).data()
    print(results)

以下代码检索库中可用的所有过程,并显示其中五个过程的详细信息。

# Assign the result of the client call to a variable
results = gds.list()

# Print the result (a Pandas DataFrame)
print(results[:5])

由于结果是 Pandas DataFrame,因此您可以使用 to_stringto_json 等方法对其进行美化输出。

# Print the result (a Pandas DataFrame) as a console-friendly string
print(results[:5].to_string())
# Print the result (a Pandas DataFrame) as a prettified JSON string
print(results[:5].to_json(orient="table", indent=2))
# Import the json module for pretty visualization
import json

# Cypher query
list_all_gds_procedures_query = """
    CALL gds.list()
"""

# Create a driver session
with driver.session() as session:
    # Use .data() to access the results array
    results = session.run(list_all_gds_procedures_query).data()

    # Print the prettified result
    print(json.dumps(results[:5], indent=2))

在 JSON dumps 中序列化 Neo4j DateTime

在某些情况下,过程调用的结果可能包含 Neo4j DateTime 对象。为了将此类对象序列化为 JSON,必须提供一个默认处理程序。

# Import for the JSON helper function
from neo4j.time import DateTime

# Helper function for serializing Neo4j DateTime in JSON dumps
def default(o):
    if isinstance(o, (DateTime)):
        return o.isoformat()

# Run the graph generation algorithm
g, _ = gds.beta.graph.generate(
    "example-graph", 10, 3, relationshipDistribution="POWER_LAW"
)

# Drop the graph keeping the result of the operation, which contains
# some DateTime fields ("creationTime" and "modificationTime")
result = gds.graph.drop(g)

# Print the result as JSON, converting the DateTime fields with
# the handler defined above
print(result.to_json(indent=2, default_handler=default))
# Import to prettify results
import json

# Import for the JSON helper function
from neo4j.time import DateTime

# Helper function for serializing Neo4j DateTime in JSON dumps
def default(o):
    if isinstance(o, (DateTime)):
        return o.isoformat()

# Example query to run a graph generation algorithm
create_example_graph_query = """
    CALL gds.beta.graph.generate(
      'example-graph', 10, 3, {relationshipDistribution: 'POWER_LAW'}
    )
"""

# Example query to delete a graph
delete_example_graph_query = """
    CALL gds.graph.drop('example-graph')
"""

# Create the driver session
with driver.session() as session:
    # Run the graph generation algorithm
    session.run(create_example_graph_query).data()

    # Drop the generated graph keeping the result of the operation
    results = session.run(delete_example_graph_query).data()

    # Prettify the results using the handler defined above
    print(json.dumps(results, indent=2, sort_keys=True, default=default))

关闭连接

在不再需要时应始终关闭连接。

尽管 GDS 客户端在对象被删除时会自动关闭连接,但显式关闭连接是一个好习惯。

# Close the client connection
gds.close()
# Close the driver connection
driver.close()

参考

Cypher