java 无法解析“Transaction”中的方法“findNode”

qacovj5a  于 2023-01-24  发布在  Java
关注(0)|答案(2)|浏览(165)

我正在尝试理解 * 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中的示例,但是依赖性仍然是一个问题。

8cdiaqws

8cdiaqws1#

您的库和代码现在已经很旧了,下面是一个包含新版本的Maven POM文件:

<?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>5.4.0</version>
        </dependency>
        <dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>neo4j-graphdb-api</artifactId>
            <version>5.3.0</version>
        </dependency>
    </dependencies>
</project>

并且不要使用遍历框架,看起来现在建议使用Cypher。而且更容易找到示例。请看这里:https://neo4j.com/docs/java-reference/current/java-embedded/cypher-java/

n9vozmp4

n9vozmp42#

你把两种东西混在一起了。
neo4j-java-driver用于与远程服务器通信,仅允许执行密码查询:

var authToken = AuthTokens.basic("neo4j", "password");
try (Driver driver = GraphDatabase.driver("bolt://localhost:7687", authToken);
     Session session = driver.session()) {
    var result = session.run("CREATE (n)");
    var summary = result.consume();
    System.out.println(summary.counters().nodesCreated());
}

遍历Java API通过扩展或嵌入Neo4j在示例中使用。不能将Java驱动程序和遍历API混合使用。

try (Transaction tx = db.beginTx()) {
    Node node; // find some node
    for (Path path : tx.traversalDescription()
        .depthFirst()
        .relationships(Rels.KNOWS)
        .uniqueness(Uniqueness.RELATIONSHIP_GLOBAL)
        .traverse(node)) {
        // do something
    }
}

注意,这里的事务是从dbGraphDatabaseService的注入示例)开始的,而不是从Java驱动程序的Session开始的。
旁注:

  • neo4j-graphdb-api的版本3.0.1非常旧。Neo4j 3.0.1已到生命周期终止日期a long time ago.
  • 编译器配置针对Java 8,针对Java 11的速记属性在此处无效

相关问题