• 好用的图数据库neo4j


    基于neo4j的数据可视化

    Neo4j是一个高性能的,NOSQL图形数据库,它将结构化数据存储在网络上而不是表中。它是一个嵌入式的、基于磁盘的、具备完全的事务特性的Java持久化引擎,但是它将结构化数据存储在网络(从数学角度叫做)上而不是表中。Neo4j也可以被看作是一个高性能的图引擎,该引擎具有成熟数据库的所有特性。程序员工作在一个面向对象的、灵活的网络结构下而不是严格、静态的表中——但是他们可以享受到具备完全的事务特性、企业级的数据库的所有好处。

        (1)安装好neo4j数据库后,进入bin目录,控制台输入,启动neo4j数据库,浏览器输入,访问数据库。

        (2)使用python导入数据。

    相关代码如下:

    1.连接数据库

    # coding:utf-8
    from py2neo import Graph, Node, Relationship, NodeMatcher
    import numpy as np
    import pandas as pd
    
    person_list = pd.read_csv('testdata1.txt', sep=' ')
    X = person_list[["id", "child_id", "relation"]]  # 取出需要的数据
    
    list = []
    nodes = []
    categories = []
    
    ##连接neo4j数据库,输入地址、用户名、密码
    graph = Graph('http://localhost:7474', auth=("neo4j", "root"))

    2.数据去重,并且创建结点

    ##创建结点
    
    i = 0
    uniqueId = []
    uniqueChildId = []
    for row in X.iteritems():
        if i == 0:
            uniqueChildId = np.unique(row[1].values)
            nodes.append(uniqueChildId)
            print(uniqueChildId)
            i =i+1
            continue
        if i == 1:
            uniqueId = np.unique(row[1].values)
            nodes.append(uniqueId)
            print(uniqueId)
            i=i+1
            continue
    uniqueId = np.append(uniqueId,uniqueChildId)
    
    globalUnique = np.unique(uniqueId)
    print(globalUnique) #id以及子id去重后的全局唯一id
    j = 0
    for row in globalUnique:
        graph.create(Node('血缘关系', name=int(row)))
        j+=1
        print('node:'+str(j))

     

    3.创建关系

    # ##创建关系
    k = 0
    for row in X.itertuples():
        list.append({"source":str(row.id),"target":str(row.child_id)})
        relation = ""
        if(row.relation=='s'):
            relation = "儿子"
        elif(row.relation=='m'):
            relation = "母亲"
        elif(row.relation=='f'):
            relation = "父亲"
        rel = Relationship(NodeMatcher(graph).match(name=row.id).first(),
                           relation,
                           NodeMatcher(graph).match(name=row.child_id).first())
        print(rel)
        k+=1
        print('res:'+str(k))
        graph.create(rel)

    整个过程大概持续2个小时左右(10万条数据)。

    (3)通过查看可视化数据如下:

     

    通过上图可以看到,数据的逻辑关系已经展示出来,但是有一个问题,就是数据的逻辑关系混乱(同一个人,既是儿子,又是母亲)

    大数据环境下,数据无法做到百分百正确,需要根据一些特定参数进行修正,但是规则也不是随意指定的,此处我们没有更多的信息,暂不修正逻辑错误。

    (4)通过查询结点

    查询指定属性结点:

    MATCH (n)-->(b) where b.name = 1296

    return b

    查询指定关系结点:

    MATCH p=()-[r:`儿子`]->() RETURN p

    查询指定label结点:

    MATCH (n:`血缘关系`) RETURN n

    按指定度查询结点:

    MATCH (k)

    WITH k, size((k)--()) as degree

    WHERE degree = 1

    MATCH (k)--(n)

    RETURN n,k,degree

     

    查找结点最短路径:

    MATCH (p1:`血缘关系` {name:9311}),(p2:`血缘关系`{name:365}),

    p=shortestpath((p1)-[*..10]->(p2))

    RETURN p

    注:[*…10]表示查询路径长度10以内的关系

  • 相关阅读:
    3.Appium运行时出现:Original error: Android devices must be of API level 17 or higher. Please change your device to Selendroid or upgrade Android on your device
    3.Python连接数据库PyMySQL
    2.Python输入pip命令出现Unknown or unsupported command 'install'问题解决
    2.Linux下安装Jenkins
    5.JMeter测试mysql数据库
    Android 4学习(7):用户界面
    Android 4学习(6):概述
    Android 4学习(5):概述
    Android 4学习(4):概述
    Android 4学习(3):概述
  • 原文地址:https://www.cnblogs.com/fengwenzhee/p/15626038.html
Copyright © 2020-2023  润新知