图数据库Neo4j安装笔记

时间:2023-02-03 21:02:26

     最近加入一个新项目,需要用到一个第一次接触的技术——图数据库Neo4j。这两天也一直在学习这门技术。记得我的一个老师说过,要学习一门技术,首先要动手去安装它。接触的东西多了,觉得这句“废话”却也是很有一番朴实的哲理。对Neo4j的学习,也就从安装开始吧。查看官网资料,就当前的社区版而言,倒也不是很麻烦,Linux系统上面手动添加一下源,就可以用超级命令安装了。

     以Ubuntu为例,参考http://debian.neo4j.org/?_ga=1.231636319.1336410407.1492272243,简单执行以下命令即可。

wget -O - https://debian.neo4j.org/neotechnology.gpg.key | sudo apt-key add -
echo 'deb https://debian.neo4j.org/repo stable/' | sudo tee /etc/apt/sources.list.d/neo4j.list
sudo apt-get update
sudo apt-get install neo4j

     CentOS的话参考http://yum.neo4j.org/stable/?_ga=1.233707870.1336410407.1492272243,就不再额外摘录安装命令出来了。

     我是用Python读写Neo4j,所以在安装好Neo4j后,就再装一个其对于Python的接口驱动。安装也很简单,直接pip即可。

pip install neo4j-driver

     安装好以后,在控制台启动Neo4j,然后打开浏览器输入访问 http://localhost:7474/ 就可以登录查看本地Neo4j的运行了,登录的时候需要修改初始密码。

neo4j --help
neo4j console

     参考http://neo4j.com/docs/api/python-driver/current/,运行一段Python测试代码。Neo4j支持一种叫做cypher的图查询语言,这里定义一个执行cypher的函数,然后调用插入一些测试数据。Python读写Neo4j参考http://blog.csdn.net/sweeper_freedoman/article/details/70231073

# !/usr/bin/python
# -*- coding: utf-8 -*-


from neo4j.v1 import GraphDatabase

uri = "bolt://localhost:7687"
driver = GraphDatabase.driver(uri, auth=("neo4j", "520"))

def cyphertx(cypher):
with driver.session() as session:
with session.begin_transaction() as tx:
tx.run(cypher)

cypher = """
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)
""" # "cypher from http://console.neo4j.org/"
cyphertx(cypher)

      写入成功后马上在后台就可以看到结果了。

图数据库Neo4j安装笔记