Python读写图数据库Neo4j
来源:互联网 发布:中国科技数据统计 编辑:程序博客网 时间:2024/06/07 00:39
安装好图数据库Neo4j和相应的Python库neo4j-driver后( http://blog.csdn.net/sweeper_freedoman/article/details/70189153 ),就正式开始读写Neo4j了。
# !/usr/bin/python# -*- coding: utf-8 -*-"""create_author : 蛙鳜鸡鹳狸猿create_time : 2017-04-18program : *_* Read and Write Neo4j *_*"""from neo4j.v1 import GraphDatabaseclass Neo4jHandler: """ Handler of graph database Neo4j reading and writing. """ def __init__(self, driver): """ Get Neo4j server driver. :param driver: driver object A driver object holds the detail of a Neo4j database including server URIs, credentials and other configuration, see " http://neo4j.com/docs/api/python-driver/current/driver.html ". """ self.driver = driver def __repr__(self): printer = 'o(>﹏<)o ......Neo4j old driver "{0}" carry me fly...... o(^o^)o'.format(self.driver) return printer def listreader(self, cypher, keys): """ Read data from Neo4j in specified cypher. Read and parse data straightly from cypher field result. :param cypher: string Valid query cypher statement. :param keys: list Cypher query columns to return. :return: list Each returned record constructs a list and stored in a big list, [[...], [...], ...]. """ with self.driver.session() as session: with session.begin_transaction() as tx: data = [] result = tx.run(cypher) for record in result: rows = [] for key in keys: rows.append(record[key]) data.append(rows) return data def dictreader(self, cypher): """ Read data from Neo4j in specified cypher. The function depends on constructing dict method of dict(key = value) and any error may occur if the "key" is invalid to Python. you can choose function dictreaderopted() below to read data by hand(via the args "keys"). :param cypher: string Valid query cypher statement. :return: list Each returned record constructs a dict in "key : value" pairs and stored in a big list, [{...}, {...}, ...]. """ with self.driver.session() as session: with session.begin_transaction() as tx: data = [] for record in tx.run(cypher).records(): item = {} for args in str(record).split('>')[0].split()[1:]: exec "item.update(dict({0}))".format(args) data.append(item) return data def dictreaderopted(self, cypher, keys=None): """ Optimized function of dictreader(). Read and parse data straightly from cypher field result. :param cypher: string Valid query cypher statement. :param keys: list, default : none(call dictreader()) Cypher query columns to return. :return: list. Each returned record constructs an dict in "key : value" pairs and stored in a list, [{...}, {...}, ...]. """ if not keys: return self.dictreader(cypher) else: with self.driver.session() as session: with session.begin_transaction() as tx: data = [] result = tx.run(cypher) for record in result: item = {} for key in keys: item.update({key : record[key]}) data.append(item) return data def cypherexecuter(self, cypher): """ Execute manipulation into Neo4j in specified cypher. :param cypher: string Valid handle cypher statement. :return: none. """ with self.driver.session() as session: with session.begin_transaction() as tx: tx.run(cypher) session.close()# self testif __name__ == "__main__": uri = "bolt://localhost:7687" driver = GraphDatabase.driver(uri, auth=("neo4j", "520")) MyNH = Neo4jHandler(driver) print(MyNH) cypher_exec = """ CREATE (Neo:Crew {name:'Neo'}), (Morpheus:Crew {name: 'Morpheus'}), (Trinity:Crew {name: 'Trinity'}), (Cypher:Crew:Matrix {name: 'Cypher'}), (Smith:Matrix {name: 'Agent Smith'}), (Architect:Matrix {name:'The Architect'}), (Neo)-[:KNOWS]->(Morpheus), (Neo)-[:LOVES]->(Trinity), (Morpheus)-[:KNOWS]->(Trinity), (Morpheus)-[:KNOWS]->(Cypher), (Cypher)-[:KNOWS]->(Smith), (Smith)-[:CODED_BY]->(Architect) """ # "example cypher statement from http://console.neo4j.org/" cypher_read = """ MATCH (a) -[:KNOWS|LOVES]-> (b:Crew {name: 'Trinity'}) RETURN a.name AS l, b.name AS r """ MyNH.cypherexecuter(cypher_exec) print(MyNH.listreader(cypher_read, ['l', 'r'])) print(MyNH.dictreader(cypher_read)) print(MyNH.dictreaderopted(cypher_read, ['l']))
0 0
- Python读写图数据库Neo4j
- 图数据库 Neo4j 入门
- 图数据库Neo4J
- 图数据库neo4j初探
- 图数据库Neo4j介绍
- 图数据库NEO4j
- 图数据库Neo4j使用
- Neo4j图数据库
- 图数据库Neo4j入门
- neo4j图数据库入门
- Neo4j图数据库初识
- neo4j 图数据库
- Neo4j实战-使用python语言操作neo4j数据库
- 图数据库Neo4J的使用
- 图数据库Neo4J的使用
- 图数据库Neo4j安装笔记
- 图数据库初探之Neo4j
- neo4j图数据库--Cypher入门
- Java序列化
- 字符串的包含
- mysql一些规范
- 关于谷歌浏览器显示隐私设置错误的解决方案
- 【11】Redis .net 实例 StackExchange.Redis框架
- Python读写图数据库Neo4j
- Ubuntu14.04运行ORB_SLAM2总结
- MFC:MFC下使用__argc,__argv
- Deeplearning 博客和论文资源
- window.postMessage跨页面通信
- 自动化测试问题之一
- Nginx slab的实现 --- 第三篇“基于块的内存分配”
- equals 的用法
- 使用 arguments