1. linux 部署参考 https://blog.csdn.net/u013946356/article/details/81736232
我用的是4.2.6版本的,需要JDK11才行, 该版本里面有些要修改的参数可能找不到,找不到就不用管了
可以新建一个用户,在该用户下安装JDK11
vim ~/.bashrc 下添加后,然后source ~/.bashrc
export JAVA_HOME=/dataxRelated/jdk-11.0.11
CLASSPATH=.:$JAVA_HOME/bin/tools.jar
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME CLASSPATH PATH
2. load csv数据(节点Node以及关系Relation)
比如:
node csv数据样例:
nodeId
"1"
"2"
"3"
"4"
"5"
数据导入: 在该输入框中输入以下命令(注意file的路径)
:auto USING PERIODIC COMMIT 1000 LOAD CSV WITH HEADERS FROM "file:///dataxRelated/neo4j-community-4.2.6/import/nodes_construct.csv" AS row CREATE (n:nodes
{nodeId: row.nodeId})
relation csv样例数据:
parentNode childNode
1 2
1 3
2 3
2 4
3 5
关系导入
LOAD CSV WITH HEADERS FROM "file:///dataxRelated/neo4j-community-4.2.6/import/nodes_relations.csv" AS line
match (from:nodes{nodeId:line.parentNode}),(to:nodes{nodeId:line.childNode})
merge (from)-[r:父子]->(to)
这时我们就能在页面看到node以及relation了(类似这样,图为生产数据,与样例不符)
3. Python导入数据
能程序导入谁会选择手动呢!!!
节点数据和关系数据都是存在mysql的,所以是从mysql中查询
另外需要注意,先导入节点,后导入关系,要先判断构成关系的两个节点是否已经存在
比如我们导入了Node1 '张三',如果我们在构建Node1 '张三'--->Node2 'zs'时不判断直接构建,会新建一个等于'张三'的节点,而不是使用原来创建过的节点
import pymysql
from py2neo import Graph, Node, Relationship,NodeMatcher
import time
import datetime
def writeRelation2Neo4j():
g = Graph("bolt://***:7687", auth=("账号", "密码"))
tx = g.begin()
matcher = NodeMatcher(g)
db = pymysql.connect(host='localhost', user='root', password='123456',
database='authbase')
cursor = db.cursor()
sqlsectPsql = "SELECT `childNode`,`parentNode` FROM `nodes` "
try:
# 执行sql
cursor.execute(sqlsectPsql)
# 获取所有结果列表:fetchall()
results = cursor.fetchall()
for row in results:
child = str(row[0])
parent = str(row[1])
#判断节点是否已经存在,如存在直接使用,不存在则新建
c = matcher.match("Nd", nodeId=child).first()
p = matcher.match("Nd", nodeId=parent).first()
if c is None:
print("a为空")
c = Node("Nd", nodeId=child)
tx.create(c)
if p is None:
print("b为空")
p = Node("Nd", nodeId=parent)
tx.create(p)
pc = Relationship(p, "父子", c)
tx.create(pc)
except:
import traceback # 打印出报错具体信息
traceback.print_exc()
print("Error: unable to fetch data")
cursor.close()
db.close()
tx.commit()
def writeNode2Neo4j():
g = Graph("bolt://****:7687", auth=("账号", "密码"))
tx = g.begin()
db = pymysql.connect(host='localhost', user='root', password='123456',
database='authbase')
cursor = db.cursor()
sqlsectPsql = "SELECT `nodeid` FROM `nodeidandname` "
try:
# 执行sql
cursor.execute(sqlsectPsql)
# 获取所有结果列表:fetchall()
results = cursor.fetchall()
for row in results:
pNode = str(row[0])
# print(pNode)
a = Node("Nd", nodeId=pNode)
# time.sleep(0.01)
tx.create(a)
# csv_writer.writerow(pNode)
except:
import traceback # 打印出报错具体信息
traceback.print_exc()
print("Error: unable to fetch data")
cursor.close()
db.close()
tx.commit()
if __name__ == '__main__':
startTime = datetime.datetime.now()
#导入节点
writeNode2Neo4j()
#导入关系
writeRelation2Neo4j()
endTime = datetime.datetime.now()
seconds = (endTime - startTime).seconds
print('任务用时' + str(seconds) + 's')
4. 数据查询(关联查询 ..N N表示查询多少层有关联关系的节点)
#向上查询
MATCH p=()-[r:`父子`*..4]->(a:nodes) where a.nodeId="**" RETURN p
#向下查询
MATCH p=(a:nodes)-[r:`父子`*..4]->() where a.nodeId="**" RETURN p