neo4j语法
图数据库在社交网络、实时推荐、征信系统、人工智能等领域有广泛应用。
集群特征:主从复制,重选主服务器和容错;每个实例都有自己的本地缓冲
性能优势:查询内不跨网络;实时操作,具有快速和一致的响应时间;缓冲分区,对于非常大的图,跨集群扩展缓冲。
1. 安装
安装现在是zip文件,解压后要配置环境变量,管理员运行cmd后执行:neo4j install-service,管理员运行cmd后执行:neo4j start,就可以进入浏览器7474界面。
可以修改config,修改里面内存大小dbms.memory.heap.initial_size=1024m
dbms.memory.heap.initial_size=1024m
如果想实现远程访问,首先关闭Windows防火墙,在config中修改监听地址为 0.0.0.0。
# To accept non-local connections, uncomment this line:
dbms.connectors.default_listen_address=0.0.0.0
2. 基本概念
标签Label: 相当于数据表,比如Person
节点Node: 每个标签下可以有N个节点Node,每个节点代表一个对象,相当于数据表里的一行。
关系Relation: 几点之间的连线代表对象之间的关系。
节点和关系都可以带若干属性。
3. cypher语法
①创建节点
Create (erzi:Person {id:'erzi'}),(baba:Person{id:'baba'}),//erzi别名
(yeye:Person{id:'yeye', name:'zhangsan'}),
(nainai:Person{id:'nainai'}),(mama:Person{id:'mama'}),
(bozi:Person{id:'hozi'}),
(erzi)-[:father]->(baba), (baba)-[:father]->(yeye),
(baba)-[:mother]->(nainai), (erzi)-[:mother]->(mama),
(erzi)-[:girlFriend]->(bozi)
②Match查找
Match (n:Person {id:’erzi’}) Return n limit 25
或者Match (n:Person) where n.id=’erzi’ Return n limit 25
Match (n:Person) where n.id=’erzi’ Return n.name, n.id limit 25
③Merge 创建或者查询(已经存在)
//如何给已经存在的节点添加新关系,并删除旧关系
Match (n:Person {id:’erzi’}) , (f:Person {id:’bozi’})
Merge (n)-[r:fuqi]->(f) delete r
Merge (n)-[r:FUQI]->(f)
Return n, f
④更新Set
Cypher无Update,用set代替
//更新属性
Match (n:Person{id:’baba’}) set n.name=’张三’, n.age=60 return n
⑤delete和remove
删之前最好先return 害怕一不小心删错了
Delete操作用于删除节点和关联关系
Remove 操作用于删除标签和属性
这两个命令都应该与MATCH命令一起使用
Match (n:Person{id:’baba’}) remove n.age return n
Match p=()-[r:teach]->() delete p //把所有teach的关系都删除
Match (a:Person), (b:Person) where a.id=’erzi’ and b.id=’bozi’
Merge (a)-[r:FUQI]->b
Delete r
Match (s:Teacher)-[r:teach] ->(d:Student) delete r,s,d //把关系和节点都删掉
Match (n:Test) remove n:Test//删除label
Match (n{name:’Andre’}) DETACH DELTET n
⑥order by
Match (n:Person) Return n order by n.id,n.name desc skip 2 LIMIT 25
⑦Union和Union all
Union:把多段Match的return结果拼成一个结果集。会自动去重
Union all:作用同Union
Match (n:Person) where n.age >30 Return n.id, n.age order by n.id,n.age union all Match (n:Person) where n.id=’erzi’ and n.age is not null return n.id, n.age
Where 属性 is null
is not null
⑧in
Match (n:Person) where n.id in [‘erzi’,’bozi’,’baba’] and n.age is not null return n.id, n.age
⑨内置id
每个relation都有个系统分配的id,从0开始递增,全局唯一。一般不会用内置的。通过函数id(node/relation)可以获取id
用户可以自定义id属性,与内置id无关。
Relation具有方向性,create节点之间关系时,必须指定方向,否则会报错,但是查询时,可以不指定,不指定他就会把双向的方向查出来。
Match(n:Person)-[:FUQI]-(s:Person) Return distinct n,s
⑩索引index
Create index on: Person(id);
Drop index on: Person(id);
不需要给索引起名称,只需要设置索引字段即可
给哪些字段建索引呢?根据查询需要,把查询多的字段建索引。
⑾属性唯一性约束
Create constrain on (a:Person) ASSERT a.id IS UNIQUE
Drop constrain on (a:Person) ASSERT a.id IS UNIQUE
⑿where
找到charlie和martin的最短路径,并且关系里面不存在father类型的关系。
MATCH (charlie:Person { name: 'Charlie Sheen' }),(martin:Person { name: 'Martin Sheen' }),
p =shortestPath((charlie)-[*]-(martin))
WHERE NONE (r IN relationships(p) WHERE type(r)= 'FATHER')
RETURN p
⑿常用函数shortestPath、allShortestPaths
Upper\lower\substring\replace
count\max\min\sum\avg 返回由Match命令返回的所有行的平均值等。。
neo4j无group by
Match (n:Person) return avg(n.age) ///只包含age不为空的节点
Match (n:Person) return Substring(n.id,1,3), n.id
Match p=shortestPath((n:Person{id=’mama’})-[*..3]-(b:Person{id=’nainai})) return p
Match p=allshortestPaths((n:Person{id=’mama’})-[*..3]-(b:Person{id=’nainai})) return p
关系链路越短,代表这两个节点的关系越密切。
⒀with
Match (user)-[:friend]-(friend)
Where user.name=$name
With user, count(friend) As friends
Where friends>10
Return user
4. 本地csv文件导入
把你的数据放在import文件夹。
LOAD CSV WITH HEADERS FROM "file:///高管节点.csv" AS row
CREATE (:高管{ggname:row.ggname,gs:row.gs,url:row.url,phone:row.phone,email:row.email,address:row.address})
LOAD CSV WITH HEADERS FROM "file:///资金.csv" AS row merge (n1:企业{名字:row.source}) with * merge (n2:企业{名字:row.target}) with * create (n1)-[i:借款]->(n2) set r.shaftWidth=row.weight
#海量数据加载
create constraint on (p:F_CERT) assert p.CASE_ID is unique; call apoc.periodic.iterate( "call apoc.load.jdbc('jdbc:oracle:thin:{username}/{password}@{hostip}:{port}:{servicename or sid}',\"select case_id,proportion from invest where start_type='2' and end_type='2'\")",
"merge (n1:F_CERT{CASE_ID:row.START_ID}) with * merge (n2:F_CERT{CASE_ID:row.END_ID}) with * create (n1)-[i:INVEST]->(n2) set i+=row",
{batchSize:100,iterateList:true})
注意事项:
1、节点里“主键”应建索引,唯一“主键”应建“约束”;2、merge时()中只出现“主键”,其余属性用set字句设定。
4. 踩过的坑
①报错:CSV import : Cannot merge node using null property value
数据里面出现了空值,去掉空值即可
②本来想批量导入关系,发现不行,必须分不同的关系分别导入
neo4j语法的更多相关文章
- spring-data-neo4j了解
本项目demo地址[请阅读readme文件]: https://gitee.com/LiuDaiHua/project-neo4j 最近项目上要搭建一个关系图谱的东西,领导给了neo4j和d3两个概念 ...
- Neo4j Cypher语法(三)
目录 5 函数 5.1 谓词函数 5.2 标量函数 5.3 聚合函数 5.4 列表函数 5.5 数学函数 5.6 字符串函数 5.7 Udf与用户自定义函数 6 模式 6.1 索引 6.2 限制 7 ...
- Neo4j Cypher语法(二)
目录 4 子句 4.1 CREATE 4.2 MATCH 4.3 Match 4.4 Create match return连用来返回一个关系基础 4.5 Optional_match 4.6 Wit ...
- Neo4j Cypher语法(一)
目录 Cypher手册详解 1 背景 2 唯一性 3 语法 3.1 命名规则 3.2 表达式 3.3 变量与保留关键字 3.4 参数 3.5 操作符 3.6 模式 3.7 列表 Cypher手册详解 ...
- neo4j 基本语法笔记(全)
按照总监要求看了两天的neo4j 数据库的使用.在网上找了一个基础教程类似于w3c.school的网站(英文 ,中文,中文的翻译的不是很好,如果英文不好可以辅助理解),这个教程基础知识很全全面,从数据 ...
- Neo4j的查询语法笔记(二)
cypher是neo4j官网提供的声明式查询语言,非常强大,用它可以完成任意的图谱里面的查询过滤,我们知识图谱的一期项目 基本开发完毕,后面会陆续总结学习一下neo4j相关的知识.今天接着上篇文章来看 ...
- 记录Neo4j上写的简单cypher语法
neo4j是一个高性能的图形数据库,既然是数据库,那么主要操作就是增.删.改.查.所以进入正题: 一.CREATE:创建 语法如下: 1.create(变量名:标签名) :建立一个标签为Animal的 ...
- Neo4j数据和Cypher查询语法笔记
Cypher数据结构 Cypher的数据结构: 属性类型, 复合类型和结构类型 属性类型 属性类型 Integer Float String: 'Hello', "World" B ...
- Neo4j 第六篇:Cypher语法
Cypher是图形数据库查询语言事实上的标准. 一,Cypher类型系统 Cypher支持的类型系统分为三类:属性类型,复合类型和结构类型. 1,属性类型 属性类型:Integer.Float.Str ...
随机推荐
- MySQL replace into (insert into 的增强版)
在使用SQL语句进行数据表插入insert操作时,如果表中定义了主键,插入具有相同主键的记录会报错: Error Code: 1062. Duplicate entry 'XXXXX' for ke ...
- 查询本地电脑IP地址
使用Windows+R键打开"运行"窗口,然后输入CMD进入命令提示窗口 进入命令窗口之后,输入:ipconfig/all 回车即可看到整个电脑的详细的IP配置信息
- ZHS16GBK的数据库导入到字符集为AL32UTF8的数据库
字符集为ZHS16GBK的数据库导入到字符集为AL32UTF8的数据库 相信大家都对字符集有相当的了解了,废话就不多说了!直接步入正题:这里主要是测试含有 汉字的数据从ZHS16GBK的数据库导入到 ...
- AtCoder Grand Contest 032-B - Balanced Neighbors (构造)
Time Limit: 2 sec / Memory Limit: 1024 MB Score : 700700 points Problem Statement You are given an i ...
- Education CodeForces Round 63 Div.2
A. Reverse a Substring 代码: #include <bits/stdc++.h> using namespace std; int N; string s; int ...
- 001 java简介
JavaSE(Java Standard Edition):标准版本,定位在个人计算机上的应用.(失败) JavaEE(Java Enterprise Edition):企业版,定位在服务器端的应用. ...
- icpc 南昌邀请赛网络赛 Max answer
就是求区间和与区间最小值的积的最大值 但是a[i]可能是负的 这就很坑 赛后看了好多dalao的博客 终于a了 这个问题我感觉可以分为两个步骤 第一步是对于每个元素 以它为最小值的最大区间是什么 第二 ...
- jsp使用
session.setAttribute("sessionName",Object); 用来设置session值的,sessionName是名称,object是你要保存的对象. s ...
- 【长期更新】迈向现代化的 .Net 配置指北
1. 欢呼 .NET Standard 时代 我现在已不大提 .Net Core,对于我来说,未来的开发将是基于 .NET Standard,不仅仅是 面向未来 ,也是 面向过去:不只是 .Net C ...
- 【XSY3320】string AC自动机 哈希 点分治
题目大意 给一棵树,每条边上有一个字符,求有多少对 \((x,y)(x<y)\),满足 \(x\) 到 \(y\) 路径上的边上的字符按顺序组成的字符串为回文串. \(1\leq n\leq 5 ...