记录neo4j图形数据库关系线的遍历
2017-07-29 11:27
295 查看
/** * * 终端设备可视化关系网图 * @param mac * @return * @throws Exception */ public JSONObject findEquipByRestAPI(String mac) throws Exception { JSONObject jsonData = new JSONObject(); Node startNode = findNode(phoneLabel, "mac", mac.trim()); if (startNode == null) return null; if (startNode == null || !startNode.hasLabel(phoneLabel)) return null; JSONArray nodeArray = new JSONArray(); JSONArray linksArray = new JSONArray(); JSONObject nodeobj = new JSONObject(); Iterator<String> proKeys = startNode.getPropertyKeys().iterator(); int i = 0; nodeobj.put("id", i); String key = ""; while (proKeys.hasNext()) { key = proKeys.next(); nodeobj.put(key, startNode.getProperty(key).toString()); nodeobj.put("type", "phone"); } HashMap<Integer,Integer> dict = new HashMap<Integer,Integer>(); dict.put((int)startNode.getId(), i); nodeArray.add(nodeobj); //获取关系下的路径 TraversalDescription ships = Traversal.description().breadthFirst() .relationships(RelationType.CONTAIN) .relationships(RelationType.REGISTER) .relationships(RelationType.HAVE); //遍历所有的关系path for(Path path : ships.traverse(startNode)){ int start =0; int end = 0; /*Node start_Node = path.startNode(); Node end_Node = path.endNode();*/ // path = (21938)--[CONTAIN,22803]-->(21941)<--[CONTAIN,22049]--(21940) Iterator<Relationship> it = path.relationships().iterator(); //遍历每条path 获取 [CONTAIN,22803] 开始节点和结束节点 while(it.hasNext()){ Relationship next = it.next(); //(21940) Node start_Node = next.getStartNode(); //(21941) Node end_Node = next.getEndNode(); if(start_Node != end_Node){ if(dict.toString().contains(start_Node.getId()+"")){ start = dict.get((int) start_Node.getId()); } else{ nodeobj = this.renderer(start_Node); if(nodeobj.size() > 0 && nodeobj != null){ start = nodeArray.size(); nodeobj.put("id", start); nodeArray.add(nodeobj); dict.put((int) start_Node.getId(), start); } } if(dict.toString().contains(end_Node.getId()+"")){ end = dict.get((int)end_Node.getId()); } else{ nodeobj = this.renderer(end_Node); if(nodeobj.size() > 0 && nodeobj != null){ end = nodeArray.size(); nodeobj.put("id", end); nodeArray.add(nodeobj); dict.put((int) end_Node.getId(), end); } } JSONObject link = new JSONObject(); link.put("source", start); link.put("target", end); if(!linksArray.contains(link)){ linksArray.add(link); } } } /*Node start_Node = path.startNode(); Node end_Node = path.endNode();*/ } jsonData.put("links", linksArray); jsonData.put("nodes", nodeArray); return jsonData; } private JSONObject renderer(Node node) { JSONArray nodeArray = new JSONArray(); JSONObject nodeobj = new JSONObject(); if (node.hasLabel(phoneLabel)) { Iterator<String> proKeys = node.getPropertyKeys().iterator(); while (proKeys.hasNext()) { String key = proKeys.next(); if (!"pic".equals(key)) { nodeobj.put(key, node.getProperty(key).toString()); nodeobj.put("type", "phone"); } } nodeArray.add(nodeobj); } if (node.hasLabel(qqLabel)) { Iterator<String> proKeys = node.getPropertyKeys().iterator(); while (proKeys.hasNext()) { String key = proKeys.next(); if (!"pic".equals(key)) { nodeobj.put(key, node.getProperty(key).toString()); nodeobj.put("type", "qq"); } } nodeArray.add(nodeobj); } if (node.hasLabel(emailLabel)) { Iterator<String> proKeys = node.getPropertyKeys().iterator(); while (proKeys.hasNext()) { String key = proKeys.next(); if (!"pic".equals(key)) { nodeobj.put(key, node.getProperty(key).toString()); nodeobj.put("type", "email"); } } nodeArray.add(nodeobj); } if (node.hasLabel(accountLabel)) { Iterator<String> proKeys = node.getPropertyKeys().iterator(); while (proKeys.hasNext()) { String key = proKeys.next(); if (!"pic".equals(key)) { nodeobj.put(key, node.getProperty(key).toString()); nodeobj.put("type", "account"); } } nodeArray.add(nodeobj); } if (node.hasLabel(personLabel)) { Iterator<String> proKeys = node.getPropertyKeys().iterator(); while (proKeys.hasNext()) { String key = proKeys.next(); nodeobj.put(key, node.getProperty(key).toString()); nodeobj.put("type", "person"); } nodeArray.add(nodeobj); } if (node.hasLabel(addressLabel)) { Iterator<String> proKeys = node.getPropertyKeys().iterator(); while (proKeys.hasNext()) { String key = proKeys.next(); nodeobj.put(key, node.getProperty(key).toString()); nodeobj.put("type", "address"); } nodeArray.add(nodeobj); } if (node.hasLabel(phonenumberLabel)) { Iterator<String> proKeys = node.getPropertyKeys().iterator(); while (proKeys.hasNext()) { String key = proKeys.next(); nodeobj.put(key, node.getProperty(key).toString()); nodeobj.put("type", "phonenumber"); } nodeArray.add(nodeobj); } if (node.hasLabel(wechatLabel)) { Iterator<String> proKeys = node.getPropertyKeys().iterator(); while (proKeys.hasNext()) { String key = proKeys.next(); nodeobj.put(key, node.getProperty(key).toString()); nodeobj.put("type", "wechat"); } nodeArray.add(nodeobj); } return nodeobj; }
Traversal框架的使用(Java版本)
http://blog.csdn.net/u012191627/article/details/50675901相关文章推荐
- Neo4j图形数据库环境安装(一)
- 五大存储模型关系模型、键值存储、文档存储、列式存储、图形数据库
- 数据库 SQL 遍历父子关系表(二叉树)获得所有子节点 所有父节点
- 通过Spring Data Neo4J操作您的图形数据库
- 数据库 SQL 遍历父子关系表(二叉树)获得所有子节点 所有父节点
- 五大存储模型关系模型、键值存储、文档存储、列式存储、图形数据库
- 为什么选择图形数据库,为什么选择Neo4j?
- 图形数据库Neo4j restart --( /var/run/neo4j/neo4j.pid: 没有那个文件或目录
- 图形数据库Neo4j
- 数据库_基础知识_SQL中遍历一张表的每条记录并对每条记录执行相应操作的一种方法
- JavaWeb整理-数据库基础/数据库介绍.MySQL安装.DB DBS DBMS关系.登陆MySQL.SQL语言认识..SQL语言对数据库操作.SQL语言对数据表操作.SQL语言对数据表中的记录操作
- 图形数据库的优势是什么,以Neo4j为例子
- MySQL关系数据库SQL命令使用学习记录
- 如何遍历某个数据库中所有有记录的表?
- 如何遍历某数据库中的每一个表的总记录数
- 图形数据库之Neo4j学习(一)
- 图形数据库 Neo4j 开发实战
- 图形数据库Neo4J简介
- 为什么选择图形数据库,为什么选择Neo4j?
- 图形数据库 Neo4j 开发实战