Neo4j相关

Posted by stq on September 26, 2021

查询n个节点之间的全部关系

MATCH path = (p1:Person)-[*]-(p2:Person)
WHERE p1.姓名 IN ["张三", "李四", "王五"]
AND p2.姓名 IN ["张三", "李四", "王五"]
RETURN path

python读取全量数据写入neo4j

#merge_nodes
from py2neo import Graph
import psycopg2
from py2neo.bulk import create_nodes,merge_nodes
from itertools import islice
import time
#读取数据
if __name__ == '__main__':
    db=psycopg2.connect("dbname=postgres user=postgres password=Cath.123 host=127.0.0.1 port=5432")
    cur=db.cursor()
    keys="foo,bar"
    keyss=keys.split(',')
    limit=1000000
    print(limit)

#随机读取N条数据
    sqls="select "+keys+" from czrkjbxx order by hh asc  limit "+str(limit)
    nodes=[]
#连接neo4j
    graph = Graph("bolt://127.0.0.1:7687", auth=("neo4j", "Cath.123"))
#多少条数据保存一次
batch_size=10000
if batch_size>limit:
    batch_size=limit
try:
    cur.execute(sqls)
    print('query:%s' % sqls)
    data=cur.fetchmany(batch_size)
    ta0=time.time()
    cnt=0
    while data:
        cnt=cnt+1
        print('load data size:%s' % len(data))
        for d in data:
            p={'xm':''}
            for i in range(len(keyss)):
                p[keyss[i]]=d[i]
            nodes.append(p)
            if len(nodes)>=batch_size:
                #启动节点创建
                ta=time.time()
                merge_nodes(graph.auto(),nodes,('Person','gmsfhm'),labels={'Person'})
                nodes.clear()
                print("提交 %s 次,耗时:%s秒" % (cnt,round(time.time()-ta)))
            #启动一个自动提交的事务
        if len(nodes)>0:
            merge_nodes(graph.auto(),nodes,('Person','gmsfhm'),labels={'Person'})
            nodes.clear()
        data=cur.fetchmany(batch_size)

except(IOError):
    print('发生错误')
print("neo4j中有%s条数据,总耗时:%s秒" % (str(graph.nodes.match('Person').count()),round(time.time()-ta0)))
cur.close()
db.close()