我正在尝试理解 * Neo4j * 的 * Traversal API *,但我遇到了依赖关系问题:
Cannot resolve method 'findNode' in 'Transaction'
还有:
Cannot resolve method 'traversalDescription' in 'Transaction'
下面是我的代码(由于依赖关系,我仍然不确定它是否工作-任何与此问题无关的提示都将受到赞赏):
import org.neo4j.driver.Driver;
import org.neo4j.driver.AuthTokens;
import org.neo4j.driver.GraphDatabase;import org.neo4j.driver.Session;
import org.neo4j.graphdb.*;
import org.neo4j.graphdb.traversal.*;
public class Main {
public static void main(String[] args) {
Driver driver = GraphDatabase.driver("bolt://localhost:7687", AuthTokens.basic("neo4j", "password"));
try (Session session = driver.session()) {
// Start a new transaction
try (Transaction tx = (Transaction) session.beginTransaction()) {
// Get the traversal description
TraversalDescription traversal = tx.traversalDescription()
.depthFirst()
.uniqueness(Uniqueness.NODE_GLOBAL);
Node startNode = tx.findNode(Label.label("Node"), "id", 0);
// Traverse the graph
Traverser traverser = traversal.traverse(startNode);
for (Path path : traverser) {
Node node = path.endNode();
System.out.println("Node id: " + node.getProperty("id"));
}
tx.success();
}
} finally {
driver.close();
}
}
}
和pom.xml
文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>groupId</groupId>
<artifactId>Traversal</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ZBD</name>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.neo4j.driver</groupId>
<artifactId>neo4j-java-driver</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-graphdb-api</artifactId>
<version>3.0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
我已经尝试使用neo4j repository on GitHub中的示例,但是依赖性仍然是一个问题。
2条答案
按热度按时间8cdiaqws1#
您的库和代码现在已经很旧了,下面是一个包含新版本的Maven POM文件:
并且不要使用遍历框架,看起来现在建议使用Cypher。而且更容易找到示例。请看这里:https://neo4j.com/docs/java-reference/current/java-embedded/cypher-java/
n9vozmp42#
你把两种东西混在一起了。
neo4j-java-driver
用于与远程服务器通信,仅允许执行密码查询:遍历Java API通过扩展或嵌入Neo4j在示例中使用。不能将Java驱动程序和遍历API混合使用。
注意,这里的事务是从
db
(GraphDatabaseService
的注入示例)开始的,而不是从Java驱动程序的Session
开始的。旁注:
neo4j-graphdb-api
的版本3.0.1非常旧。Neo4j 3.0.1已到生命周期终止日期a long time ago.