neo4j简介
- 开源 NoSQL 数据库,原生的图数据库,2003 年开始开发,使用 scala和java 语言,2007年开始发布;
- 世界上最先进的图数据库之一,提供原生的图数据存储,检索和处理;
- 采用属性图模型(Property graph model),极大的完善和丰富图数据模型;
- 专属查询语言 Cypher,直观,高效;
图形数据库应用场景:
- 银行欺诈
- 保险欺诈
- 电子商务欺诈
- 工商企业图谱
- 社交网络
Neo4j图数据库中基本元素与概念
- 节点(node):一个实体记录,就像关系型数据库中的一条记录。一个节点包含多个属性和标签。
- 关系(relationship):
- 属性(property):由键值对组成的,就像java当中哈希,
- 标签(lable):一组拥有相同属性的节点,但不强制要求相同,一个节点可以有多个标签。
- 路径(path)
Cypher查询语言
Cypher是一种生命是图数据库查询语言,类似关系数据库中的SQL。
- MATCH:匹配图模式
- WHERE:过滤条件
- RETURN:定义返回的结果
基本语法:
- 增(CREATE)
- 删(DELETE)
- 改(SET)
- 查(MATCH)
- 函数
- WITCH
语法:
<!-- 1. 全表扫描 --> <!-- mysql --> SELECT p.* FROM products as p; <!-- neo4j --> MATCH (p:Product) RETURN p; <!-- 2. 查询价格最贵的10个商品,只返回商品名字和单价 --> <!-- mysql --> SELECT p.ProductName, p.UnitPrice FROM products as p ORDER BY p.UnitPrice DESC LIMIT 10; <!-- neo4j --> MATCH (p:Product) RETURN p.productName, p.unitPrice ORDER BY p.unitPrice DESC LIMIT 10; <!-- 3. 按照商品名字筛选 --> <!-- mysql --> SELECT p.ProductName, p.UnitPrice FROM products AS p WHERE p.ProductName = 'Chocolade'; <!-- neo4j --> MATCH (p:Product) WHERE p.productName = "Chocolade" RETURN p.productName, p.unitPrice; <!-- 其他的写法 --> MATCH (p:Product {productName:"Chocolade"}) RETURN p.productName, p.unitPrice; <!-- 4. 按照商品名字筛选2 --> <!-- mysql --> SELECT p.ProductName, p.UnitPrice FROM products as p WHERE p.ProductName IN ('Chocolade','Chai'); <!-- neo4j --> MATCH (p:Product) WHERE p.productName IN ['Chocolade','Chai'] RETURN p.productName, p.unitPrice; <!-- 5. 模糊查询和数值过滤 --> <!-- mysql --> SELECT p.ProductName, p.UnitPrice FROM products AS p WHERE p.ProductName LIKE 'C%' AND p.UnitPrice > 100; <!-- neo4j --> MATCH (p:Product) WHERE p.productName STARTS WITH "C" AND p.unitPrice > 100 RETURN p.productName, p.unitPrice; <!-- 6. 多表联合查询--> <!-- mysql --> SELECT DISTINCT c.CompanyName FROM customers AS c JOIN orders AS o ON (c.CustomerID = o.CustomerID) JOIN order_details AS od ON (o.OrderID = od.OrderID) JOIN products AS p ON (od.ProductID = p.ProductID) WHERE p.ProductName = 'Chocolade'; <!-- neo4j --> MATCH (p:Product {productName:"Chocolade"})<-[:PRODUCT]-(:Order)<-[:PURCHASED]-(c:Customer) RETURN distinct c.companyName; <!-- 7. 分组topN --> <!-- mysql --> SELECT e.EmployeeID, count(*) AS Count FROM Employee AS e JOIN Order AS o ON (o.EmployeeID = e.EmployeeID) GROUP BY e.EmployeeID ORDER BY Count DESC LIMIT 10; <!-- neo4j --> MATCH (:Order)<-[:SOLD]-(e:Employee) RETURN e.name, count(*) AS cnt ORDER BY cnt DESC LIMIT 10
1优于2
1. MATCH (n) OPTIONAL MATCH (n)-[r]->() RETURN count(n.prop) + count(r.prop);
2. MATCH (n) OPTIONAL MATCH (n)-[r]->() RETURN count(*) + count(*);
//增 create (:pig{name:"猪爷爷",age:15}); create (:pig{name:"猪爷爷",age:13}); //查 match (a:pig{name:"猪奶奶"}) match (b:pig{name:"猪爷爷"}) create (a)-[r:夫妻]->(b) return r; create (:pig{name:"猪爸爸",age:12})-[:夫妻{age:5}]->(:pig{name:"猪妈妈",age:9}); create (:pig{name:"佩奇",age:2})-[:姐弟]->(:pig{name:"乔治",age:1}); match (a:pig{name:"猪爸爸"}) match (b:pig{name:"猪爷爷"}) create (b)-[r:父子]->(a) return r; match (a:pig{name:"猪爸爸"}) match (b:pig{name:"佩奇"}) create (b)-[r:父女]->(a) return r; match (a:pig{name:"猪爷爷"}) match (b:pig{name:"佩奇"}) create (b)-[r:孙女]->(a) return r; match (a:pig{name:"猪奶奶"}) match (b:pig{name:"佩奇"}) create (b)-[r:孙女]->(a) return r; match (a:pig{name:"猪奶奶"}) match (b:pig{name:"猪爸爸"}) create (b)-[r:母女]->(a) return r; //改 match (n:pig{name:"猪妈妈"}) set n.age=8 create (:die:pig{name:"猪祖父",age:20}),(:pig{name:"猪祖奶",age:21}); //删 match (n:die) delete n; match p=allshortestpath((:Star{name:"小明"})-[*..5]->(:Star{name:"王大拿"})) return p; match (:Star{name:"王大拿"})->() limit 50;
csv数据导入
LOAD CSV WITH HEADERS FROM ’file:///data/locations.csv’ AS line WITH split(line.locations,";") as locations, line.title as title UNWIND locations AS location --类似for location in locations MERGE (x:Location {name:location}) MERGE (m:Movie {title:title}) MERGE (m)-[:FILMED_IN]->(x)
neo4j下载及安装
Download Neo4j 安装在下载成功跳转页下拉内容中有写,需复制“Activation Key”中激活码进行激活
NEO4J安装教程 windows下安装neo4j报错解决 jdk下载地址
Invoke-Neo4j : Cannot validate argument on parameter 'ArgumentList'. The argument is null, empty, or an element of the argument collection contains a null value. Supply a collection that does not contain any null values and then try the command again.
NEO4J_HOME = D: eo4j-community-4.2.4
path > ;%NEO4J_HOME%in;
(1)neo4j browser
(2)neo4j bloom
python 连接 neo4j
# pip3 install neo4j-driver # python3 example.py from neo4j import GraphDatabase, basic_auth driver = GraphDatabase.driver( "bolt://<HOST>:<BOLTPORT>", auth=basic_auth("<USERNAME>", "<PASSWORD>")) cypher_query = ''' MATCH (m:Movie {title:$movie})<-[:RATED]-(u:User)-[:RATED]->(rec:Movie) RETURN distinct rec.title AS recommendation LIMIT 20 ''' with driver.session(database="neo4j") as session: results = session.read_transaction( lambda tx: tx.run(cypher_query, movie="Crimson Tide").data()) for record in results: print(record['recommendation']) driver.close()
参考资料:
NoSQL 新贵之崛起的 Neo4j 受益匪浅
知识图谱和 Neo4j 浅析 知识图谱使用场景及neo4j性能方面的介绍