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

相关推荐
TF男孩7 小时前
ARQ:一款低成本的消息队列,实现每秒万级吞吐
后端·python·消息队列
该用户已不存在12 小时前
Mojo vs Python vs Rust: 2025年搞AI,该学哪个?
后端·python·rust
站大爷IP14 小时前
Java调用Python的5种实用方案:从简单到进阶的全场景解析
python
用户83562907805119 小时前
从手动编辑到代码生成:Python 助你高效创建 Word 文档
后端·python
侃侃_天下19 小时前
最终的信号类
开发语言·c++·算法
c8i19 小时前
python中类的基本结构、特殊属性于MRO理解
python
echoarts20 小时前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
liwulin050620 小时前
【ESP32-CAM】HELLO WORLD
python
Aomnitrix20 小时前
知识管理新范式——cpolar+Wiki.js打造企业级分布式知识库
开发语言·javascript·分布式
Doris_202320 小时前
Python条件判断语句 if、elif 、else
前端·后端·python