持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第17天,点击查看活动详情
前言
昨天文记录中,可以通过远程方式访问Neo4J的前端以及前端的基本操作,简而言之除了需要映射前端所需的7474端口,还需要映射后端所需的7687端口,映射没问题方可远程登录到数据库,用户名密码初始化均为neo4j,而第一次登录需要修改登录密码。登录之后可以进行数据库的增加create、查询match、删除delete等操作,创建节点后就可以看到节点与节点的关系等信息。而今天将使用python对neo4j进行基本操作,并选择之前的漏洞数据进行大批量数据插入、查询等测试。
python安装py2neo
使用pip安装py2neo
pip install py2neo
python连接neo4j数据库
from py2neo import Graph
class connect2Neo4J():
def __init__(self):
self.userName = "neo4j"
self.password = "#你的密码"
self.neo4jUrl = 'http://127.0.0.1:17474'
self.conn = Graph(self.neo4jUrl, auth=(self.userName, self.password))
连接测试一下下:
from connectUtils import connect2Neo4J
def print_hi(name):
print(f'Hi, {name}')
if __name__ == '__main__':
conn = connect2Neo4J().conn
print_hi('PyCharm')
结果:
没有报错,表示连接成功。
python创建节点
def creatNode(self, labels,properties):
insertNode = Node(labels,name = properties)
self.conn.create(insertNode)
return insertNode
python创建关系
def creatRelationship(self,nodeA,nodeB,properties):
relation = Relationship(nodeA, properties, nodeB)
self.conn.create(relation)
创建功能测试一下下
from connectUtils import connect2Neo4J
def print_hi(name):
print(f'Hi, {name}')
if __name__ == '__main__':
connect = connect2Neo4J()
nodeA = connect.creatNode("IpAddr", "2.2.2.10")
nodeB = connect.creatNode("IpAddr", "2.2.2.11")
connect.creatRelationship(nodeA, nodeB, "关联")
print_hi('PyCharm')
节点创建成功:
python查询节点
def searchNode(self, labels, limit=None):
if limit:
data = NodeMatch(self.conn, labels=frozenset({'{}'.format(labels)})).limit(limit)
else:
node_matcher = NodeMatcher(self.conn)
data = node_matcher.match(labels)
return data
查询节点中,使用了py2neo两个原生的查询方法,一个是NodeMatch,而另一个则是NodeMatcher,两个的方法略有不同:
其中而另一个则是NodeMatcher有以下方法
| 方法 | 说明 |
|---|---|
| first() | 返回查询结果第一个Node,没有则返回空 |
| all() | 返回所有节点 |
| where(condition,properties) | 对查询结果二次过滤 |
| order_by | 排序 |
而NodeMatch则有更多的方法:
| 方法 | 说明 |
|---|---|
| iter(match) | 遍历所匹配节点 |
| len(match) | 返回匹配到的节点个数 |
| all() | 返回所有节点 |
| count() | 返回节点计数,评估所选择的节点 |
| limit(amount) | 返回节点的最大个数 |
| order_by(*fields) | 按指定的字段或字段表达式排序。要引用字段或字段表达式中的当前节点,请使用下划线字符 |
| where(predicates, properties) | 二次过滤 |
因此使用上结合两者,最终输出都转为list格式。
测试一下下:
from connectUtils import connect2Neo4J
def print_hi(name):
print(f'Hi, {name}')
if __name__ == '__main__':
connect = connect2Neo4J()
# nodeA = connect.creatNode("IpAddr", "2.2.2.10")
# nodeB = connect.creatNode("IpAddr", "2.2.2.11")
# connect.creatRelationship(nodeA, nodeB, "关联")
data = connect.searchNode("IpAddr",limit = 50)
print_hi('PyCharm')
print(list(data))
print(data.count())
python 查询关系
def getRelations(self,nodes = None,r_type = None,limit = None):
relation = RelationshipMatcher(self.conn)
relationData = relation.match(nodes=nodes, r_type='{}'.format(r_type)).limit(limit)
return relationData
测试一下
relation = connect.getRelations(r_type="关联")
print(list(relation))
python修改节点
修改节点建立在查询的基础上,因此能查询到的节点就能够进行修改
data = connect.searchNode("IpAddr", limit=50)
item = (data.first())
item["IP"] = "100.100.100.100"
connect.conn.push(item)
修改成功。
python删除节点
data = connect.searchNode("IpAddr", limit=50)
item = (data.first())
print(item)
connect.conn.delete(item)
节点已经被删除了。