当前位置: 首页 > ops >正文

python中使用neo4j

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

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

from py2neo import Graphgraph = 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等登录信息放进配置文件中。

import configparser
from py2neo import Graphconfig = 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.查询指定个数的信息

import configparser
from py2neo import Graphconfig = 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.条件查询

import configparser
from py2neo import Graphconfig = 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出演的电影名称

import configparser
from py2neo import Graphconfig = 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中填写需要的查询命令,就能操作各种查询。

 

http://www.xdnf.cn/news/6314.html

相关文章:

  • LeetCode 45. 跳跃游戏 II(中等)
  • 牛客网NC22015:最大值和最小值
  • 【Linux系列】Linux 系统下 SSD 磁盘识别
  • 二、xlib事件
  • tomcat项目重构踩坑易错点
  • 【RAP】RAP动作与流行舞蹈/街舞
  • 基于昇腾300IDUO 部署PaddleOCR模型
  • mock 数据( json-server )
  • React学习———Redux 、 React Redux和react-persist
  • <C++> MFC自动关闭对话框(MessageBoxTimeout)
  • Vue 学习随笔系列二十三 -- el-date-picker 组件
  • Python中的虚拟环境
  • U9C与钉钉审批流对接完整过程
  • lesson02-PyTorch开发环境安装
  • 光谱相机的图像预处理技术
  • 当服务器出现宕机情况该怎么办?
  • Axure中继器高保真交互原型的核心元件
  • 科技的成就(六十八)
  • 网络原理 | 网络基础概念复习
  • Automatic Recovery of the Atmospheric Light in Hazy Images论文阅读
  • 使用 SiamMask 实现单目标逐帧跟踪与掩码中心提取
  • 深入探索 OpenCV:从实时视频流到图像处理的实战指南
  • 技术文档不完善,如何促进知识传承
  • AI日报 · 2025年5月15日|GPT-4.1 登陆 ChatGPT
  • [原创](现代Delphi 12指南):[macOS 64bit App开发]: 注意“回车换行“的跨平台使用.
  • 基于YOLO算法的目标检测系统实现指南
  • 蓝桥杯11届国B 约数
  • 小程序映射逻辑处理
  • 【Python】在vscode利用pyinstaller中的.spec文件把py项目打包为.exe实现非py环境下使用的操作步骤
  • 如何通过partclone克隆Ubuntu 22系统