当前位置:   article > 正文

neo4j图形数据库第四弹——整合springboot(支持查询路径)_neo4j 路径查询

neo4j 路径查询

正经学徒,佛系记录,不搞事情

基于上文: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 对返回的路径进行解析

  1. /**
  2. * cql 路径查询 返回节点和关系
  3. * @param cql 查询语句
  4. * @param nodeList 节点
  5. * @param edgeList 关系
  6. * @return List<Map<String,Object>>
  7. */
  8. public static <T> void getPathList(String cql, Set<T> nodeList, Set<T> edgeList) {
  9. try {
  10. Session session = driver.session();
  11. StatementResult result = session.run(cql);
  12. List<Record> list = result.list();
  13. for (Record r : list) {
  14. for (String index : r.keys()) {
  15. Path path = r.get(index).asPath();
  16. //节点
  17. Iterable<Node> nodes = path.nodes();
  18. for (Iterator iter = nodes.iterator(); iter.hasNext(); ) {
  19. InternalNode nodeInter = (InternalNode) iter.next();
  20. Map<String, Object> map = new HashMap<>();
  21. //节点上设置的属性
  22. map.putAll(nodeInter.asMap());
  23. //外加一个固定属性
  24. map.put("nodeId", nodeInter.id());
  25. nodeList.add((T) map);
  26. }
  27. //关系
  28. Iterable<Relationship> edges = path.relationships();
  29. for (Iterator iter = edges.iterator(); iter.hasNext(); ) {
  30. InternalRelationship relationInter = (InternalRelationship) iter.next();
  31. Map<String, Object> map = new HashMap<>();
  32. map.putAll(relationInter.asMap());
  33. //关系上设置的属性
  34. map.put("edgeId", relationInter.id());
  35. map.put("edgeFrom", relationInter.startNodeId());
  36. map.put("edgeTo", relationInter.endNodeId());
  37. edgeList.add((T) map);
  38. }
  39. }
  40. }
  41. } catch (Exception e) {
  42. e.printStackTrace();
  43. }
  44. }

 调用方法:

  1. @GetMapping("getPath")
  2. public Map<String, Object> getPath(){
  3. Map<String, Object> retMap = new HashMap<>();
  4. //cql语句
  5. String cql = "match l=(m)-[]-(n) return l";
  6. //待返回的值,与cql return后的值顺序对应
  7. Set<Map<String ,Object>> nodeList = new HashSet<>();
  8. Set<Map<String ,Object>> edgeList = new HashSet<>();
  9. neo4jUtil.getPathList(cql,nodeList,edgeList);
  10. retMap.put("nodeList",nodeList);
  11. retMap.put("edgeList",edgeList);
  12. return retMap;
  13. }
  14. @GetMapping("getShortPath")
  15. public Map<String, Object> getShortPath(){
  16. Map<String, Object> retMap = new HashMap<>();
  17. //cql语句
  18. String cql = "match l=shortestPath(({name:'Keanu Reeves'})-[*]-({title:\"Jerry Maguire\"})) return l";
  19. //待返回的值,与cql return后的值顺序对应
  20. Set<Map<String ,Object>> nodeList = new HashSet<>();
  21. Set<Map<String ,Object>> edgeList = new HashSet<>();
  22. neo4jUtil.getPathList(cql,nodeList,edgeList);
  23. retMap.put("nodeList",nodeList);
  24. retMap.put("edgeList",edgeList);
  25. return retMap;
  26. }

结果:

项目地址:

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/1013787
推荐阅读
相关标签
  

闽ICP备14008679号