python 实现查询Neo4j多节点的多层关系

需求:查询出满足3人及3案有关系的集合

# -*- coding: utf-8 -*-
from py2neo import Graph
import psycopg2

# 二维数组查找
def find(target, array):
 for i, val in enumerate(array):
  for j, temp in enumerate(val):
   if temp == target:
    return True;
 return False

graph = Graph(host="192.168.3.186://7474", auth=("neo4j", "wabjtam123"))
data = graph.run('match data=(p:anjian)-[:参与 *2..5]-(u:anjian) with p,collect( distinct u.AJBH) as ajbhs match (p:anjian)-[:参与 *1..5]-(c:xyr) with p,ajbhs, collect(distinct c.XYRBH) as xyrbh_list,(ajbhs+p.AJBH) as ajbh_list where size(xyrbh_list)>2 and size(ajbh_list)>2 return p.AJBH as ajbh,ajbh_list,xyrbh_list')

aj = []

xyr_result = []
aj_result = []

for node in data:
 ajbh = node.get('ajbh')
 ajbh_list = node.get('ajbh_list')
 xyrbh_list = node.get('xyrbh_list')

 ajbh_list.append(ajbh)

 ajbh_list = list(set(node.get('ajbh_list')))
 xyrbh_list = list(set(node.get('xyrbh_list')))

 flag = True
 if len(aj_result) > 0:
  for i, val in enumerate(ajbh_list):
   f = find(val, aj_result) 

   if f:
    flag = False
    break;

 if flag:
  if len(ajbh_list) > 2 and len(xyrbh_list) > 2:
   aj.append(ajbh) 

   aj_result.append(ajbh_list)
   xyr_result.append(xyrbh_list)

   print('MATCH p=(:anjian{AJBH:"'+ajbh+'"})-[*]-() RETURN p LIMIT 25')
   name = str(len(xyrbh_list)) + "人" + str(len(ajbh_list)) + "案"
   print(name)

print(len(aj_result))
print(aj)
print('----------------------------')
print(aj_result)

conn = psycopg2.connect(database="zfgfh", user="postgres", password="postgres", host="192.168.3.202", port="5432")
cursor = conn.cursor()

for ajbh in aj_result:
 for temp in ajbh:
  insert_sql = "insert into test.gm (\"ajbh\") values ('%s')"
  insert_sql = insert_sql % temp
  print(insert_sql)
  # 插入
  cursor.execute(insert_sql)
# 提交
conn.commit()
cursor.close() # 关闭Cursor
conn.close() # 关闭数据库

