Neo4j基本命令使用
neo4j
- neo4j简介
- 安装
- 可视化管理后台登录
- Cypher
- create
- match
- merge
- create创建关系
- merge创建关系
- where
- delete
- sort命令
- 字符串函数
- toUpper()函数
- toLower()函数
- substring()函数
- replace()函数
- 聚合函数
- count()函数
- max()函数
- min()函数
- sum()函数
- avg()函数
- 索引index
- python 中使用neo4j
neo4j简介
安装
可视化管理后台登录
Cypher
create
CREATE (e:Employee{id:222, name:'Bob', salary:6000, deptnp:12})
match
MATCH (e:Employee) RETURN e.id, e.name, e.salary, e.deptno
merge
MERGE (e:Employee {id:146, name:'Lucer', salary:3500, deptno:16})
create创建关系
CREATE (p1:Profile1)-[r:Buy]->(p2:Profile2)
merge创建关系
MERGE (p1:Profile1)-[r:miss]-(p2:Profile2)
where
MATCH (e:Employee) WHERE e.id=123 RETURN e
delete
delete命令: 删除节点/关系及其关联的属性.
#演示:
# 注意: 删除节点的同时, 也要删除关联的关系边
MATCH (c1:CreditCard)-[r]-(c2:Customer) DELETE c1, r, c2
sort命令
sort命令: Cypher命令中的排序使用的是order by.
# 匹配查询标签Employee, 将所有匹配结果按照id值升序排列后返回结果
MATCH (e:Employee) RETURN e.id, e.name, e.salary, e.deptno ORDER BY e.id
# 如果要按照降序排序, 只需要将ORDER BY e.salary改写为ORDER BY e.salary DESC
MATCH (e:Employee) RETURN e.id, e.name, e.salary, e.deptno ORDER BY e.salary DESC
字符串函数
toUpper()函数
toLower()函数
substring()函数
replace()函数
toUpper()函数
将一个输入字符串转换为大写字母.
MATCH (e:Employee) RETURN e.id, toUpper(e.name), e.salary, e.deptno
toLower()函数
将一个输入字符串转换为小写字母.
MATCH (e:Employee) RETURN e.id, toLower(e.name), e.salary, e.deptno
substring()函数
返回一个子字符串
# 输入字符串为input_str, 返回从索引start_index开始, 到end_index-1结束的子字符串
substring(input_str, start_index, end_index)
# 示例代码, 返回员工名字的前两个字母
MATCH (e:Employee) RETURN e.id, substring(e.name,0,2), e.salary, e.deptno
replace()函数
替换掉子字符串.
# 输入字符串为input_str, 将输入字符串中符合origin_str的部分, 替换成new_str
replace(input_str, origin_str, new_str)
# 示例代码, 将员工名字替换为添加后缀_HelloWorld
MATCH (e:Employee) RETURN e.id, replace(e.name,e.name,e.name + "_HelloWorld"), e.salary, e.deptno
聚合函数
count()函数
max()函数
min()函数
sum()函数
avg()函数
count()函数
count()函数: 返回由match命令匹配成功的条数.
# 返回匹配标签Employee成功的记录个数
MATCH (e:Employee) RETURN count( * )
max()函数
max()函数: 返回由match命令匹配成功的记录中的最大值.
# 返回匹配标签Employee成功的记录中, 最高的工资数字
MATCH (e:Employee) RETURN max(e.salary)
min()函数
min()函数: 返回由match命令匹配成功的记录中的最小值.
# 返回匹配标签Employee成功的记录中, 最低的工资数字
MATCH (e:Employee) RETURN min(e.salary)
sum()函数
sum()函数: 返回由match命令匹配成功的记录中某字段的全部加和值.
# 返回匹配标签Employee成功的记录中, 所有员工工资的和
MATCH (e:Employee) RETURN sum(e.salary)
avg()函数
avg()函数: 返回由match命令匹配成功的记录中某字段的平均值.
# 返回匹配标签Employee成功的记录中, 所有员工工资的平均值
MATCH (e:Employee) RETURN avg(e.salary)
索引index
Neo4j支持在节点或关系属性上的索引, 以提高查询的性能.
可以为具有相同标签名称的所有节点的属性创建索引.
创建索引: 使用create index on来创建索引.
# 创建节点Employee上面属性id的索引
CREATE INDEX ON:Employee(id)
删除索引: 使用drop index on来删除索引.
# 删除节点Employee上面属性id的索引
DROP INDEX ON:Employee(id)
python 中使用neo4j
neo4j-driver简介:
neo4j-driver是一个python中的package, 作为python中neo4j的驱动, 帮助我们在python程序中更好的使用图数据库.
neo4j-driver的安装:
pip install neo4j-driver
from neo4j import GraphDatabase
# 关于neo4j数据库的用户名,密码信息已经配置在同目录下的config.py文件中
from config import NEO4J_CONFIG
driver = GraphDatabase.driver( **NEO4J_CONFIG)
# 直接用python代码形式访问节点Company, 并返回所有节点信息
with driver.session() as session:
cypher = "CREATE(c:Company) SET c.name='Qiyu-company' RETURN c.name"
record = session.run(cypher)
result = list(map(lambda x: x[0], record))
print("result:", result)