Neo4j数据库操作性能优化(Java)

天行健,君子以自强不息;地势坤,君子以厚德载物。


每个人都有惰性,但不断学习是好好生活的根本,共勉!


文章均为学习整理笔记,分享记录为主,如有错误请指正,共同学习进步。

Neo4j数据库操作性能优化

  • 前言
  • 具体实现
    • 1. properties文件
    • 2. 配置类代码
    • 3. 工具类代码
    • 4. 请求控制类


前言

基于上一篇,Java实现对Neo4j数据库的操作
文章链接:https://blog.csdn.net/mo_sss/article/details/132082356

基于上篇文章对neo4j数据库的操作,优化其资源创建和关闭的过程,提高数据处理的效率
其主要部分就是将资源创建放在配置类中,不去关闭资源,这样就会快很多

具体实现

1. properties文件

neo4j数据库参数读取文件neo4j.properties

neo4j.url=bolt://47.101.136.161:7687
neo4j.username=neo4j
neo4j.password=123456

注:数据库地址和账号密码都是假的请根据自己的数据来配置

2. 配置类代码

Neo4jConfig.java

package com.data.config;


import lombok.Data;
import org.neo4j.driver.AuthTokens;
import org.neo4j.driver.Driver;
import org.neo4j.driver.GraphDatabase;
import org.neo4j.driver.Session;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;


/**
* @ClassDescription: neo4j数据库参数配置类
* @Author:李白
* @Date:2023/7/31 15:57
*/
@Data
@Component
//@PropertySource("neo4j.yml")
@PropertySource("neo4j.properties")
@ConfigurationProperties(prefix = "neo4j")
public class Neo4jConfig {
private String url;
   private String username;
   private String password;


@Bean
public Session neoSession() {

Driver driver = GraphDatabase.driver(url, AuthTokens.basic(username, password));
  Session session = driver.session();


       return session;
}

}

3. 工具类代码

Neo4jUtil.java

package com.data.utils.neo4j;


import com.alibaba.fastjson.JSONObject;
import com.data.config.Neo4jConfig;
import org.neo4j.driver.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;


/**
* @ClassDescription: 更快
* @Author:李白
* @Date:2023/8/14 16:01
*/
@Component
public class Neo4jUtil {
@Autowired
Session session;
   private static Session neoSession;
@PostConstruct
public void s(){
neoSession = session;
}


/**
    * 增删改接口调用(create/delete/set)
  * @param cql
  */
public static void cds(String cql){
neoSession.run(cql);
}




/**
    * 查询接口调用(match)
    * @param cql
  * @return
  */
public static List m(String cql){



Result result = neoSession.run(cql);
//        System.out.println("result: "+result);
  List list = new ArrayList();
  //获取每一个节点
  while ( result.hasNext() )

{

Record record = result.next();
//            System.out.println("record: "+record.get("n"));
//            record.get("n");
  Map<String, Object> map = record.get("n").asMap();
  Set<Entry<String, Object>> set = map.entrySet();
  JSONObject nodeProperties = new JSONObject();
  //遍历每个节点中的信息,都是key-value形式的
  for(Map.Entry one : set){

nodeProperties.put(one.getKey().toString(),one.getValue());
  System.out.print(one.getKey() + "=" + one.getValue() + ";");
  }

System.out.println();
  list.add(nodeProperties);
//            System.out.println("nodeProperties: "+nodeProperties);
  }

list.stream().sorted();
//        System.out.println(list);
  return list;
}




/**
    * 检查节点是否存在接口调用
  * @param cql
  * @return
  */
public static Boolean checkNode(String cql){

Result result = neoSession.run(cql);


       return result.hasNext();
}
}

4. 请求控制类

对数据库的操作,即上篇文章中的代码中的Neo4jUtils换成Neo4jUtil即可
Neo4jController.java

package com.data.neo4j;


