可视化 Neo4j 图谱

下面是一个与 Neo4j 数据库的基本连接。我们使用 Result.graph 结果转换器将结果映射到图对象,并将结果存储在 result 中。

[ ]:
%pip install neo4j
%pip install neo4j-viz

让我们首先设置与数据库的连接。

[ ]:
import os

URI = os.environ.get("NEO4J_URI", "bolt://localhost:7687")

auth = None
if os.environ.get("NEO4J_USER") and os.environ.get("NEO4J_PASSWORD"):
    auth = (os.environ.get("NEO4J_USER"), os.environ.get("NEO4J_PASSWORD"))

为了说明该库,我们将创建一个代表网页的小型示例图谱。

[ ]:
from neo4j import GraphDatabase

with GraphDatabase.driver(URI, auth=auth) as driver:
    driver.verify_connectivity()
    driver.execute_query(
        """
        CREATE
         (dan:Person {name: 'Dan'}),
         (annie:Person {name: 'Annie'}),
         (matt:Person {name: 'Matt'}),
         (jeff:Person {name: 'Jeff'}),
         (brie:Person {name: 'Brie'}),
         (elsa:Person {name: 'Elsa'}),

         (cookies:Product {name: 'Cookies'}),
         (tomatoes:Product {name: 'Tomatoes'}),
         (cucumber:Product {name: 'Cucumber'}),
         (celery:Product {name: 'Celery'}),
         (kale:Product {name: 'Kale'}),
         (milk:Product {name: 'Milk'}),
         (chocolate:Product {name: 'Chocolate'}),

         (dan)-[:BUYS {amount: 1.2}]->(cookies),
         (dan)-[:BUYS {amount: 3.2}]->(milk),
         (dan)-[:BUYS {amount: 2.2}]->(chocolate),

         (annie)-[:BUYS {amount: 1.2}]->(cucumber),
         (annie)-[:BUYS {amount: 3.2}]->(milk),
         (annie)-[:BUYS {amount: 3.2}]->(tomatoes),

         (matt)-[:BUYS {amount: 3}]->(tomatoes),
         (matt)-[:BUYS {amount: 2}]->(kale),
         (matt)-[:BUYS {amount: 1}]->(cucumber),

         (jeff)-[:BUYS {amount: 3}]->(cookies),
         (jeff)-[:BUYS {amount: 2}]->(milk),

         (brie)-[:BUYS {amount: 1}]->(tomatoes),
         (brie)-[:BUYS {amount: 2}]->(milk),
         (brie)-[:BUYS {amount: 2}]->(kale),
         (brie)-[:BUYS {amount: 3}]->(cucumber),
         (brie)-[:BUYS {amount: 0.3}]->(celery),

         (elsa)-[:BUYS {amount: 3}]->(chocolate),
         (elsa)-[:BUYS {amount: 3}]->(milk)
    """
    )

现在我们可以从数据库中获取数据,以便稍后将其包含在我们的可视化中。

[ ]:
from neo4j import Result, RoutingControl

with GraphDatabase.driver(URI, auth=auth) as driver:
    driver.verify_connectivity()

    result = driver.execute_query(
        "MATCH (n)-[r]->(m) RETURN n,r,m",
        database_="neo4j",
        routing_=RoutingControl.READ,
        result_transformer_=Result.graph,
    )

    result

print(
    f"Result graph has: {len(result.nodes)} nodes, {len(result.relationships)} relationships"
)

下面我们将图对象中的节点和关系映射为 NVL 所需的正确格式。节点和关系类型的文档可在 NVL 文档中找到:https://neo4j.ac.cn/docs/nvl/current/base-library/#_nodes

现在,我们可以使用 NVL 按如下方式渲染结果

[5]:
from neo4j_viz.neo4j import from_neo4j

VG = from_neo4j(result)

VG.render()
[5]:
[ ]:
VG.color_nodes(field="caption")
[7]:
VG.render()
[7]:

最后,我们通过删除我们的示例图来清理数据库。

[ ]:
with GraphDatabase.driver(URI, auth=auth) as driver:
    result = driver.execute_query(
        "MATCH (n:Person|Product) DETACH DELETE n RETURN count(n) "
    )
    print(result.summary.counters)

注意: 由于在此示例中,我们的 Neo4j DB 尚未填充数据,因此使用无服务器 from_gql_create 导入方法创建我们的 VisualizationGraph 实际上会更方便。

© . All rights reserved.