精华 neo4j笔记
发布于 7 年前 作者 wkq278276130 10029 次浏览 最后一次编辑是 5 年前 来自 分享

neo4j笔记

转自 http://weikeqin.cn/2017/03/17/neo4j-notes/

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所有版本的下载地址

https://neo4j.com/download-thanks/?edition=community&release=3.1.2&flavour=unix Linux Mac版  https://neo4j.com/download-thanks/?edition=community&release=3.1.2&flavour=winzip Windows版

2.2 安装

  1. 下载的压缩包neo4j-community-3.1.2.zip,解压完就可以用。
  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.
  在浏览器里输入http://localhost:7474即可访问数据库
4 Visit http://localhost:7474 in your web browser. 
  第一次登录数据库默认用户名密码是neo4j/neo4j,第一次登录需要修改密码
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 配置

常用配置

#设置可以通过ip当问Neo4j数据库
dbms.connectors.default_listen_address=0.0.0.0  

#历史版本请修改 
dbms.connector.http.address=0.0.0.0:7474
org.neo4j.server.webserver.address=0.0.0.0 

#(可以不配置)
export NEO4J_HOME=/usr/neo4j/neo4j-community-3.1.2  

#启动Neo4j数据库
./${NEO4J_HOME}/bin/neo4j start  
#停止数据库
neo4j stop 

windows 使用neo4j console来启动数据库

使用nohup命令可以使neo4j数据库一直运行
nohup ./bin/neo4j console  = neo4j start
要结束该命令时,用 kill -9 进程号 来关闭该进程

设置neo4j开机启动

vim /etc/rc.d/rc.local
在文件最后添加如下命令行:
/usr/share/neo4j/bin/neo4j start
其中/usr/share/neo4j/bin/是安装Neo4j的路径

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.

Cypher

: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 a.name = 'zhangsan' and b.name = 'lisi'
merge (a)-[r:RELTYPE]->(b) return r
// 模糊匹配
match (n:Person) where n.name =~ '张.*' return n

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

// 去重
match (n:Person) with n.name 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);

github例子

https://github.com/neo4j-examples/movies-java-spring-data-neo4j-4

参考 [1] http://neo4j.com/docs/operations-manual/3.1/ [2] https://neo4j.com/docs/developer-manual/3.1/ [3] http://neo4j.com/docs/2.2.9/query-delete.html [4] https://neo4j.com/docs/developer-manual/3.1/cypher/ [5] https://neo4j.com/blog/neo4j-3-1-ga-release/?ref=home [6] https://neo4j.com/docs/developer-manual/3.1/cypher/clauses/set/ [7] https://neo4j.com/docs/operations-manual/3.2/installation/linux/debian/#multiple-java-versions [8] https://neo4j.com/docs/operations-manual/current/installation/windows/
[9] http://neo4j.com/docs/developer-manual/current/extending-neo4j/procedures/ [10] https://neo4j.com/developer/guide-importing-data-and-etl/ 使用ETL方式导入Neo4j [11] https://neo4j.com/developer/guide-import-csv/ 使用csv文件方式导入Neo4j

本文转自 http://weikeqin.cn/2017/03/17/neo4j-notes/

1 回复
回到顶部