估计内存使用并调整实例大小
设置
有关如何开始使用 Python 的更多信息,请参阅 使用 Python 连接 教程。
pip install graphdatascience
# Import the client
from graphdatascience import GraphDataScience
# Replace with the actual URI, username, and password
AURA_CONNECTION_URI = "neo4j+s://xxxxxxxx.databases.neo4j.io"
AURA_USERNAME = "neo4j"
AURA_PASSWORD = ""
# Configure the client with AuraDS-recommended settings
gds = GraphDataScience(
AURA_CONNECTION_URI,
auth=(AURA_USERNAME, AURA_PASSWORD),
aura_ds=True
)
有关如何开始使用 Cypher Shell 的更多信息,请参阅 Neo4j Cypher Shell 教程。
从安装 Cypher shell 的目录运行以下命令。 |
export AURA_CONNECTION_URI="neo4j+s://xxxxxxxx.databases.neo4j.io"
export AURA_USERNAME="neo4j"
export AURA_PASSWORD=""
./cypher-shell -a $AURA_CONNECTION_URI -u $AURA_USERNAME -p $AURA_PASSWORD
有关如何开始使用 Python 的更多信息,请参阅 使用 Python 连接 教程。
pip install neo4j
# Import the driver
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 = ""
# Instantiate the driver
driver = GraphDatabase.driver(
AURA_CONNECTION_URI,
auth=(AURA_USERNAME, AURA_PASSWORD)
)
# 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()
创建示例图
创建内存中图的一种简单方法是通过 GDS 图生成 算法。通过指定节点数、每个节点发出的关系的平均数和关系分布函数,算法创建了一个具有以下形状的图
(:50000000_Nodes)-[:REL]→(:50000000_Nodes)
# Run the graph generation algorithm and retrieve the corresponding
# graph object and call result metadata
g, result = gds.beta.graph.generate(
"example-graph",
50000000,
3,
relationshipDistribution="POWER_LAW"
)
# Print prettified graph stats
print(result)
CALL gds.beta.graph.generate(
'example-graph',
50000000,
3,
{relationshipDistribution: 'POWER_LAW'}
)
YIELD name,
nodes,
relationships,
generateMillis,
relationshipSeed,
averageDegree,
relationshipDistribution,
relationshipProperty
RETURN *
# Cypher query
create_example_graph_query = """
CALL gds.beta.graph.generate(
'example-graph',
50000000,
3,
{relationshipDistribution: 'POWER_LAW'}
)
YIELD name,
nodes,
relationships,
generateMillis,
relationshipSeed,
averageDegree,
relationshipDistribution,
relationshipProperty
RETURN *
"""
# Create the driver session
with driver.session() as session:
# Run query
result = session.run(create_example_graph_query).data()
# Prettify the result
print(json.dumps(result, indent=2, sort_keys=True, default=default))
该图相当大,因此生成过程将需要几分钟才能完成。 |
运行estimate
模式
在内存中图上估算算法的内存需求对于确定当前 AuraDS 实例是否拥有足够的资源来完成算法运行非常有用。
图数据科学内置了防护栏:如果估计算法使用的 RAM 超过可用 RAM,则会引发异常。在这种情况下,可以在再次运行算法之前调整 AuraDS 实例的大小。
在以下示例中,我们获得了标签传播算法在生成的图上运行的内存估算。估计内存介于 381 MiB 和 4477 MiB 之间,这高于 8 GB 实例可用的内存 (4004 MiB)。
result = gds.labelPropagation.mutate.estimate(
g,
mutateProperty="communityID"
)
print(result)
CALL gds.labelPropagation.mutate.estimate(
'example-graph',
{mutateProperty: 'communityID'}
)
YIELD nodeCount,
relationshipCount,
bytesMin,
bytesMax,
requiredMemory
RETURN *
# Cypher query
page_rank_mutate_estimate_example_graph_query = """
CALL gds.labelPropagation.mutate.estimate(
'example-graph',
{mutateProperty: 'communityID'}
)
YIELD nodeCount,
relationshipCount,
bytesMin,
bytesMax,
requiredMemory
RETURN *
"""
# Create the driver session
with driver.session() as session:
# Run query
results = session.run(page_rank_mutate_estimate_example_graph_query).data()
# Prettify the result
print(json.dumps(results, indent=2, sort_keys=True))
mutate
过程在 8 GB 实例上触及了防护栏,引发了一个异常,建议调整 AuraDS 实例的大小。
result = gds.labelPropagation.mutate(
g,
mutateProperty="communityID"
)
print(result)
CALL gds.labelPropagation.mutate(
'example-graph',
{mutateProperty: 'communityID'}
)
YIELD preProcessingMillis,
computeMillis,
mutateMillis,
postProcessingMillis,
nodePropertiesWritten,
communityCount,
ranIterations,
didConverge,
communityDistribution,
configuration
RETURN *
# Cypher query
page_rank_mutate_example_graph_query = """
CALL gds.labelPropagation.mutate(
'example-graph',
{mutateProperty: 'communityID'}
)
YIELD preProcessingMillis,
computeMillis,
mutateMillis,
postProcessingMillis,
nodePropertiesWritten,
communityCount,
ranIterations,
didConverge,
communityDistribution,
configuration
RETURN *
"""
# Create the driver session
with driver.session() as session:
# Run query
results = session.run(page_rank_mutate_example_graph_query).data()
# Prettify the result
print(json.dumps(results, indent=2, sort_keys=True))
调整 AuraDS 实例的大小
您需要将实例调整为下一个可用大小 (16 GB) 才能继续。可以从 Neo4j Aura 控制台 主页调整 AuraDS 实例的大小。有关更多信息,请查看 实例操作 部分。
调整 AuraDS 实例的大小会导致短暂的停机时间。 |
调整大小后,等待几秒钟,直到投影图重新加载,然后再次运行 mutate
步骤。这次没有抛出异常,并且该步骤成功完成。
清理
现在可以删除内存中图。
result = gds.graph.drop(g)
print(result)
CALL gds.graph.drop('example-graph')
delete_example_in_memory_graph_query = """
CALL gds.graph.drop('example-graph')
"""
with driver.session() as session:
# Run query
results = session.run(delete_example_in_memory_graph_query).data()
# Prettify the results
print(json.dumps(results, indent=2, sort_keys=True, default=default))
参考
Cypher
-
详细了解 Cypher 语法
-
您可以使用 Cypher 速查表 作为所有可用 Cypher 功能的参考