Springboot集成Neo4j驱动模式开发

     在学习Neo4j的时候网上找了很多资料也参考了很多大牛的博客,感觉这方面的资料实在太少了有的博客只是只言片语表达的很不清晰因此走了很多弯路。查找了很多关于Springboot集成Neo4j的资料大部分都是基于springdata来做的,这方面看似很简单但是如果执行插入操作就会很慢 影响插入效率。虽然他有@query标签可以书写cypher语句但是无法做到拼接组装cypher,无奈之下只有重构,改成Neo4j的驱动模式开发,具体操作如下:

   1:首先创建一个简单的Springboot项目

Springboot集成Neo4j驱动模式开发_第1张图片


2:在pom.xml中导入Neo4j的相关联的Maven坐标


   org.springframework.boot
   spring-boot-starter-data-neo4j



   org.neo4j
   neo4j-ogm-bolt-driver
   2.1.3

3:配置文件中配置行相应的信息

这个是http访问的配置 我的项目里有两个模式http模式和驱动模式

#neo4j 数据库配置
spring:
  data:
    neo4j:
      uri: http://192.168.1.108:7474
      username: neo4j
      password: 123456
#Neo4j驱动模式配置
blotUri: bolt://192.168.1.108:7687

4:在程序中配置驱动模式信息

public class Neo4jV2Application {

    @Value("${blotUri}")
    private String uri;

    @Value("${spring.data.neo4j.username}")
    private String username;

    @Value("${spring.data.neo4j.password}")
    private String password;

    public static void main(String[] args) {
        SpringApplication.run(Neo4jV2Application.class, args);
    }

    /**
     * 图数据库驱动模式
     *
     * @return
     */

    @Bean
    public Driver neo4jDriver() {
        return GraphDatabase.driver(uri, AuthTokens.basic(username,password));
    }

5:注入实例访问Neo4j图数据库

@Resource
private Driver driver;

public void getWbswryxxMessage(ConsumerRecord record) {

    Wbswryxx wbswryxx = JSON.parseObject(record.value().toString(), Wbswryxx.class);
    if (wbswryxx != null) {
        Session session = null;
        Transaction tx = null;
        try {
            if (wbswryxx.getZjhm() != null) {
                Map map = IdCardUtil.VerifyIdcardAndFullIDCard(wbswryxx.getZjhm());
                String idcard = (String)map.get("idcard");

                if ((Boolean) map.get("idcardIsOk")) {

                    Map params = new HashMap<>();
                    String detailAddress=null;
                    if (wbswryxx.getYycsmc() != null) {
                        StringBuffer sb = new StringBuffer();

                        if (wbswryxx.getYycsxzqh() != null) {
                            sb.append(wbswryxx.getYycsxzqh());
                        }
                        if (wbswryxx.getYycsdz() != null) {
                            sb.append(wbswryxx.getYycsdz());
                        }

                        sb.append(wbswryxx.getYycsmc());
                        detailAddress = sb.toString();


                    }


                    StringBuffer sbCypher = new StringBuffer();

                    idcard = IdCardUtil.getFullIDCard(idcard);
                    params.put("idcard", idcard);
                    sbCypher.append("Merge(a:Persion{idcard:{idcard}})");

                    if (wbswryxx.getYycsmc() != null) {
                        params.put("detailAddress", detailAddress);
                        sbCypher.append(" Merge(b:InternetBar{detailAddress:{detailAddress}})").append("Merge (a)-[r:PersionToInternetBar{swsj:{beginTime},xwsj:{endTime}}]->(b)");
                    }

                    if (wbswryxx.getSwkssjDate() != null) {

                        params.put("beginTime", wbswryxx.getSwkssjDate().getTime());
                    } else {
                        params.put("beginTime", 0);

                    }
                    if (wbswryxx.getXwsjDate() != null) {

                        params.put("endTime", wbswryxx.getXwsjDate().getTime());
                    } else {
                        params.put("endTime", 0);

                    }
                    params.put("persionName", wbswryxx.getSwryxm());
                    params.put("persionSex", wbswryxx.getXb());

                    String birthday = idcard.substring(6, 14);
                    if(IdCardUtil.VerifyBirthday(birthday)){

                        params.put("persionBirthday", Integer.parseInt(birthday));
                    }else {
                        params.put("persionBirthday", Integer.parseInt("0"));

                    }
                    sbCypher.append("on create set a.name={persionName},a.sex={persionSex},a.birthday={persionBirthday}");

                    if (wbswryxx.getYycsmc() != null) {

                        params.put("InternetBarName", wbswryxx.getYycsmc());
                        sbCypher.append(",b.name={InternetBarName}");

                        if (wbswryxx.getYycsdz() != null) {
                            params.put("InternetBarAddress", wbswryxx.getYycsdz());
                            sbCypher.append(",b.address={InternetBarAddress}");
                        }
                        if (wbswryxx.getYycsxzqh() != null) {
                            params.put("InternetBarQH", wbswryxx.getYycsxzqh());
                            sbCypher.append(",b.administrativeDivision={InternetBarQH} ");
                        }
                    }

                    String cypher = sbCypher.toString();


                    session = driver.session();
                    tx = session.beginTransaction();
                    tx.run(cypher, params);

                    tx.success();
                }

            }


        } catch (Exception e) {
            logger.error("网吧信息表插入数据报错具体信息如下 " + record.toString());
            logger.error("异常信息为: " + e.getMessage());
        } finally {
            if (null != tx) {
                tx.close();
            }
            if (null != session) {
                session.close();
            }

        }
    }
}

6:通过驱动模式进行查询操作  转换成实体对象的时候用到了fastJson进行转换

@Resource
private Driver neo4j;
@Override
public Neo4jResoult getPersionsByImportPersionCase(Long hour, String prevNumber, String name, Neo4jResoult
        neo4jResoult) {


    Session session = null;
    Transaction tx = null;
    try {
        session = neo4j.session();
        tx = session.beginTransaction();

        //查询出案件结点
        Map params = new HashMap();
        params.put("name", name);
        StatementResult result1 = tx.run(" match(a:ImportPersionCase{name:{name}}) return  a", params);
        int indexImportPersionCase = 0;
        while (result1.hasNext()) {
            org.neo4j.driver.v1.types.Node node = result1.next().get("a").asNode();
            Map, Object> map = node.asMap();
            String mapString = JSONObject.toJSONString(map);
            ImportPersionCase importPersionCase = JSONObject.parseObject(mapString, ImportPersionCase.class);


        }

        tx.success();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {

        if (null != tx) {
            tx.close();
        }
        if (null != session) {
            session.close();
        }

    }


    return new Neo4jResoult();


}
欢迎评论转载 转载请注明出处 https://blog.csdn.net/zhanaolu4821/article/details/80940598

你可能感兴趣的:(大数据~~Neo4j)