最近加入一个新项目,需要用到一个第一次接触的技术——图数据库Neo4j。这两天也一直在学习这门技术。记得我的一个老师说过,要学习一门技术,首先要动手去安装它。接触的东西多了,觉得这句“废话”却也是很有一番朴实的哲理。对Neo4j的学习,也就从安装开始吧。查看官网资料,就当前的社区版而言,倒也不是很麻烦,Linux系统上面手动添加一下源,就可以用超级命令安装了。
以Ubuntu为例,参考:
简单执行以下命令即可。
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的话参考:
Neo4j Stable Yum Repo
First, you'll want our key:
cd /tmp
wget http://debian.neo4j.org/neotechnology.gpg.key
rpm --import neotechnology.gpg.key
Then, you'll want to add our yum repo to /etc/yum.repos.d/neo4j.repo:
cat <<EOF> /etc/yum.repos.d/neo4j.repo
[neo4j]
name=Neo4j Yum Repo
baseurl=http://yum.neo4j.org/stable
enabled=1
gpgcheck=1
EOF
Finally, install a package
yum install neo4j
我是用Python读写Neo4j,所以在安装好Neo4j后,就再装一个其对于Python的接口驱动。安装也很简单,直接pip即可。
pip install neo4j-driver
安装好以后,在控制台启动,然后打开浏览器输入访问 http://localhost:7474/ 就可以登录查看本地Neo4j的运行了,登录的时候需要修改初始密码。
neo4j --help
neo4j console
参考:
Installation
To install the latest stable version, use:
pip install neo4j-driver
For the most up-to-date version (possibly unstable), use:
pip install git+https://github.com/neo4j/neo4j-python-driver.git#egg=neo4j-driver
运行一段Python测试代码。Neo4j支持一种叫做cypher的图查询语言,这里定义一个执行cypher的函数,然后调用插入一些测试数据。
# !/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 <a target="_blank" href="http://console.neo4j.org/">http://console.neo4j.org/</a>"
cyphertx(cypher)
写入成功后马上在后台就可以看到结果了。