首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

基础知识四:利用java取得neo4j中的路径信息

利用java取得neo4j中的路径信息

neo4j中某路径的信息如下:

图片说明:

该路径包含两个关系,类型都是“包含”关系,含有一个属性,名叫“关系”,值为“include”

注意上面的并不是该关系的属性 ,而是和节点一样,neo4j都会为每个关系分配唯一的id

控制台打印的数据如下:

图片说明:打印的关系类型,前驱结点id和后继节点id等一些信息

下面将详细讨论怎样得到上面的数据

1、得到driver

代码段

static Driver driver = null;

public static void getDriver(){

String uri = "Bolt://localhost:7687";

String user = "";//写你自己的neo4j的用户名

String password = "";//写你自己的neo4j的密码

driver = GraphDatabase.driver(uri, AuthTokens.basic(user,password));

}

2、关闭driver

代码段

public static void close(){

if(driver!=null){

driver.close();

}

}

3、得到路径信息

代码段

public static void getPathsInfo(String cypher){

getDriver();

int count = 0;

try(Session session = driver.session()){

//result包含了所有的path

StatementResult result = session.run(cypher);

while(result.hasNext()){

Record record = result.next();

List value = record.values();

for(Value i:value){

Path path = i.asPath();

//处理路径中的关系

Iterator relationships = path.relationships().iterator();

//Iterator nodes = path.nodes().iterator();//得到path中的节点

while(relationships.hasNext()){

count++;

Relationship relationship = relationships.next();

long startNodeId = relationship.startNodeId();

long endNodeId = relationship.endNodeId();

String relType = relationship.type();

System.out.println("关系"+count+": ");

System.out.println("关系类型:"+relType);

System.out.println("from "+startNodeId+"-----"+"to "+endNodeId);

System.out.println("关系属性如下:");

//得到关系属性的健

Iterator relKeys = relationship.keys().iterator();

//这里处理关系属性

while(relKeys.hasNext()){

String relKey = relKeys.next();

String relValue = relationship.get(relKey).asObject().toString();

System.out.println(relKey+"-----"+relValue);

}

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

}

}

}

}

close();

}

代码说明:

这里的Record实则是Path,而每条path包括关系和组成path的节点,在这里我就只取出关系的信息,节点信息通过这句代码取得该路径所包含的节点:

代码段

//Iterator nodes = path.nodes().iterator();//得到path中的节点

(这句代码在上方代码中以注释的形式体现)得到节点后,详细请看这篇文章:

基础知识三:利用java取得neo4j中的节点信息

4、在main函数中调用上面方法

代码段

public static void main(String... args){

String cypher = "match p=shortestPath((n1:数据库课程)-[*..6]-(n2:数据库知识点)) return p";

getPathsInfo(cypher);

}

代码说明:这里的cypher语句返回的是一条最短路径,这里也是针对所有路径情况!所有读者可以自行创建测试数据!

最后,控制台打印的数据如下:

利用java取得neo4j中的路径信息源码浏览地址:

https://github.com/mp2930696631/visualization-neo4j-using-java/blob/master/preparations/GetPaths.java

基础知识:四:利用java取得neo4j中的路径信息

标题

坚持努力,因为最后你会发现

努力了这么久

我怎么舍得放弃

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20180726G0AJXW00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券