图数据库 py2neo driver 使用笔记

本文介绍使用Neo4j图数据库进行路径查询的方法,并详细解释了如何通过Cypher语句匹配从起始节点到目标节点的关系路径。同时,文中还提供了处理查询结果的具体步骤,包括节点和关系的提取及格式化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、Node

py2neo.data – Graph data types — py2neo 2021.1

2、relationships

py2neo.data – Graph data types — py2neo 2021.1

3、path

py2neo.data – Graph data types — py2neo 2021.1

        1、路径查询

根据起始节点与目标节点查询关系图

        cypher语句

MATCH p=(n)-[*..3]->(m) 
where n.name = '韩国' and m.name = '中国'
return p limit 10

        查询结果

        查询处理

def pathFinding(kgName, sourceName, targetName):
    graph = graphClient
    cates, filtEntity, filtRela = [], [], []
    result = {}
    entityList, relationList = [], []
    query = ''' MATCH p=(n)-[*..3]-(m) where n.name contains('{}') and m.name contains("{}")  
    return p limit 10 '''.format(sourceName.strip(), targetName.strip())
    print('''====关系查询''', query)
    try:
        res = graph.run(query).data()
        print(type(res), len(res))
        for i in res:
            i = i['p']
            for r in i.relationships:
                print(type(r), r)
                print(r['name'])
                print(r.identity)
                print(type(r.start_node), type(r.end_node))
                print(r.start_node, r.end_node)
                ids = r.start_node['objId']
                if ids not in filtEntity:
                    nd = {}
                    nd['id'] = r.start_node['objId']
                    nd['name'] = r.start_node['name']
                    if sourceName in nd['name']:
                        nd['category'] = '起始节点'
                        cates.append('起始节点')
                    elif targetName in nd['name']:
                        nd['category'] = '目标节点'
                        cates.append('目标节点')
                    else:
                        nd['category'] = r.start_node['category']
                        cates.append(nd['category'])
                    entityList.append(nd)
                    filtEntity.append(ids)
                ide = r.end_node['objId']
                if ide not in filtEntity:
                    nd = {}
                    nd['id'] = r.end_node['objId']
                    nd['name'] = r.end_node['name']
                    if sourceName in nd['name']:
                        nd['category'] = '起始节点'
                        cates.append('起始节点')
                    elif targetName in nd['name']:
                        nd['category'] = '目标节点'
                        cates.append('目标节点')
                    else:
                        nd['category'] = r.start_node['category']
                        cates.append(nd['category'])
                    entityList.append(nd)
                    filtEntity.append(ide)
                idr = r.identity
                if idr not in filtRela:
                    link = {}
                    link['id'] = str(idr)
                    link['source'] = r.start_node['objId']
                    link['target'] = r.end_node['objId']
                    link['name'] = r['shortName']
                    relationList.append(link)
                    filtRela.append(idr)
        result['entityList'] = entityList[:]
        result['relationList'] = relationList[:]
        cates = [{'name': i} for i in list(set(cates))]
        result['cates'] = sorted(cates, key=lambda e: e.__getitem__('name'))
        print("====所有分类排序", result['cates'])
        return result
    except Exception as e:
        print("=====error 路径查询错误", e)
        result['entityList'] = entityList[:]
        result['relationList'] = relationList[:]
        result['cates'] = cates
        return result

4、有关异常

TypeError: 'frozenset' object does not support indexing

        :py2neo.graph.run(cql).data()返回数据中获取关系类型【relationshipe.types()】时报错

参考:py2neo.data – Graph data types — py2neo 2021.1

正确获取方式:

list(relationshipe.types())[0]

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值