1. 程式人生 > >neo4j圖形資料庫第四彈——整合springboot(支援查詢路徑)

neo4j圖形資料庫第四彈——整合springboot(支援查詢路徑)

正經學徒,佛系記錄,不搞事情

基於上文:https://blog.csdn.net/qq_31748587/article/details/84286411 的專案

普通的neo4j查詢路徑的cql語法如下:

match l=(m)-[]-(n) return l

neo4j還支援最短路徑的查詢方式,語法如下:

match l=shortestPath(({name:'Keanu Reeves'})-[*]-({title:"Jerry Maguire"})) return l

對應的springboot專案的工具類 Neo4jUtil 中新增方法 getPathList

對返回的路徑進行解析

    /**
     * cql 路徑查詢 返回節點和關係
     * @param cql 查詢語句
     * @param nodeList 節點
     * @param edgeList 關係
     * @return List<Map<String,Object>>
     */
    public static <T> void getPathList(String cql, Set<T> nodeList, Set<T> edgeList) {
        try {
            Session session = driver.session();
            StatementResult result = session.run(cql);
            List<Record> list = result.list();
            for (Record r : list) {
                for (String index : r.keys()) {
                    Path path = r.get(index).asPath();
                    //節點
                    Iterable<Node> nodes = path.nodes();
                    for (Iterator iter = nodes.iterator(); iter.hasNext(); ) {
                        InternalNode nodeInter = (InternalNode) iter.next();
                        Map<String, Object> map = new HashMap<>();
                        //節點上設定的屬性
                        map.putAll(nodeInter.asMap());
                        //外加一個固定屬性
                        map.put("nodeId", nodeInter.id());
                        nodeList.add((T) map);
                    }
                    //關係
                    Iterable<Relationship> edges = path.relationships();
                    for (Iterator iter = edges.iterator(); iter.hasNext(); ) {
                        InternalRelationship relationInter = (InternalRelationship) iter.next();
                        Map<String, Object> map = new HashMap<>();
                        map.putAll(relationInter.asMap());
                        //關係上設定的屬性
                        map.put("edgeId", relationInter.id());
                        map.put("edgeFrom", relationInter.startNodeId());
                        map.put("edgeTo", relationInter.endNodeId());
                        edgeList.add((T) map);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 呼叫方法:

    @GetMapping("getPath")
    public Map<String, Object> getPath(){
        Map<String, Object> retMap = new HashMap<>();
        //cql語句
        String cql = "match l=(m)-[]-(n) return l";
        //待返回的值,與cql return後的值順序對應
        Set<Map<String ,Object>> nodeList = new HashSet<>();
        Set<Map<String ,Object>> edgeList = new HashSet<>();
        neo4jUtil.getPathList(cql,nodeList,edgeList);
        retMap.put("nodeList",nodeList);
        retMap.put("edgeList",edgeList);
        return retMap;
    }
    @GetMapping("getShortPath")
    public Map<String, Object> getShortPath(){
        Map<String, Object> retMap = new HashMap<>();
        //cql語句
        String cql = "match l=shortestPath(({name:'Keanu Reeves'})-[*]-({title:\"Jerry Maguire\"})) return l";
        //待返回的值,與cql return後的值順序對應
        Set<Map<String ,Object>> nodeList = new HashSet<>();
        Set<Map<String ,Object>> edgeList = new HashSet<>();
        neo4jUtil.getPathList(cql,nodeList,edgeList);
        retMap.put("nodeList",nodeList);
        retMap.put("edgeList",edgeList);
        return retMap;
    }

結果:

專案地址:

http://note.youdao.com/noteshare?id=9da90834156c95d23a6f80e3a28ba623