• lifresh
    了解作者
  • Java
    开发工具
  • 1.6MB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 1
    下载次数
  • 2020-02-19 14:41
    上传日期
springboot集成neo4j图数据库,包含功能有节点和关系的上传和删除以及neo4j图算法的调用来挖掘节点之间的关系。
springboot_neo4j.zip
内容介绍
package com.lyz.springboot_neo4j.service; import com.alibaba.fastjson.JSONObject; import com.lyz.springboot_neo4j.entity.Expert; import com.lyz.springboot_neo4j.util.Neo4jUtil; import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer; import org.apache.commons.lang3.StringEscapeUtils; import org.neo4j.driver.v1.StatementResult; import org.neo4j.driver.v1.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.apache.log4j.Logger; import java.text.DecimalFormat; import java.util.*; /** * Created by zhzy on 19-7-10. */ @Service public class PersonSimilarity { private static final Logger logger = org.apache.log4j.Logger.getLogger(PersonSimilarity.class); @Autowired Neo4jUtil neo4jUtil; public String JaccordSim(String name,String orgname){ JSONObject re = new JSONObject(); List item_list = new ArrayList(); // List<Expert> list = new ArrayList<>(); try { String query = String.format("MATCH (p1:EXPERT {name: '%s',orgnizationname:'%s'})-[r:rel]-(cuisine1)\n" + "WITH p1, collect(id(cuisine1)) AS p1Cuisine\n" + "MATCH(p2:EXPERT)-[r:rel]-(cuisine2) WHERE p1 <> p2\n"+ "WITH p1, p1Cuisine, p2, collect(id(cuisine2)) AS p2Cuisine\n" + "RETURN p1.name AS from,\n" + "p2.name AS to,\n" + "p2.orgnizationname AS orgname," + "algo.similarity.jaccard(p1Cuisine, p2Cuisine) AS similarity\n" + "ORDER BY similarity DESC",name,orgname); StatementResult result = neo4jUtil.excuteCypherSql(query); Expert expert = new Expert(); Record record = result.next(); JSONObject reitem = new JSONObject(); String similarnode_name = record.get("to").toString().replace("\"",""); String similarnode_org = record.get("orgname").toString().replace("\"",""); DecimalFormat df = new DecimalFormat("#.##"); String similarity = df.format(Double.valueOf(record.get("similarity").toString())); reitem.put("similarnodename",similarnode_name); reitem.put("similarnodeorg",similarnode_org); reitem.put("similarity",similarity); item_list.add(reitem); /* expert.setSimilarnodename(similarnode_name); expert.setSimilarnodeorg(similarnode_org); expert.setSimilarity(df.format(Double.valueOf(record.get("similarity").toString()))); list.add(expert);*/ re.put("expert_list",item_list); String tmp = StringEscapeUtils.unescapeEcmaScript(re.toJSONString()); return tmp; }catch (Exception e){ logger.info("节点不存在!"); return "false"; } } public String DegreeSim(String name,String orgname){ JSONObject re = new JSONObject(); List item_list = new ArrayList(); List<Expert> list = new ArrayList<>(); List<Expert> list1 = new ArrayList<>(); String query0 = String.format("match (n:EXPERT{name:'%s',orgnizationname:'%s'}) Return n",name,orgname); StatementResult result0 = neo4jUtil.excuteCypherSql(query0); if(result0.hasNext()){ String query1 = String.format("match (n:EXPERT{name:'%s',orgnizationname:'%s'})"+ "-[r:rel]-(target) match (m)-[:rel]-(target) return m.name as name," + "m.orgnizationname as orgname,m.id as id",name,orgname); StatementResult result = neo4jUtil.excuteCypherSql(query1); List<String> id_list = new ArrayList<String>(); while(result.hasNext()){ Record record = result.next(); if(!id_list.contains(record.get("id").toString())){ id_list.add(record.get("id").toString()); Expert expert = new Expert(); String expert_name = record.get("name").toString(); String expert_orgname = record.get("orgname").toString(); expert.setName(expert_name); expert.setOrgnizationname(expert_orgname); list1.add(expert); } } System.out.println("listsize"+list1.size()); HashMap<Expert, Integer> hashMap = new HashMap<Expert, Integer>(); for(int i=0;i<list1.size();i++) { String expert_name = list1.get(i).getName(); String expert_orgname = list1.get(i).getOrgnizationname(); String str1 = expert_name.replace("\"", ""); String str2 = expert_orgname.replace("\"", ""); System.out.println(expert_name); System.out.println(expert_orgname); String query = String.format("match (n:EXPERT{name:'%s',orgnizationname:'%s'})\n" + "--(target)--(m:EXPERT{name:'%s',orgnizationname:'%s'})\n" + " return count(distinct target) as num", name, orgname, str1, str2); System.out.println(query); StatementResult result1 = neo4jUtil.excuteCypherSql(query); int count = Integer.valueOf(result1.next().get("num").toString()); hashMap.put(list1.get(i), count); } List<Map.Entry<Expert, Integer>> list_Data = new ArrayList<>(hashMap.entrySet()); Collections.sort(list_Data, new Comparator<Map.Entry<Expert, Integer>>() { @Override public int compare(Map.Entry<Expert, Integer> o1, Map.Entry<Expert, Integer> o2) { if (o2.getValue() != null && o1.getValue() != null && o2.getValue().compareTo(o1.getValue()) > 0) { return 1; } else { return -1; } } }); System.out.println(list_Data); Queue<Integer> queue = new LinkedList<>(); for(Map.Entry<Expert,Integer> item : list_Data){ JSONObject reitem = new JSONObject(); System.out.println("name"+item.getKey().getName()); System.out.println("name1"+name); if(item.getKey().getName().replace("\"","").equals(name)){ continue; } if(queue.isEmpty()||queue.peek()==item.getValue()){ DecimalFormat df = new DecimalFormat("#.00"); String similarnode_name = item.getKey().getName().replace("\"",""); String similarnode_org = item.getKey().getOrgnizationname().replace("\"",""); String similarity = df.format(item.getValue()); reitem.put("similarnodename",similarnode_name); reitem.put("similarnodeorg",similarnode_org); reitem.put("similarity",similarity); queue.add(item.getValue()); /* Expert expert = new Expert(); expert.setSimilarnodename(item.getKey().getName()); expert.setSimilarnodeorg(item.getKey().getOrgnizationname()); expert.setSimilarity(df.format(item.getValue())); list.add(expert);*/ }else if(queue.peek()!=item.getValue()){ break; } item_list.add(reitem); } re.put("expert_list",item_list); String tmp = StringEscapeUtils.unescapeEcmaScript(re.toJSONString()); return tmp; }else{ return "false"; } } }
评论
    相关推荐
    • supermarket-database.rar
      小型超市管理系统 目录 1、项目计划 1.1系统开发目的 1.2背景说明 1.3项目确立 1.4应用范围 1.5定义 1.6参考资料 2、逻辑分析与详细分析 2.1系统功能 2.2数据流图 2.3用户类型与职能 2.4系统开发步骤 2.5系统环境需求 2.6系统安全问题 3、基于UML的建模 3.1语义规则 3.2 UML模型 3.3系统实现图 4、概要设计文档 5、逻辑设计文档 6、物理设计文档 7、小结
    • SQL Server精华 (CHM).rar
        这是一个sql的reference,收集得很全的,是初学者不可少的工具的, 真的很好的,我以前总是不懂的去查书,现在不用了, 有这个就足够了,
    • 200472721100970.rar
      做数据库课程设计的同学们,可以下载做为参考!一个不错的数据库系统,打开后,别忘了连数据库!
    • Jason.rar
      一个网上商城系统,JSP+MySql.源码+全套论文。涉及JavaScript,力求美观。
    • mysql3.22.zip
      mysql 3.22源码
    • studentinformationmanagement.rar
      自己用VB和SQL编写的一个学生学籍管理系统,希望对大家有帮助
    • 20080403.rar
      员工管理系统 vb+sql 文档全 管理员admin 密码 123
    • gnusql-0.7b5.1.tar.gz
      免费的Sql数据库系统
    • likoxglc.taz
      一个Unix下的ODBC数据库引擎,支持SQL 92 and SQL 89,支持Oracle、Sybase、Informix、DB2
    • MS_Access_2000.rar
      MS Access 2000 中文绿色精简版 可以正常的运行在Win2000、WinXP系统中,对于那些平常需要打开Access数据库而不愿意安装庞大的office的用户来说,很必要的。