import com.alibaba.fastjson.JSONObject;
import com.data.neo4j.entity.UserNode;
import com.data.utils.neo4j.Neo4jUtil;
import org.springframework.web.bind.annotation.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
* @ClassDescription: 请求调用-neo4j数据操作
* @Author:李白
* @Date:2023/7/31 8:27
*/
@RestController
@RequestMapping("/neo4j-part")
public class Neo4jController {


/**
    * 新增节点-用户----------------------问题:字符串直接使用需要用反斜杠转义,以变量形式填入则会报错。
  */
@PostMapping("/add")
public void add(/*@RequestParam("name")String name*/){

String name = "libai666";
  String cql = "create (u:User {name:\""+name+"\", Group:\"internal\", email:\"[email protected]\"})";
  Neo4jUtil.cds(cql);
}


/**
    * ----------------------------------------------------新增节点-------------------------------------------------------
   */
  /**
    * 新增用户
  */
@PostMapping("/addDevice")
public void addDevice(/*@RequestParam("deviceIdPre")String deviceIdPre*/){

String deviceGroup = "internal";
  //创建二十个设备
  for (int i = 1; i < 21; i++) {
if (i<10){

String deviceIdPre = "UA000";
  String cql = "create (d:Device {deviceId:"+"\""+deviceIdPre+i+"\""+", deviceGroup:"+"\""+deviceGroup+"\""+"})";
  Neo4jUtil.cds(cql);
  }else {

String deviceIdPre = "UA00";
  String cql = "create (d:Device {deviceId:"+"\""+deviceIdPre+i+"\""+", deviceGroup:"+"\""+deviceGroup+"\""+"})";
  Neo4jUtils.cds(cql);
  }

}

}


/**
    * 新增设备
  */
@PostMapping("/addUser")
public void addUser(){
//用户信息
  UserNode un1 = new UserNode("cx","internal","[email protected]");
  UserNode un2 = new UserNode("zzw","internal","[email protected]");
  UserNode un3 = new UserNode("cxl","internal","[email protected]");
  UserNode un4 = new UserNode("ljk","internal","[email protected]");
  UserNode un5 = new UserNode("bsj","internal","[email protected]");
  UserNode un6 = new UserNode("wzw","internal","[email protected]");
  UserNode un7 = new UserNode("wgx","internal","[email protected]");
  UserNode un8 = new UserNode("jxh","internal","[email protected]");
  UserNode un9 = new UserNode("wt","internal","[email protected]");
  List<UserNode> list = new ArrayList();
  list.add(un1);
  list.add(un2);
  list.add(un3);
  list.add(un4);
  list.add(un5);
  list.add(un6);
  list.add(un7);
  list.add(un8);
  list.add(un9);
  //创建9个用户
  for (int i = 0; i < list.size(); i++) {

UserNode u = list.get(i);
  String cql = "create (u:User {name:\""+u.getUserName()+"\", userGroup:\""+u.getUserGroup()+"\",email:\""+u.getEmail()+"\"})";
  Neo4jUtil.cds(cql);
  }

}


/**
    * ----------------------------------------------------查询节点-------------------------------------------------------
   */