6人3案
4人5案
3人6案
11人3案
3人13案
6人46案
8人4案
5人4案
7人4案
28人17案
4人4案
3人8案
4人4案
8人3案
6人6案
5人4案
4人6案
19人77案
4人22案
4人14案
4人5案
9人5案
12人8案
4人3案
17人4案
5人7案
18人30案
7人11案
3人22案
20人12案
30
['A2301260500002019020002', 'A2301096000002018040010', 'A2301246400002018060001', 'A2301270500002018040003', 'A2301085100002018100001', 'A2301062020012018100099', 'A2301830517002018090003', 'A2301290501002019040001', 'A2301240500002018080001', 'A2301260200002018090006', 'A2301821414022018011647', 'A2301000514012018090001', 'A2301245100002018050014', 'A2301004908002018110004', 'A2301295500002018110005', 'A2301270300002018090005', 'A2301848900002018070017', 'A2301210504002018080011', 'A2301004902002018110002', 'A2301810500002018110038', 'A2301025103002018120002', 'A2301290500002019020003', 'A2301096000002018060043', 'A2301260200002018060002', 'A2301810500002018090002', 'A2301815100002018060014', 'A2301820506002018070006', 'A2301037200002018100154', 'A2301849000002018090001', 'A2301280200002018110002']
----------------------------
['A2301260500002019030006', 'A2301260500002019030007', 'A2301260500002019020002']
['A2301095200002018060039', 'A2301096000002018050002', 'A2301096000002018060009', 'A2301095200002018070016', 'A2301096000002018040010']
['A2301246400002018050003', 'A2301245200002018060007', 'A2301236400002018060005', 'A2301245200002018060003', 'A2301246400002018060002', 'A2301246400002018060001']
['A2301270500002018040003', 'A2301270500002018040004', 'A2301270500002018040005']
['A2301080506002018120002', 'A2301080506002018120001', 'A2301080506002018120011', 'A2301080506002018120004', 'A2301080506002018120010', 'A2301080506002018120013', 'A2301080506002018120005', 'A2301085100002018100001', 'A2301080506002018120012', 'A2301080506002018120007', 'A2301080506002018120006', 'A2301080506002018120003', 'A2301080506002018120009']
['A2301060504002018110006', 'A2301060504002018110005', 'A2301060504002018110028', 'A2301060504002018110026', 'A2301060504002018110036', 'A2301060504002018110035', 'A2301060504002018110030', 'A2301060504002018110003', 'A2301061723042018103961', 'A2301060504002018110024', 'A2301060504002018110004', 'A2301060504002018110025', 'A2301060504002018110044', 'A2301063010412018100861', 'A2301060504002018110022', 'A2301060504002018110031', 'A2301060504002018110038', 'A2301060504002018110007', 'A2301060504002018110032', 'A2301060504002018110009', 'A2301060504002018110039', 'A2301063011482018102790', 'A2301060504002018110029', 'A2301060504002018110021', 'A2301060504002018100001', 'A2301060504002018110015', 'A2301060504002018110043', 'A2301062020012018100099', 'A2301060504002018110011', 'A2301060504002018110040', 'A2301060504002018110023', 'A2301060504002018110027', 'A2301060504002018110037', 'A2301060504002018110014', 'A2301060504002018110033', 'A2301060504002018110002', 'A2301060504002018110041', 'A2301060504002018110008', 'A2301060504002018110001', 'A2301060504002018110010', 'A2301060504002018100002', 'A2301060504002018110019', 'A2301060504002018120004', 'A2301060504002018110018', 'A2301060504002018110020', 'A2301060504002018110034']
['A2301830517002018090003', 'A2301830517002018090005', 'A2301830517002018090006', 'A2301830517002018090004']
['A2301290501002019040001', 'A2301290501002019040002', 'A2301290501002019030002', 'A2301290501002019040004']
['A2301240500002018080001', 'A2301240500002018080004', 'A2301240500002018080003', 'A2301240500002018080002']
['A2301260200002018100006', 'A2301260200002018090002', 'A2301260200002018100013', 'A2301260200002018100008', 'A2301260200002018090006', 'A2301260200002018100003', 'A2301260200002018100001', 'A2301260200002018100010', 'A2301260200002018100005', 'A2301260200002018100012', 'A2301260200002018100002', 'A2301260200002018090004', 'A2301260200002018100011', 'A2301260200002018090003', 'A2301260200002018100014', 'A2301260200002018090005', 'A2301260200002018100007']
['A2323037100002018120006', 'A2301827600002018090005', 'A2301821414022018011647', 'A2301820209332018024067']
['A2301000514012018090001', 'A2301230500002018060001', 'A2301000514012018110004', 'A2301230300002018110005', 'A2301230500002018050005', 'A2301000514012019010001', 'A2301000514012018090002', 'A2301000514012018100001']
['A2301245100002018050014', 'A2301245100002018070018', 'A2301286100002018050005', 'A2301286100002018060002']
['A2303016200002018100001', 'A2301004908002018110004', 'A2301004908002018110006']
['A2301295500002018110008', 'A2301295500002018110006', 'A2301295500002018110003', 'A2301295500002018110007', 'A2301295500002018110005', 'A2301002104002018100007']
['A2301270300002018100004', 'A2301270300002018100002', 'A2301270300002018100001', 'A2301270300002018090005']
['A2301848900002018070017', 'A2301845700002018070027', 'A2301840502002018070002', 'A2301848800002018070024', 'A2301840502002018070003', 'A2301840502002018070001']
['A2301210504002018120003', 'A2301210504002018120019', 'A2301210504002018120013', 'A2301210504002018080007', 'A2301210504002018080021', 'A2301210504002018080015', 'A2301210504002018120017', 'A2301210504002018120045', 'A2301210504002018080014', 'A2301210504002018120049', 'A2301210504002018080019', 'A2301210504002018120028', 'A2301210504002018080004', 'A2301210504002018120029', 'A2301210504002018120051', 'A2301210504002018110005', 'A2301210504002018120023', 'A2301210504002018120043', 'A2301210504002018120044', 'A2301210504002018120041', 'A2301210504002018120014', 'A2301210504002018120032', 'A2301210504002018080003', 'A2301210504002018110006', 'A2301210504002018080009', 'A2301210504002018110004', 'A2301210504002018110003', 'A2301210504002018120038', 'A2301210504002018120039', 'A2301210504002018120015', 'A2301210504002018120011', 'A2301210504002018120036', 'A2301210504002018080010', 'A2301210504002018120048', 'A2301210504002018120025', 'A2301210504002018120006', 'A2301210504002018120012', 'A2301210504002018080013', 'A2301210504002018120022', 'A2301210504002018080017', 'A2301210504002018120008', 'A2301210504002018120046', 'A2301210504002018120052', 'A2301210504002018110007', 'A2301210504002018120024', 'A2301210504002018080022', 'A2301210504002018080005', 'A2301210504002018120050', 'A2301210504002018080002', 'A2301210504002018120009', 'A2301210504002018120031', 'A2301210504002018120033', 'A2301210504002018120042', 'A2301210504002018120035', 'A2301210504002018120040', 'A2301210504002018120026', 'A2301210504002018120005', 'A2301210504002018120030', 'A2301210504002018120027', 'A2301210504002018120018', 'A2301210504002018120010', 'A2301210504002018120016', 'A2301210504002018120002', 'A2301210504002018120001', 'A2301210504002018120034', 'A2301210504002018120007', 'A2301210504002018120037', 'A2301210504002018120004', 'A2301210504002018080008', 'A2301210504002018080011', 'A2301210504002018080018', 'A2301210504002018120021', 'A2301210504002018080020', 'A2301210504002018120047', 'A2301210504002018080006', 'A2301210504002018120020', 'A2301210504002018080016']
['A2301004906002019010005', 'A2301004902002018120001', 'A2301004902002018100006', 'A2301004908002018110002', 'A2301004902002018110002', 'A2301004906002019010003', 'A2301004904002019010003', 'A2301004908002019010002', 'A2301004902002018110003', 'A2301004908002019010004', 'A2301004908002018110001', 'A2301004905002019010008', 'A2301004904002018100004', 'A2301004908002019010005', 'A2301004904002019010004', 'A2301004909002019010001', 'A2301004905002019010007', 'A2301004908002019010003', 'A2301004904002019010002', 'A2301004908002018100002', 'A2301004902002018110001', 'A2301004906002019010004']
['A2301810500002018120044', 'A2301810500002018110039', 'A2301810500002018110041', 'A2301810500002018120034', 'A2301810500002018120055', 'A2301810500002018120052', 'A2301810500002018120053', 'A2301810500002018120032', 'A2301810500002018110038', 'A2301810500002018120054', 'A2301810500002018120035', 'A2301810500002018110040', 'A2301810500002018120033', 'A2301810500002018120056']
['A2301025103002019010002', 'A2301025103002018120002', 'A2301025103002018120004', 'A2301025103002018120005', 'A2301025103002019010001']
['A2301290500002019020004', 'A2301293014052013074923', 'A2301292809352016111088', 'A2301290500002019020003', 'A2301290500002019030001']
['A2301090506002018090006', 'A2301096000002018060044', 'A2301090506002018090002', 'A2301090506002018090004', 'A2301096000002018060045', 'A2301090506002018090005', 'A2301090506002018090003', 'A2301096000002018060043']
['A2301260200002018070019', 'A2301260200002018060002', 'A2301260200002018070001']
['A2301810500002018100008', 'A2301810500002018090002', 'A2301810500002018070015', 'A2301810500002018100006']
['A2301815100002018060014', 'A2301847300002018070003', 'A2301837900002018060010', 'A2301847300002018070002', 'A2301835600002018060007', 'A2301815500002018050010', 'A2301817900002018060001']
['A2301820502002018120006', 'A2301820502002019020003', 'A2301820502002018110004', 'A2301820502002018120003', 'A2301820502002018120014', 'A2301820502002018120001', 'A2301820502002018120002', 'A2301820502002018110005', 'A2301820502002018110007', 'A2301820502002018110010', 'A2301820502002018110002', 'A2301820506002018070006', 'A2301820502002018120004', 'A2301820502002018120009', 'A2301820502002018110012', 'A2301820502002018110014', 'A2301820502002018110013', 'A2301820502002018110011', 'A2301820502002019020002', 'A2301820502002018120005', 'A2301820502002018110006', 'A2301820502002019010001', 'A2301820502002018120013', 'A2301820502002019020001', 'A2301820502002018110009', 'A2301820502002018110008', 'A2301820502002018120011', 'A2301820502002018120010', 'A2301820502002018110001', 'A2301820502002018110003']
['A2301025600002018120006', 'A2301031714062013011545', 'A2301037200002018100154', 'A2301025700002018120027', 'A2301032513122013013630', 'A2301025700002018120050', 'A2301025600002018120051', 'A2301032816032012120005', 'A2301030916252013014087', 'A2301026600002018120008', 'A2301025600002018120044']
['A2323015500002018100003', 'A2301849000002018090018', 'A2323015700002018100005', 'A2301848700002018090013', 'A2323015500002018100004', 'A2301849000002018090001', 'A2301265300002018090007', 'A2301845700002018090004', 'A2301265500002018090005', 'A2301845700002018090019', 'A2323015700002018100004', 'A2301265300002018090006', 'A2323016100002018100003', 'A2301849000002018090008', 'A2301265300002018090011', 'A2301265300002018090005', 'A2301849000002018090019', 'A2301265500002018090006', 'A2301265300002018090010', 'A2301265300002018090009', 'A2301265300002018090008', 'A2301848700002018090006']
['A2301280200002018110003', 'A2301280200002018110011', 'A2301280200002018110013', 'A2301280200002018110005', 'A2301280200002018110010', 'A2301280200002018110007', 'A2301280200002018110012', 'A2301280200002018110002', 'A2301280200002018110008', 'A2301280200002018110009', 'A2301280200002018110014', 'A2301280200002018110006']

