本教程展示了如何使用
图数据科学 (GDS) 客户端
或
Python 驱动程序
与 AuraDS 交互。在以下部分中,您可以通过单击相应的选项卡在客户端和驱动程序代码之间切换。
必须有一个正在运行的 AuraDS 实例以及访问凭据(在
创建 AuraDS 实例
部分中生成)及其连接 URI(在实例详细信息页面中找到,以
neo4j+s://
开头)。
# 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
# 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)
# 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)
# 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))
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))
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))