美文网首页
【neo4j】生成关系图 2019-10-04

【neo4j】生成关系图 2019-10-04

作者: 耕耘菜菜 | 来源:发表于2019-10-04 14:57 被阅读0次
    1. 准备要显示的数据如下

    data.csv 文件内容

    from,to,amount
    A,A,12
    A,F,3
    A,L,3
    A,E,4
    B,F,70
    B,B,14
    B,K,2
    B,im,1
    B,C,5
    D,I,30
    D,D,88
    D,C,20
    Y,G,8
    Y,F,132
    Y,H,16460
    Y,J,8229
    Y,emds,2
    Y,U,2
    Y,V,2
    Y,im,20
    Y,M,2
    W,N,14
    W,O,77580
    W,L,95
    W,C,7
    W,P,2
    W,Q,1
    others,K,219
    others,im,28
    others,R,222
    others,C,59
    X,K,10
    X,im,35
    X,X,83
    X,C,45
    S,S,8231
    S,T,2851
    S,L,4323
    S,R,8229
    
    1. 使用 python 读取上面的数据,生成 Cypher 语法(以后再升级为直接写数据库)

      from collections import defaultdict
      import pandas as pd

    file = r"data.csv"
    df = pd.read_csv(file)
    node_dict = defaultdict(int)
    service_list = list()
    
    row_num = len(df.index)
    for i in range(row_num):
        # get the data of each row
        _from, _to, amount = df.loc[i]
    
        # add the node name to service list if it is new
        # generate the node creation command
        if _from not in service_list:
            command = f"CREATE ({_from}:Node{{name: {repr(_from)}}})"
            print(command)
            service_list.append(_from)
        if _to not in service_list:
            command = f"CREATE ({_to}:Node{{name: {repr(_to)}}})"
            print(command)
            service_list.append(_to)
    
        # generate relationship creation command
        command = f"CREATE ({_from})-[:Visit{{time:{amount}}}]->({_to})"
        print(command)
    

    生成的 Cypher 语句如下

    CREATE (A:Node{name: 'A'})
    CREATE (A)-[:Visit{time:12}]->(A)
    CREATE (F:Node{name: 'F'})
    CREATE (A)-[:Visit{time:3}]->(F)
    CREATE (L:Node{name: 'L'})
    CREATE (A)-[:Visit{time:3}]->(L)
    CREATE (E:Node{name: 'E'})
    CREATE (A)-[:Visit{time:4}]->(E)
    CREATE (B:Node{name: 'B'})
    CREATE (B)-[:Visit{time:70}]->(F)
    CREATE (B)-[:Visit{time:14}]->(B)
    CREATE (K:Node{name: 'K'})
    CREATE (B)-[:Visit{time:2}]->(K)
    CREATE (im:Node{name: 'im'})
    CREATE (B)-[:Visit{time:1}]->(im)
    CREATE (C:Node{name: 'C'})
    CREATE (B)-[:Visit{time:5}]->(C)
    CREATE (D:Node{name: 'D'})
    CREATE (I:Node{name: 'I'})
    CREATE (D)-[:Visit{time:30}]->(I)
    CREATE (D)-[:Visit{time:88}]->(D)
    CREATE (D)-[:Visit{time:20}]->(C)
    CREATE (Y:Node{name: 'Y'})
    CREATE (G:Node{name: 'G'})
    CREATE (Y)-[:Visit{time:8}]->(G)
    CREATE (Y)-[:Visit{time:132}]->(F)
    CREATE (H:Node{name: 'H'})
    CREATE (Y)-[:Visit{time:16460}]->(H)
    CREATE (J:Node{name: 'J'})
    CREATE (Y)-[:Visit{time:8229}]->(J)
    CREATE (emds:Node{name: 'emds'})
    CREATE (Y)-[:Visit{time:2}]->(emds)
    CREATE (U:Node{name: 'U'})
    CREATE (Y)-[:Visit{time:2}]->(U)
    CREATE (V:Node{name: 'V'})
    CREATE (Y)-[:Visit{time:2}]->(V)
    CREATE (Y)-[:Visit{time:20}]->(im)
    CREATE (M:Node{name: 'M'})
    CREATE (Y)-[:Visit{time:2}]->(M)
    CREATE (W:Node{name: 'W'})
    CREATE (N:Node{name: 'N'})
    CREATE (W)-[:Visit{time:14}]->(N)
    CREATE (O:Node{name: 'O'})
    CREATE (W)-[:Visit{time:77580}]->(O)
    CREATE (W)-[:Visit{time:95}]->(L)
    CREATE (W)-[:Visit{time:7}]->(C)
    CREATE (P:Node{name: 'P'})
    CREATE (W)-[:Visit{time:2}]->(P)
    CREATE (Q:Node{name: 'Q'})
    CREATE (W)-[:Visit{time:1}]->(Q)
    CREATE (others:Node{name: 'others'})
    CREATE (others)-[:Visit{time:219}]->(K)
    CREATE (others)-[:Visit{time:28}]->(im)
    CREATE (R:Node{name: 'R'})
    CREATE (others)-[:Visit{time:222}]->(R)
    CREATE (others)-[:Visit{time:59}]->(C)
    CREATE (X:Node{name: 'X'})
    CREATE (X)-[:Visit{time:10}]->(K)
    CREATE (X)-[:Visit{time:35}]->(im)
    CREATE (X)-[:Visit{time:83}]->(X)
    CREATE (X)-[:Visit{time:45}]->(C)
    CREATE (S:Node{name: 'S'})
    CREATE (S)-[:Visit{time:8231}]->(S)
    CREATE (T:Node{name: 'T'})
    CREATE (S)-[:Visit{time:2851}]->(T)
    CREATE (S)-[:Visit{time:4323}]->(L)
    CREATE (S)-[:Visit{time:8229}]->(R)
    
    1. 打开 Neo4j Desktop, 并启动 Graph 数据


      neo4j desktop - testGraph.png
    2. 点击打开 "Neo4j Browser"

    3. 运行命令先清理数据库

    // Delete everything
    MATCH (n)
    DETACH DELETE n;

    DETACH COMMAND.png
    1. 运行上面的所有 CREATE 命令,创建节点和关系
    CREATE COMMANDS.png
    1. 运行命令,显示所有节点和关系
      // Display all nodes
      MATCH (n)
      RETURN n
    MATCH ALL NODES.png

    相关文章

      网友评论

          本文标题:【neo4j】生成关系图 2019-10-04

          本文链接:https://www.haomeiwen.com/subject/jbqspctx.html