python中使用neo4j

参考上片问文档,除了在控制台输入查询外,还可在python中执行查询命令

1.find单个节点。查询tom信息

bash 复制代码
from py2neo import Graph

graph = Graph('bolt://xx.xx.xx.xx:7687',auth=("neo4j","neo4j1234"))
print ("数据库连接正常")

result = graph.run("match (tom {name:'Tom Hanks'}) return tom")

for record in result:
    person_node = record["tom"]
    print(person_node)

2.以节点中的某个属性去查询

修改了下脚本,将url等登录信息放进配置文件中。

bash 复制代码
import configparser
from py2neo import Graph

config = configparser.ConfigParser()
config.read('config.ini')

#获取数据库连接
neo4j_config = config['neo4j']
neo4j_url = neo4j_config['neo4jUrl']
neo4j_name = neo4j_config['neo4jName']
neo4j_passwd = neo4j_config['neo4jPasswd']


graph = Graph(neo4j_url,auth=(neo4j_name,neo4j_passwd))
print ("数据库连接正常")

result = graph.run("match (cloudAtlas {title:'Cloud Atlas'}) return cloudAtlas").data()

for record in result:
    person_node = record["cloudAtlas"]
    print(person_node)

3.查询指定个数的信息

python 复制代码
import configparser
from py2neo import Graph

config = configparser.ConfigParser()
config.read('config.ini')

#获取数据库连接
neo4j_config = config['neo4j']
neo4j_url = neo4j_config['neo4jUrl']
neo4j_name = neo4j_config['neo4jName']
neo4j_passwd = neo4j_config['neo4jPasswd']


graph = Graph(neo4j_url,auth=(neo4j_name,neo4j_passwd))
print ("数据库连接正常")

result = graph.run("match (cloudAtlas:Person) return cloudAtlas limit 10")

for record in result:
    person_node = record["cloudAtlas"]
    print(person_node)

4.条件查询

python 复制代码
import configparser
from py2neo import Graph

config = configparser.ConfigParser()
config.read('config.ini')

#获取数据库连接
neo4j_config = config['neo4j']
neo4j_url = neo4j_config['neo4jUrl']
neo4j_name = neo4j_config['neo4jName']
neo4j_passwd = neo4j_config['neo4jPasswd']


graph = Graph(neo4j_url,auth=(neo4j_name,neo4j_passwd))
print ("数据库连接正常")

result = graph.run("match (nineties:Movie) where nineties.released >= 1990 and nineties.released < 2000 return nineties.title limit 5")

for record in result:
    person_node = record["nineties.title"]
    print(person_node)

5.查询出tom出演的电影名称

python 复制代码
import configparser
from py2neo import Graph

config = configparser.ConfigParser()
config.read('config.ini')

#获取数据库连接
neo4j_config = config['neo4j']
neo4j_url = neo4j_config['neo4jUrl']
neo4j_name = neo4j_config['neo4jName']
neo4j_passwd = neo4j_config['neo4jPasswd']


graph = Graph(neo4j_url,auth=(neo4j_name,neo4j_passwd))
print ("数据库连接正常")
query="match (tom:Person{ name:'Tom Hanks'})-[:ACTED_IN]->(tomHanksMovies) return tom, tomHanksMovies"
result = graph.run(query)

for record in result:
    name = record["tom"]
    movies = record["tomHanksMovies"]
    print(name, movies)

后续的操作,只需要在query中填写需要的查询命令,就能操作各种查询。

相关推荐
阿enna2 小时前
从误检和漏检出发改进YOLO26n:建立错误样本库
人工智能·python·深度学习·yolo·目标检测·机器学习·cnn
青少儿编程课堂2 小时前
蓝桥杯真题解析:手链配色计数与波利亚定理(全国青少年信息素养大赛与电子学会软件编程考级)
c++·python·算法·bfs·信息学竞赛
曲径海盗2 小时前
【Python 学习第 66 天】字典基础
python·学习笔记·编程入门
布莱克6052 小时前
Swift 可选链详解:从语法到实战
开发语言·ios·swift·可选链
布莱克6052 小时前
Swift 类型转换详解:从基础语法到实战技巧
开发语言·ios·swift·类型转换
码途行者3 小时前
每天10分钟学Go:第23课 协程(goroutine)
开发语言·后端·golang
专注仿真3 小时前
仿真智能体训练:强化学习中的生存、任务、杀伤、能耗与规则惩罚算法详解与实战
python·算法·afsim智能体·仿真训练·afsim智能体训练
cpolar技术支持4 小时前
datetime.now() 和 datetime.now(timezone.utc) 混进 SQLite:零报错,读出来差 8 小时
python·sqlite·时间戳·datetime·时区
艾莉丝努力练剑4 小时前
【AI大模型接入SDK】项目总结
java·开发语言·前端·c++·人工智能·学习·架构
Evand J4 小时前
【MATLAB例程,无人机避障7】基于RRT*与人工势场法(RRT*+APF)的多无人机二维动态路径规划与避障。附下载链接
开发语言·matlab·无人机·rtt·路径规划·人工势场