Neo4J操作2(关系查询): python3.6的py2neo操作neo4j3.4.2,获取所有关系

关系查询,用到的是py2neo,match

不说废话,直接上代码:

# -*- coding: UTF-8 -*-
#!/usr/bin/python
# @Time     :2019/3/12 15:26
# @author   :Mo
# @site     :https://blog.csdn.net/rensihui

# graph密码必须自己换一个,

from py2neo import Graph, Node, Relationship, NodeMatcher

def writeLines(lines, file_path, write_type = 'w', encode_type = 'gbk'):
    '''写入txt'''
    file_write = open(file_path, write_type, encoding=encode_type)
    file_write.writelines(lines)
    file_write.close()

print('\n\n        必须输入自己的密码\n\n')

'''单条Relationship查询'''
graph = Graph("http://localhost:7474", username="neo4j", password='jy')
relationship = graph.match_one(r_type='rel') #注意r_type,只是查1个
start_uid = relationship.start_node
end_iid = relationship.end_node
print(relationship, type(relationship))


'''多条Relationship查询'''
user_id = [','.join(["card_id", "year", "salary"]) + "\n"]
item_id = [','.join(["product_id", "product_category", "rate"]) +  "\n"]
relation_ship = [','.join(["card_id", "product_id", "sales_amount"]) +  "\n"]

relationship2 = graph.match(r_type='rel')
for relationship2_one in relationship2:
    hl_uid = ["card_id", "year", "salary"]
    start_uid = relationship2_one.start_node
    start_uid_list = []
    for hl_uid_one in hl_uid:
        start_uid_list.append(start_uid[hl_uid_one])
    user_id.append(','.join(start_uid_list) +  "\n")

    hl_iid = ["product_id", "product_category", "rate"]
    end_iid = relationship2_one.end_node
    end_iid_list = []
    for hl_iid_one in hl_iid:
        end_iid_list.append(end_iid[hl_iid_one])
    item_id.append(','.join(end_iid_list) +  "\n")

    # relation_ship_list = []
    relation_ship_list = [start_uid_list[0], end_iid_list[0],  relationship2_one.relationships[0]["sales_amount"]]
    relation_ship.append(','.join(relation_ship_list) +  "\n")

writeLines(user_id,       'user_id.csv',       write_type='w', encode_type='gbk')
writeLines(item_id,       'item_id.csv',       write_type='w', encode_type='gbk')
writeLines(relation_ship, 'relation_ship.csv', write_type='w', encode_type='gbk')






gg = 0




#删除所有知识图谱实体与关系
# graph.delete_all()
'''
1 —— 创建node,函数第一个参数是节点类型,第二个参数是value值
'''
a = Node('PersonTest', name='张三')
b = Node('PersonTest', name='李四')
r = Relationship(a, 'KNOWNS', b)
s = a | b | r
graph.create(s)
###
r1 = Relationship(b, 'LOVES', a)
s1 = a | b | r1
graph.create(s1)

# 用CQL进行查询,返回的结果是list
data1 = graph.run('MATCH(p:PersonTest) return p')
print("data1 = ", data1, type(data1))
print('###########################################')


matcher = NodeMatcher(graph)
nodes = matcher.match(label = "Person", name="test_node_2")
print(nodes)
# print('###########################################')


#    3 —— Relationship查询
relationship = graph.match_one(r_type='KNOWNS')
print (relationship, type(relationship))
print('###########################################')

# #   5 —— 删除某node,在删除node之前需要先删除relationship
# node = matcher.match(label='PersonTest', property_key='name', property_value="李四")
# relationship = graph.match_one(r_type='KNOWNS')
# graph.delete(relationship)
# graph.delete(node)
# data6 = matcher.match(label='PersonTest')
# for data in data6:
#     print ("data6 = ", data)
# print('###########################################')

#   6 —— 多条件查询
a = Node('PersonTest', name='张三', age=21, location='广州')
b = Node('PersonTest', name='李四', age=22, location='上海')
c = Node('PersonTest', name='王五', age=23, location='北京')
r1 = Relationship(a, 'KNOWS', b)
r2 = Relationship(b, 'KNOWS', c)
s = a | b | c | r1 | r2
graph.create(s)
data7 = matcher.match(label='PersonTest')
for data in data7:
    print ("data7 = ", data)

print('###########################################')
# 单条件查询,返回的是多个结果
selector = NodeMatcher(graph)
persons = selector.match('PersonTest', age=21)
print("data8 = ", list(persons))
print('###########################################')



# 多条件查询
selector = NodeMatcher(graph)
persons = selector.match('PersonTest', age=21, location='广州')
print("data9 = ", list(persons))



# orderby进行更复杂的查询
selector = NodeMatcher(graph)
persons = selector.match('PersonTest').order_by('_.age')
for data in persons:
    print ("data10 = ", data)

希望对你有所帮助

你可能感兴趣的:(知识图谱,Neo4J)