发布于 5 个月前 作者 wkq278276130 1004 次浏览 最后一次编辑是 11 天前 来自 分享

1. Neo4j介绍

Neo4j是一种NoSQL数据库,而且属于NoSQL数据库里的图数据库。 常见的SQL数据库有MySQL Oracle等 常见的NoSQL数据库有Redis ES Mongdb Neo4j等 近几年比较流行的的图数据库有Neo4j Titan OrientDB Sparksee Virtuso ArangoDb Airaph GraphDB GraphBase等

Neo4j数据库比较适合处理关系,比如人和人之间的关系。 比较成功的应用有 领英 FaceBooke Twitter

2. Neo4j下载、安装、配置

neo4j分为企业版和社区版,社区版免费,企业版收费。 社区版企业版对比

2.1 下载

linux unix 推荐下载.tar.gz包 windows推荐下载.zip包,第一,和linux上目录及文件一样;第二,使用.exe安装的用户经常找不见配置,load csv以及其他操作时经常找不见目录

下载地址: Neo4j所有版本的下载地址 Linux Mac版 Windows版

2.2 安装

  1. 下载的压缩包,解压完就可以用。
  2. 解压完进入bin目录,输入neo4j console就可以看到neo4j数据库启动了,可以在浏览器里输入http://localhost:7474来访问数据库,默认用户名、密码: neo4j/neo4j
  3. 如果想自定义配置,可以在${NEO4J_HOME}/conf/neo4j.conf修改对应配置


1 Open up your terminal/shell.  
2 Extract the contents of the archive, using: tar -xf <filecode>. 
  For example,tar -xf neo4j-community-3.1.2-unix.tar.gz  
  the top level directory is referred to as NEO4J_HOME
  $NEO4J_HOME指安装目录,在neo4j安装目录的bin目录使用neo4j console启动neo4j数据库
3 Run Neo4j using, $NEO4J_HOME/bin/neo4j console  
  Instead of 'neo4j console', you can use neo4j start to start the server process in the background.
4 Visit http://localhost:7474 in your web browser. 
5 Change the password for the 'neo4j' account. 

linux用户注意: 使用超级用户修改/etc/security/limits.conf文件,允许当前用户(neo4j)打开40000个文件

neo4j   soft    nofile  40000
neo4j   hard    nofile  40000

2.3 配置




export NEO4J_HOME=/usr/neo4j/neo4j-community-3.1.2  

./${NEO4J_HOME}/bin/neo4j start  
neo4j stop 

windows 使用neo4j console来启动数据库

nohup ./bin/neo4j console  = neo4j start
要结束该命令时,用 kill -9 进程号 来关闭该进程


vim /etc/rc.d/rc.local
/usr/share/neo4j/bin/neo4j start

3. Neo4j使用

A row is a node A table name is a label name

Properties  Both nodes and relationships can have properties.  Properties are named values where the name is a string. The supported property values are:  • Numeric values,  • String values,  • Boolean values,  • Collections of any other type of value.

Labels have an id space of an int, meaning the maximum number of labels the database can contain is roughly 2 billion.

Paths A path is one or more nodes with connecting relationships, typically retrieved as a query or traversal result

Neo4j is a schema-optional graph database

You can use Neo4j without any schema. Optionally you can introduce it in order to gain performance or modeling benefits. This allows a way of working where the schema does not get in your way until you are at a stage where you want to reap the benefits of having one.

Indexs Performance is gained by creating indexes, which improve the speed of looking up nodes in the database.


:help  帮助页面
:schema  查看数据库结构
:schema ls -l :Person

:server change-password  // 修改密码
CALL dbms.changePassword("newpassword")  // (旧版本)修改密码

:server connect  连接

:play sysinfo 查看系统信息

// List node labels  查询所有的label
CALL db.labels()

// List relationship types  查询所有的type
CALL db.relationshipTypes()

// What is related, and how  查询数据里的节点和关系 类似于 SQL的desc
CALL db.schema()

// List functions
CALL dbms.functions()

// List procedures
CALL dbms.procedures()

CALL dbms.listQueries() ;

CALL dbms.killQuery(queryId);

// 查询一共有多少节点
// Count all nodes
match (n) RETURN count(n)

// 查询一共有多少关系  // 不带方向的话结果是2倍
// Count all relationships
match ()-->() RETURN count(*);
match ()-[r]->() return count(r); 

match (a:Person), (b:Person) where = 'zhangsan' and = 'lisi'
merge (a)-[r:RELTYPE]->(b) return r
// 模糊匹配
match (n:Person) where =~ '张.*' return n

// 包含
match (n:Person) where contains '张' return n;

// 去重
match (n:Person) with as name  return distinct name;

// Count all nodes  查询一共有多少节点
match (n) RETURN count(n)

// Count all relationships 查询一共有多少关系
match ()-->() RETURN count(*);

// 查询一共有多少种节点
call db.labels();
match (n) return distinct label(n);

// 查询一共有多少关系
call db.relationshipTypes()

// 查询数据库里的所有属性
match (n) unwind keys(n) as allkeys  return distinct allkeys;
// 查询关系最多的节点
// 实际使用时,最好对n加个范围,要不然 全图扫描
// 使用with 和 别名,能减少一次count(*)的查询
match (n:Movie)--() with n.title as title, count(*) as count return title, count order by count desc limit 1;
match (n:Movie)-[r]-() with n.tile as title, count(r) as count return title, count order by count desc limit 1; 

// 查询孤立节点
match (n) where not (n)--() return id(n);


参考 [1] [2] [3] [4] [5] [6] [7] [8]
[9] [10] 使用ETL方式导入Neo4j [11] 使用csv文件方式导入Neo4j


1 回复