以上这篇python 实现查询Neo4j多节点的多层关系就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • python定位xpath 节点位置的方法

    chrome 右键有copy xpath地址 但是有些时候获取的可能不对 可以自己用代码验证一下 如果还是不行 可以考虑从源码当中取出来 趁热打铁,使用前一篇文章中 XPath 节点来定位HTML 页面. HTML文件如下(您可以将其拷贝,保存成html文件,跟我笔者实验): <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title

  • 在Python中使用Neo4j的方法

    Neo4j是面向对象基于Java的 ,被设计为一个建立在Java之上.可以直接嵌入应用的数据存储.此后,其他语言和平台的支持被引入,Neo4j社区获得持续增长,获得了越来越多的技术支持者.目前已支持.NET.Ruby.Python.Node.js及PHP等.因此,不管是什么项目,没有理由不引入Neo4j. 本文重点介绍Python,这门语言的哲学与Java大大不同,同时展示py2neo库如何被用来建立一个简单的应用程序. 一个快速的REST例子 首先来看些基本知识.如果没有服务API,Neo4j

  • python计算无向图节点度的实例代码

    废话不多说了,直接上代码吧: #Copyright (c)2017, 东北大学软件学院学生 # All rightsreserved #文件名称:a.py # 作 者:孔云 #问题描述:统计图中的每个节点的度,并生成度序列 #问题分析:利用networkx.代码如下: import networkx as nx G=nx.random_graphs.barabasi_albert_graph(1000,3)#生成n=1000,m=3的无标度的图 print ("某个节点的度:",G.d

  • python 实现查询Neo4j多节点的多层关系

    需求:查询出满足3人及3案有关系的集合 # -*- coding: utf-8 -*- from py2neo import Graph import psycopg2 # 二维数组查找 def find(target, array): for i, val in enumerate(array): for j, temp in enumerate(val): if temp == target: return True; return False graph = Graph(host="192

  • python使用py2neo查询Neo4j的节点、关系及路径

    一.连接Neo4j数据库 本文中会用到多种数据类型,在此一并引用 <import numpy as np import pandas as pd from py2neo import Node,Relationship,Graph,Path,Subgraph from py2neo import NodeMatcher,RelationshipMatcher 配置Neo4j数据库的访问地址.用户名和密码: neo4j_url = '访问地址' user = '用户名' pwd = '密码' 在此

  • Python使用Py2neo创建Neo4j的节点、关系及路径

    目录 一.安装Py2neo 二.连接Neo4j数据库 1. 使用graph.run执行Cypher语句创建节点 2. 使用Node数据结构创建节点 3. 使用Node.Relationship和Subgraph数据结构创建节点和关系 三.性能对比 一.安装Py2neo 使用pip安装Py2neo,执行: pip install py2neo 查看已安装的Py2neo是什么版本的: pip show py2neo 二.连接Neo4j数据库 本文中会用到多种数据类型,在此一并引用 import nu

  • python使用py2neo创建neo4j的节点和关系

    1.核心代码 使用py2neo连接neo4j的方法: from py2neo import Graph graph = Graph("http://localhost:7474", auth=("neo4j", "neo4j")) graph.delete_all()  # 删除已有的所有内容 根据dict创建Node: from py2neo import Node node = Node(**{"key":"va

  • 在Python中使用Neo4j数据库的教程

     一个快速的REST例子 首先来看些基本知识.如果没有服务API,Neo4j就不能支持其他语言.该接口提供一组基于JSON消息格式的RESTful Web服务和一个全面的发现机制.使用中使用这个接口的最快和最容易的方法是通过使用cURL: $ curl http://localhost:7474/db/data/ { "extensions" : { }, "node" : "http://localhost:7474/db/data/node"

  • Python批量查询域名是否被注册过

    step1. 找一个单词数据库 这里有一个13万个单词的 http://download.csdn.net/detail/u011004567/9675906 新建个mysql数据库words,导入words里面就行 step2.找个查询接口 这里我用的是http://apistore.baidu.com/astore/serviceinfo/27586.html step3. 执行Python脚本 # -*- coding: utf-8 -*- ''' 域名注册查询 ''' __author_

  • Python模糊查询本地文件夹去除文件后缀的实例(7行代码)

    7行代码实现的,废话不多说,直接上代码: import os,re def fuzzy_search(path): word= input('请输入要查询的内容:') for filename in os.listdir(path): #遍历指定文件夹 re_filename = re.findall('.\w+', str(filename)) #去除文件后缀名 if word in re_filename[0]: print(re_filename[0]) 以上这篇Python模糊查询本地文

  • python 通过xml获取测试节点和属性的实例

    写在前面:通过xml获取测试数据,主要是为了使数据参数化.测试脚本和测试数据分离,使得脚本清晰容易维护,方便排查问题. XML:可扩展的标记语言,是一种用于标记电子文件使其具有结构行的标记语言. 自动化测试中的使用场景: 1. 经常变动的测试数据: 2. 数据量大,不方便放在脚本中: 3. 数据作用于多个地方: 4. 相同测试用例,可以使用不同的数据: 5. 例:不稳定,后续改动较多功能:容易出错的功能 XML特征 • 具有自我描述性,本身不做任何事情 • 声明部分 <?xml version=

  • Python实现查询某个目录下修改时间最新的文件示例

    本文实例讲述了Python实现查询某个目录下修改时间最新的文件.分享给大家供大家参考,具体如下: 通过Python脚本,查询出某个目录下修改时间最新的文件. 应用场景举例:比如有时候需要从ftp上拷贝自己刚刚上传的文件,那么这时就需要判断哪个文件的修改时间是最新的,即最后修改的文件是我们的目标文件. 直接撸代码: # -*- coding: utf-8 -*- import os import shutil def listdir(path, list_name): #传入存储的list for

随机推荐