Neo4j基础操作

最后更新:2024-05-10 05:30:01 | 状态:未完成
//以下说到的标签相当于关系型数据库中的表
//原生CQL创建节点
service.execute("create (d:Dept{name:'财务部', leader:'张三'})");
//创建多标签(表)节点
service.execute("CREATE (e:User:Employee{NAME:'张三',AGE:20})");
//创建多节点
service.execute("CREATE (e:User{NAME:'张三',AGE:20})");

//根据DataRow创建节点
DataRow r = new Neo4jDataRow();
r.put("NAME","ZH");
r.put("AGE", 20);
long qty = service.insert("User", r);
log.warn(LogUtil.format("[DataRow单行插入][影响行数:{}][生成主键:{}]", 36), qty, r.getId());
Assertions.assertEquals(qty, 1);
Assertions.assertNotNull(r.getId());

//创建多标签(表)节点,用:将多个标签分隔
service.insert("User:Employee", r);
log.warn(LogUtil.format("[DataRow多标签单行插入][影响行数:{}][生成主键:{}]", 36), qty, r.getId());
Assertions.assertEquals(qty, 1);
Assertions.assertNotNull(r.getId());

//根据entity创建节点
User u = new User("u1", 10);
service.insert(u);
log.warn(LogUtil.format("[Entity单行插入][影响行数:{}][生成主键:{}]", 36), qty, u.getId());
Assertions.assertEquals(qty, 1);
Assertions.assertNotNull(u.getId());

//临时指定标签(表)
service.insert("set_user",u);
log.warn(LogUtil.format("[Entity单行插入][临时指定目标表][影响行数:{}][生成主键:{}]", 36), qty, u.getId());
Assertions.assertEquals(qty, 1);
Assertions.assertNotNull(u.getId());

//指定多标签(表)
service.insert("User:Employee", u);
log.warn(LogUtil.format("[Entity多标签单行插入][影响行数:{}][生成主键:{}]", 36), qty, u.getId());
Assertions.assertEquals(qty, 1);
Assertions.assertNotNull(u.getId());


//根据DataSet创建节点
DataSet set = new DataSet();
for(int i=1; i<=10; i++){
	DataRow row = new Neo4jDataRow();
	row.put("NAME", "user"+i);
	row.put("AGE", i*5);
	set.add(row);
}
qty = service.insert("Batch", set);
log.warn(LogUtil.format("[DataSet批量插入][影响行数:{}][生成主键:{}]", 36), qty, set.getStrings("ID"));
Assertions.assertEquals(qty, set.size());
Assertions.assertEquals(set.getStrings("ID").size(), set.size());

List<User> users = new ArrayList<>();
for(int i=1; i<=10; i++){
	users.add(new User("euser"+i, i*3));
}
qty = service.insert(users);
log.warn(LogUtil.format("[List<Entity>批量插入][影响行数:{}]", 36), qty);
Assertions.assertEquals(qty, users.size());

qty = service.insert("Batch", (Object) users);
log.warn(LogUtil.format("[List<Entity>批量插入临时指定表][影响行数:{}][users:{}]", 36), qty, BeanUtil.object2json(users));
Assertions.assertEquals(qty, users.size());

//合计
long total = service.count("Dept","leader:张三");
log.warn(LogUtil.format("[合计数量][total:{}]", 36), total);

total = service.count("");
log.warn(LogUtil.format("[整库合计数量][total:{}]", 36), total);

//节点是否存在
boolean exists = service.exists("Dept","leader:张三");
log.warn(LogUtil.format("[检测节点][exists:{}]", 36), exists);

//查询
set = service.querys("match (s:Dept) return s.name, s.qty limit 3 ");
log.warn(LogUtil.format("[原生CQL查询][result:{}]", 36), set.toJSON());

//按条件查询,查询结果不含前缀
set = service.querys("Dept","leader:张三","id:>0");
log.warn(LogUtil.format("[按条件查询][result:{}]", 36), set.toJSON());


//模糊查询
set = service.querys("Dept", "name:%财务%");
log.warn(LogUtil.format("[模糊查询][result:{}]", 36), set.toJSON());
set = service.querys("Dept", "name:%财务");
log.warn(LogUtil.format("[模糊查询][result:{}]", 36), set.toJSON());
set = service.querys("Dept", "name:财务%");
log.warn(LogUtil.format("[模糊查询][result:{}]", 36), set.toJSON());


//按条件分页查询
PageNavi navi = new DefaultPageNavi(3, 10);
set = service.querys("Dept", navi, "leader:张三");
log.warn(LogUtil.format("[按条件分页查询][result:{}]", 36), set.toJSON());

//指定截取
set = service.querys("Dept", 0, 9, "leader:张三");
log.warn(LogUtil.format("[指定截取][result:{}]", 36), set.toJSON());

set = service.querys("match (s:Dept) return s.name limit 3 ");
log.warn(LogUtil.format("[原生CQL][result:{}]", 36), set.toJSON());


set = service.querys("match (s:Dept) return s, s.name as name limit 10 ");

log.warn(LogUtil.format("[原生CQL][result:{}]", 36), set.toJSON());

set = service.querys("MATCH (e:Person)-[k:KNOWS]-(f) where e.name='Johan' RETURN e,k, f");
//返回{e={},k={},f={}}
log.warn(LogUtil.format("[原生CQL][result:{}]", 36), set.toJSON());

//按条件删除
service.delete("Dept", "name","财务部");


set = service.querys("Batch", 0, 9);
//根据ID删除
service.delete("Batch", set);
首页 最近更新 搜索 提交 回复