启动:neo4j.bat console
CQL 增: 增加一个节点create(n:Person{name:‘我’,age:31})
n:创建的节点名称
Person:节点标签名称
name:节点的属性的名称
value:节点的属性的值
带有关系属性create(p:Person{name:‘我’,age:“31”})-[:包公程{金额:10000}]->(n:Person{name:“好大哥”,age:“35”})
MATCH (e:Customer),(cc:CreditCard)
CREATE (e)-[r:DO_SHOPPING_WITH ]->(cc)
创建带有多个标签的节点
CREATE (m:Movie:Cinema:Film:Picture)
删
create(n:Person{name:“TYD”,age:31})
match(n:Person{name:“TYD”})delete n
删除关系match (p:Person{name:“我”,age:“31”})-[f:包工程]->(n:Person{name:“好大哥”,age:“35”})
delete f
MATCH (cc: CreditCard)-[rel]-(c:Customer)
DELETE cc,c,rel
改
加上标签
match (t:Person) where id(t)=789 set t:好人return t
加上属性
match (a:好人) where id(a)=789 set a.战斗力=200 return a
修改属性
match (a:好人) where id(a)=789 set a.战斗力=500 return a
查(查 *** 作太多啦,直接参考neo4j例子就好)
match (p:Person) - [:包工程] -> (n:Person) return p,n
MATCH (dept:Dept) return dept
MATCH (p:Employee {id:123,name:"Lokesh"}) RETURN p
MATCH (p:Employee) WHERE p.name = "Lokesh" RETURN p
MATCH (video1:YoutubeVideo1)-[movie:ACTION_MOVIES]->(video2:YoutubeVideo2)
RETURN movie
布尔运算符:and,or,not,xor
比较运算符:=,<>(不等于)
MATCH (cust:Customer),(cc:CreditCard)
WHERE cust.id = "1001" AND cc.id= "5001"
CREATE (cust)-[r:DO_SHOPPING_WITH{shopdate:"12/12/2014",price:55000}]->(cc)
RETURN r
快速清空数据库:
MATCH (n)
DETACH DELETE n
remove 永久删除节点的属性MATCH (book { id:122 }) REMOVE book.price RETURN book
删除不需要的标签
MATCH (m:Movie) REMOVE m:Picture
set子句
MATCH (book:Book) SET book.title = ‘superstar’ RETURN book
Sorting排序order by(升序)
MATCH (emp:Employee)
RETURN emp.empid,emp.name,emp.salary,emp.deptno
ORDER BY emp.name
ORDER BY emp.name DESC(降序)
UNION联盟
MATCH (cc:CreditCard) RETURN cc.id,cc.number
UNION
MATCH (dc:DebitCard) RETURN dc.id,dc.number
因为xyk和借记卡有相同的属性名,但有不同的节点名称前缀
"as"子句用于给一些别名
MATCH (cc:CreditCard)
RETURN cc.id as id,cc.number as number,cc.name as name,
cc.valid_from as valid_from,cc.valid_to as valid_to
UNION
MATCH (dc:DebitCard)
RETURN dc.id as id,dc.number as number,dc.name as name,
dc.valid_from as valid_from,dc.valid_to as valid_to
在这里,因为UNION子句过滤它们,可以看到该命令返回9行没有重复的行
而使用union all命令返回10行
LIMIT和SKIPlimit来过滤或限制返回的行数(修剪底部)
skip(修剪顶部)
MATCH (emp:Employee) RETURN emp
LIMIT 2
MATCH (emp:Employee) RETURN emp
SKIP 2
merge合并
merge命令将新的节点添加到数据库,只有当它不存在
MERGE (gp2:GoogleProfile2{ Id: 201402,Name:"Nokia"})
NULL值
将空值视为对节点或关系属性的缺失值或未定义值
MATCH (e:Employee)
WHERE e.id IS NULL
RETURN e.id,e.name,e.sal,e.deptno
IN *** 作符
为CQL命令提供值的集合
MATCH (e:Employee)
WHERE e.id IN [123,124]
RETURN e.id,e.name,e.sal,e.deptno
ID属性
是节点和关系的默认内部属性
字符串函数 upper改为大写字母MATCH (e:Employee)
RETURN e.id,UPPER(e.name),e.sal,e.deptno
LOWER
MATCH (e:Employee)
RETURN e.id,LOWER(e.name),e.sal,e.deptno
substring获取给定string的子字符串
substring(字符串,索引开始,索引结束)
MATCH (e:Employee)
RETURN e.id,SUBSTRING(e.name,0,2),e.sal,e.deptno
aggregation聚合
count(行数)*,节点或关系标签名称或属性名称
MATCH (e:Employee) RETURN COUNT(*)
max,min,sum,avg
MATCH (e:Employee)
RETURN MAX(e.sal),MIN(e.sal),SUM(e.sal),AVG(e.sal)
关系函数
startnode(关系的开始节点),endnote,id(关系的id),type
MATCH (a)-[movie:ACTION_MOVIES]->(b)
RETURN STARTNODE(movie),ENDNODE(movie),ID(movie),TYPE(movie)
索引
CREATE INDEX ON : ()
DROP INDEX ON : ()
CREATE INDEX ON :Customer (name)
DROP INDEX ON :Customer (name)
unique约束
(避免重复记录)
CREATE CONSTRAINT ON (cc:CreditCard)
ASSERT cc.number IS UNIQUE
在CreditCard节点的number属性上创建了一个unique约束
DROP CONSTRAINT ON ()
ASSERT IS UNIQUE
Py2neo
link = Graph("http://localhost:7474", username="neo4j", password="tianzaichen1977")
1.创建节点与关系
from py2neo import Node,Relationship
a=Node("Person",name="Alice")
b=Node("Person",name="Bob")
ab=Relationship(a,"Knows",b)
//对Node或Relationship进行属性赋值
a['age']=20
b['age']=21
r['time']='2017/08/31'
//也可以通过setdefault()方法赋值默认属性
a.setdefault('location','北京')
//也可以用update()方法对属性批量更新
data={
'name':'Amy',
'age':21
}
a.update(data)
- hash(node) 返回node的ID的哈希值
- node[key] 返回node的属性值,没有此属性就返回None
- node[key] = value 设定node的属性值
- del node[key] 删除属性值,如果不存在此属性报KeyError
- len(node) 返回node属性的数量
- dict(node) 返回node所有的属性
- walk(node) 返回一个生成器且只包含一个node
- labels() 返回node的标签的集合
- has_label(label) node是否有这个标签
- add_label(label) 给node添加标签
- remove_label(label) 删除node的标签
- clear_labels() 清楚node的所有标签
- update_labels(labels) 添加多个标签,注labels为可迭代的
其中连接的属性有:
- hash(relationship) 返回一个关系的hash值
- relationship[key] 返回关系的属性值
- relationship[key] = value 设定关系的属性值
- del relationship[key] 删除关系的属性值
- len(relationship) 返回关系的属性值数目
- dict(relationship) 以字典的形式返回关系的所有属性
- walk(relationship) 返回一个生成器包含起始node、关系本身、终止node
- type() 返回关系type
s = a | b | r
print(s)
print(s.nodes())
print(s.relationships())
from py2neo import Node, Relationship, size, order
s = a | b | r
print(s.keys())
print(s.labels())
print(s.nodes())
print(s.relationships())
print(s.types())
print(order(s))
print(size(s))
>>> frozenset({'name'})
>>> frozenset({'Person'})
>>> frozenset({(alice:Person {name:"Alice"}), (bob:Person >>> >>> >>> {name:"Bob"})})
>>> frozenset({(alice)-[:KNOWS]->(bob)})
>>> frozenset({'KNOWS'})
>>> 2
>>> 1
-
并”|“
-
交”&“
-
差”-“
-
对称差”^"
-
keys()返回子图节点和关系所有属性的集合
-
labels()返回节点label的集合
-
nodes()返回所有节点集合
-
relationships()
-
types()返回所有关系的type集合
-
order(subgraph)返回子图节点的数目
-
size(subgraph)返回子图关系的数目
walk()转换为一个生成器包含节点和关系
//将子图合并起来
from py2neo import Node, Relationship
a = Node('Person', name='Alice')
b = Node('Person', name='Bob')
c = Node('Person', name='Mike')
ab = Relationship(a, "KNOWS", b)
ac = Relationship(a, "KNOWS", c)
w = ab + Relationship(b, "LIKES", c) + ac
print(w)
>>> (alice)-[:KNOWS]->(bob)-[:LIKES]->(mike)<-[:KNOWS]-(alice)
//调用walk()方法实现遍历
from py2neo import walk
for item in walk(w):
print(item)
'''
(alice:Person {name:"Alice"})
(alice)-[:KNOWS]->(bob)
(bob:Person {name:"Bob"})
(bob)-[:LIKES]->(mike)
(mike:Person {name:"Mike"})
(alice)-[:KNOWS]->(mike)
(alice:Person {name:"Alice"})
'''
//strat_node(),end_node(),nodes(),relationship()
print(w.start_node())
print(w.end_node())
print(w.nodes())
print(w.relationships())
'''
>>> (alice:Person {name:"Alice"})
>>> (alice:Person {name:"Alice"})
>>> ((alice:Person {name:"Alice"}), (bob:Person {name:"Bob"}), (mike:Person {name:"Mike"}), (alice:Person {name:"Alice"}))
>>> ((alice)-[:KNOWS]->(bob), (bob)-[:LIKES]->(mike), (alice)-[:KNOWS]->(mike))
'''
1.4链接已有图数据库.Graph()
//在database模块中包含了和Neo4j数据交互的API,最重要的是Graph
test_graph = Graph(
"http://localhost:7474",
username="neo4j",
password="xxxx"
)
//利用create()方法传入subgraph对象将关系图,节点,关系添加到数据库中
graph=Graph("http://localhost:7474",username="neo4j",password="x")
graph.create(s)
1.5查询Query
graph.nodes.match()
graph = Graph()
matcher = NodeMatcher(graph)
matcher.match("Person", name="Keanu Reeves").first()
graph.match(nodes=None, r_type=None, limit=None)
//用CQL进行查询,返回list
data1 = test_graph.run('MATCH (a:人) RETURN a')
print(data1, type(data1))
import pandas as pd
print(pd.DataFrame(graph.nodes.match('人')))
print(pd.DataFrame(graph.nodes.match('人', name='小刚')))
print(list(graph.match(r_type="喜欢")))
1.6删
graph.delete(a)
graph.delete_all()#全删
1.7改
a['age'] = 20
graph.push(a)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)