图数据库 py2neo driver 使用笔记

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

        查询结果

图数据库 py2neo driver 使用笔记

        查询处理

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

上一篇:C#技术栈入门到精通系列13——日志框架Log4Net


下一篇:%nd宽度规则