  /**
    * 查询所有节点信息
  * @return
  */
@GetMapping("/searchAll")
public JSONObject searchAll(){

String cql = "match (n) return n";
  List m = Neo4jUtil.m(cql);
  JSONObject listJson = new JSONObject();
  listJson.put("allNodes",m);
       return listJson;
}


/**
    * 查询指定标签的所有节点信息
  * @param label 标签
  * @return
  */
@GetMapping("/searchLabelNodes")
public JSONObject searchLabelNodes(@RequestParam("label")String label){

String cql = "match (n:"+label+") return n";
  List m = Neo4jUtil.m(cql);
  JSONObject listJson = new JSONObject();
  listJson.put("labelNodes",m);
       return listJson;
}


/**
    * 查询具体的一个节点信息--查询设备具体节点
  * @param deviceId 设备号
  * @return
  */
@GetMapping("/searchOneDevice")
public JSONObject searchOneDevice(@RequestParam("deviceId")String deviceId){

String cql = "match (n:Device{deviceId:\""+deviceId+"\"}) return n";
  List m = Neo4jUtil.m(cql);
  JSONObject listJson = new JSONObject();
  listJson.put("device",m);
       return listJson;
}


/**
    * 查询具体的一个节点信息--查询设备具体节点
  * @param name 用户名
  * @return
  */
@GetMapping("/searchOneUser")
public JSONObject searchOneUser(@RequestParam("name")String name){

String cql = "match (n:User{name:\""+name+"\"}) return n";
  List m = Neo4jUtil.m(cql);
  JSONObject listJson = new JSONObject();
  listJson.put("user",m);
       return listJson;
}


/**
    * ----------------------------------------------------删除节点-------------------------------------------------------
   */
  /**
    * 删除全部节点
  */
@DeleteMapping("/deleteAllNodes")
public void deleteAllNodes(){

String cql = "match(n) delete n";
  Neo4jUtil.cds(cql);
}


/**
    * 删除同一标签的全部节点
  * @param label 标签
  */
@DeleteMapping("/deleteLabelAllNodes")
public void deleteLabelAllNodes(@RequestParam("label")String label){

String cql = "match(n:"+label+") delete n";
  Neo4jUtil.cds(cql);
}


/**
    * 删除指定的节点
  * @param name 用户名
  */
@DeleteMapping("/deleteOneUser")
public void deleteOneUser(@RequestParam("name")String name){

String cql = "match(n:User{name:\""+name+"\"}) delete n";
  Neo4jUtil.cds(cql);
}


/**
    * 删除指定的节点
  * @param deviceId 设备号
  */
@DeleteMapping("/deleteOneDevice")
public void deleteOneDevice(@RequestParam("deviceId")String deviceId){

String cql = "match(n:Device{deviceId:\""+deviceId+"\"}) delete n";
  Neo4jUtil.cds(cql);
}


/**
    * ----------------------------------------------------修改节点-------------------------------------------------------
   */
  /**
    * 根据用户名修改用户组和用户邮箱
  * @param name 用户名
  * @param userGroup 用户组
  * @param email 用户邮箱
  */
@PutMapping("/updateUser")
public void updateUser(@RequestParam("name")String name,
  @RequestParam("userGroup")String userGroup,
  @RequestParam("email")String email){

String cql = "match(n:User{name:\""+name+"\"}) set n.userGroup=\""+userGroup+"\",n.email=\""+email+"\"";
  Neo4jUtil.cds(cql);
}


/**
    * 根据deviceId修改设备组
  * @param deviceId 设备号
  * @param deviceGroup 设备组
  */
@PutMapping("/updateDevice")
public void updateDevice(@RequestParam("deviceId")String deviceId,
  @RequestParam("deviceGroup")String deviceGroup){

String cql = "match(n:Device{deviceId:\""+deviceId+"\"}) set n.deviceGroup=\""+deviceGroup+"\"";
  Neo4jUtil.cds(cql);
}




/**
    * ----------------------------------------------------创建关系-------------------------------------------------------
   */
  /**
    * 创建用户和设备之间的关系
  * @param name 用户名
  * @param deviceId 设备号
  */
@PostMapping("/createR")
public void createR(@RequestParam("name")String name,
  @RequestParam("deviceId")String deviceId){

Date date = new Date();
  SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd hhmmss");
  String dateStr = sdf.format(date);
  String cql1 = "match(u:User{name:\""+name+"\"}),(d:Device{deviceId:\""+deviceId+"\"})create (u)-[r:Borrow{date:\""+dateStr+"\"}]->(d)";
  Neo4jUtil.cds(cql1);
  String cql2 = "match(u:User{name:\""+name+"\"}),(d:Device{deviceId:\""+deviceId+"\"})create (u)<-[r:Belong{date:\""+dateStr+"\"}]-(d)";
  Neo4jUtil.cds(cql2);
}


/**
    * ----------------------------------------------------查询关系-------------------------------------------------------
   */
  /**
    * 查询指定用户名下的借用设备
  * @param name 用户名
  * @return
  */
@GetMapping("/searchU2D")
public JSONObject searchU2D(@RequestParam("name")String name){

JSONObject u2dJson = new JSONObject();
  String cql = "match(u:User{name:\""+name+"\"})-[r:Borrow]->(n) return n";
  List m = Neo4jUtil.m(cql);
  u2dJson.put("u2dList",m);
       return u2dJson;
}


/**
    * 查询指定设备的使用用户列表
  * @param deviceId 设备号
  * @return
  */
@GetMapping("/searchD2U")
public JSONObject searchD2U(@RequestParam("deviceId")String deviceId){

JSONObject d2uJson = new JSONObject();
  String cql = "match(d:Device{deviceId:\""+deviceId+"\"})-[r:Belong]->(n) return n";
  List m = Neo4jUtil.m(cql);
  d2uJson.put("d2uList",m);
       return d2uJson;
}


/**
    * ----------------------------------------------------删除关系-------------------------------------------------------
   */
  /**
    * 删除指定用户与指定设备的关系
  * @param name 用户名
  * @param deviceId 设备号
  */
@DeleteMapping("/deleteU2DR")
public void deleteU2DR(@RequestParam("name")String name,
  @RequestParam("deviceId")String deviceId){

String cql1 = "match(u:User{name:\""+name+"\"})-[r:Borrow]->(d:Device{deviceId:\""+deviceId+"\"}) delete r";
  String cql2 = "match(u:User{name:\""+name+"\"})<-[r:Belong]-(d:Device{deviceId:\""+deviceId+"\"}) delete r";
  Neo4jUtil.cds(cql1);
  Neo4jUtil.cds(cql2);
}


/**
    * 删除指定用户与所有设备的关系
  * @param name 用户名
  */
@DeleteMapping("/deleteU2AllDR")
public void deleteU2AllDR(@RequestParam("name")String name){

String cql1 = "match(u:User{name:\""+name+"\"})-[r:Borrow]->(d:Device) delete r";
  String cql2 = "match(u:User{name:\""+name+"\"})<-[r:Belong]-(d:Device) delete r";
  Neo4jUtil.cds(cql1);
  Neo4jUtil.cds(cql2);
}


/**
    * 删除指定设备与所有用户的关系
  * @param deviceId 设备号
  */
@DeleteMapping("/deleteD2AllUR")
public void deleteD2AllUR(@RequestParam("deviceId")String deviceId){

String cql1 = "match(u:User)-[r:Borrow]->(d:Device{deviceId:\""+deviceId+"\"}) delete r";
  String cql2 = "match(u:User)<-[r:Belong]-(d:Device{deviceId:\""+deviceId+"\"}) delete r";
  Neo4jUtil.cds(cql1);
  Neo4jUtil.cds(cql2);
}




/**
    * ----------------------------------------------------修改关系-------------------------------------------------------
   */
  /**
    * 修改指定用户与指定设备的关系
  * @param name 用户名
  * @param deviceId 设备号
  * @param expire 设备借用时间
  */
@PutMapping("/updateU2DR")
public void updateU2DR(@RequestParam("name")String name,
  @RequestParam("deviceId")String deviceId,
  @RequestParam("expire")String expire){

String cql1 = "match(u:User{name:\""+name+"\"})-[r:Borrow]->(d:Device{deviceId:\""+deviceId+"\"}) set r.date=\""+expire+"\"";
  String cql2 = "match(u:User{name:\""+name+"\"})<-[r:Belong]-(d:Device{deviceId:\""+deviceId+"\"}) set  r.date=\""+expire+"\"";
  Neo4jUtil.cds(cql1);
  Neo4jUtil.cds(cql2);
}


/**
    * 修改指定用户与所有设备的关系
  * @param name 用户名
  * @param expire 设备借用时间
  */
@PutMapping("/updateU2AllDR")
public void updateU2AllDR(@RequestParam("name")String name,
  @RequestParam("expire")String expire){

String cql1 = "match(u:User{name:\""+name+"\"})-[r:Borrow]->(d) set r.date=\""+expire+"\"";
  String cql2 = "match(u:User{name:\""+name+"\"})<-[r:Belong]-(d) set  r.date=\""+expire+"\"";
  Neo4jUtil.cds(cql1);
  Neo4jUtil.cds(cql2);
}


/**
    * 修改指定设备与所有用户的关系
  * @param deviceId 设备号
  * @param expire 设备借用时间
  */
@PutMapping("/updateD2AllUR")
public void updateD2AllUR(@RequestParam("deviceId")String deviceId,
  @RequestParam("expire")String expire){

String cql1 = "match(u)-[r:Borrow]->(d:Device{deviceId:\""+deviceId+"\"}) set r.date=\""+expire+"\"";
  String cql2 = "match(u)<-[r:Belong]-(d:Device{deviceId:\""+deviceId+"\"}) set  r.date=\""+expire+"\"";
  Neo4jUtil.cds(cql1);
  Neo4jUtil.cds(cql2);
}



}


你可能感兴趣的:(DataBase,neo4j,数据库,java)