java查询neo4j_neo4j的一些复杂查询(java)

查询重复的关系,并删除

String url ="bolt://192.168.126.xx:7687";

String userName = "neo4j";

String passWord = "xxx";

// 使用 basic authentication方式创建连接

try {

Driver driver = GraphDatabase.driver(url, AuthTokens.basic(userName, passWord), Config.builder().withMaxConnectionLifetime(30, TimeUnit.MINUTES).withMaxConnectionPoolSize(50).withConnectionAcquisitionTimeout(2, TimeUnit.MINUTES).build());

Session session = driver.session();

//查询关系类型为1种,且关系数量>1的

String cond="match (a)-[r]->(b) with a,b,count(distinct type(r)) as tr, count(r) as c where c>1 and tr=1 return a, b, c,tr limit 10";

List rst= session.run(cond).list();

while(rst.size()>0) {

for(Record r:rst) {

NodeValue a=(NodeValue)r.get("a");

NodeValue b=(NodeValue)r.get("b");

System.out.println(r);

cond="match (a)-[r]->(b) with a,b,r,(type(r)) as ct where id(a)="+a.asNode().id()+ " and id(b)="+b.asNode().id()+" match (a)-[r]->(b) with distinct ct,count(r) as cr where cr>1 return ct,cr";

List typeCountRst= session.run(cond).list();

for(Record typeCount:typeCountRst) {

cond="match (a)-[r]->(b) with a,b,r where id(a)="+a.asNode().id()+ " and id(b)="+b.asNode().id()+" and type(r)='"+typeCount.get("ct").asString()+"' return a,b,r";

List dupRst= session.run(cond).list();

for(int i=0;dupRst.size()>1&&i

Record rel=dupRst.get(i);

List dupRst2= session.run("match (a)-[r]->(b) where id(r)="+rel.get("r").asRelationship().id()+" delete r").list();

System.out.println(dupRst2);

}

}

System.out.println(typeCountRst);

}

cond="match (a)-[r]->(b) with a,b,count(distinct type(r)) as tr, count(r) as c where c>1 and tr=1 return a, b, c,tr limit 10";

rst= session.run(cond).list();

}

System.out.println("end ...");

} catch (Exception e) {

log.error("图数据库连接异常",e);

}

评论:

fbe0a9b77004d9d11301d8211b7d3880.png

提交

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值