图数据库neo4j-Echarts可视化

由于时效问题,该文某些代码、技术可能已经过期,请注意!!!本文最后更新于:2 年前

Neo4j Flask Echarts

安装 Neo4j

Neo4j依赖Java环境,所以需要先安装JDK, JDK下载地址如下:

Neo4j下载地址如下:

社区版是免费的,所以选择社区版安装最新版即可。
另:Neo4j 插件 apoc 安装地址如下(插件根据需求安装):

插件这里有个小坑,就是版本之间的依赖,如果Neo4j下载的是最新版,插件也最好选择最新版进行安装。
下载完后直接放在 neo4j-community-4.4.4/plugins 目录下,并且需要在配置文件里进行设置。

启动 Neo4j

如果是桌面端直接运行程序即可,如果是server端配置好环境后运行 neo4j start 即可启动,停止运行命令:neo4j stop
启动后可以在浏览器输入 http://localhost:7474/ 进入数据库,初始用户名和密码均是:neo4j,第一次进入需要修改密码,自行修改即可。
另外neo4j自带的数据库为 system 和 neo4j。如果想创建一个新的数据库需要在配置文件里修改,配置文件地址为:

  • neo4j-community-4.4.4/conf/neo4j.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
修改默认数据库
#The name of the default database
#dbms.default_database=neo4j
dbms.default_database=test

修改插件配置
# A comma separated list of procedures and user defined functions that are allowed
# full access to the database through unsupported/insecure internal APIs.
#dbms.security.procedures.unrestricted=my.extensions.example,my.procedures.*
dbms.security.procedures.unrestricted=apoc.*

修改免密登陆(测试需求)
# Whether requests to Neo4j are authenticated.
# To disable authentication, uncomment this line
dbms.security.auth_enabled=false

其它配置有需求再进行修改。

Neo4j 节点和关系创建
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
## pip install py2neo 
from py2neo import *

# 连接neo4j数据库,输入地址、用户名、密码
graph = Graph("bolt://localhost:7687", auth=('neo4j','neo4j'))
graph.delete_all() #清除neo4j中原有的结点等所有信息

# 随机生成100个节点
N = 100
node_ls = []
for i in range(N):
random_name = "P"+str(round(random.random()*N*2))
random_age = round(random.random()*15)
node = Node("Person", name=random_name, age=random_age)
node_ls.append(node)

subgraph = Subgraph(node_ls, []) ## [] 代表关系为空
tx = graph.begin()
tx.create(subgraph)
graph.commit(tx)

详情可参考:https://mp.weixin.qq.com/s/1DnMHOx6jPi0j0GhZwNPqw

Echarts可视化

neo4j本身有自己的可视化系统,比如browser、Bloom等,但是blowser面向开发者,Bloom又是收费的,所以只能自己动手了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
## 主要就是 option的设置
var categories = [{name:"DrugName"},{name:"TargetGene"},{name:"DiseaseLabel"}];

option = {
// color:['#5470c6', '#91cc75', '#fac858', '#ee6666', '#73c0de', '#3ba272', '#fc8452', '#9a60b4', '#ea7ccc'],
tooltip: {
trigger: 'item',
triggerOn: 'mousemove',
enterable:true,//鼠标是否可进入提示框浮层中
formatter:formatterHover,//修改鼠标悬停显示的内容
},
color:["#c12e34", "#e6b600", "#0098d9",],
textStyle:{
fontFamily: "sans-serif",
fontSize: 12,
fontStyle: "normal",
fontWeight: "normal",
},
legend: {
data: ['DrugName', 'TargetGene', 'DiseaseLabel'] //此处的数据必须和关系网类别中name相对应
},
series: [{
type: 'graph',
layout: 'force', # 力导图
animation: false,
symbolSize: 25,
label: {
normal: {
show: false,
position: 'right',
textStyle: {
fontSize: 10
},
},
},
labelLayout: {
hideOverlap: true
},
draggable: true,
roam: true,
focusNodeAdjacency: true,
force: {
edgeLength: 80, //连线的长度
repulsion: 100, //子节点之间的间距
// gravity: 0.2
},
categories: categories,
data: data.nodes,
edges: data.edges,

edgeSymbol: ['circle', 'arrow'],
edgeSymbolSize: [4, 4],
edgeLabel: {
normal: {
show: false,
formatter: function (x) {
return x.data.relationship;
},
textStyle: {
fontSize: 10
}
}
},
lineStyle: {
normal: {
width: 2,
color: '#4b565b',
}
},
}]
};

剩下的就是把 data 和 edges 的数据传人即可。
同样可以用python进行请求获取

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import requests
import json

login_url = 'http://localhost:7474/db/data/transaction/commit'
headers = {'Content-Type':'application/json'}
name_pwd = {'username':'neo4j',
'password':'neo4j',
}


def request_result(login_url, query_cql, headers):

nodes, edges = [], []

query_json = {
"statements" : [ {
"statement" : query_cql,
"resultDataContents" : [ "row", "graph" ]
}]
}

response = requests.post(url=login_url, json= query_json, headers=headers)
print(response.status_code)

result = json.loads(response.text)['results'][0]['data']
for i in range(len(result)):
for n in result[i]['graph']['nodes']:
if n not in nodes:
nodes.append(n)

for r in result[i]['graph']['relationships']:
if r not in edges:
edges.append(r)

return nodes, edges


def query_all():
query_cql = 'match p=()-->() return p'

nodes, edges = request_result(login_url, query_cql, headers)

return nodes, edges


## 修改成 Echarts 需要的格式
def buildNodes(nodeRecord):
data = {"id": str(nodeRecord['id']), "label": next(iter(nodeRecord['labels']))}
data.update(nodeRecord['properties'])

tradeNames = data.get('TradeNames')
if tradeNames and len(tradeNames) > 20:
tradeNames = tradeNames[:20] + '...'
data['TradeNames'] = tradeNames

data['category'] = category_dict.get(data['label'])
# newdata = {'name': data['name'], 'category': category_dict.get(data['label'])}
return data

def buildEdges(relationRecord):
data = {"source": str(relationRecord['startNode']),
"target": str(relationRecord['endNode']),
"relationship": relationRecord['type']}
# return {"links": data}
return data


nodes, edges = query_all()
nodes = [buildNodes(i) for i in nodes]
edges = [buildEdges(i) for i in edges]

整体前端框架参考