Java版本:jdk1.8.0_121

Neo4j版本:neo4j-community-3.5.22

相关依赖如下:

<neo4j.version>3.5.22</neo4j.version>
<neo4j.graph.version>3.5.22</neo4j.graph.version>
<neo4j.driver.version>4.1.1</neo4j.driver.version>

<!-- Neo4j依赖包 -->
<dependency>
    <groupId>org.neo4j</groupId>
    <artifactId>neo4j</artifactId>
    <version>${neo4j.version}</version>
</dependency>

<!-- Neo4jGraph依赖包 -->
<dependency>
    <groupId>org.neo4j</groupId>
    <artifactId>neo4j-graph-algo</artifactId>
    <version>${neo4j.graph.version}</version>
</dependency>

<!-- Neo4jDriver依赖包 -->
<dependency>
    <groupId>org.neo4j.driver</groupId>
    <artifactId>neo4j-java-driver</artifactId>
    <version>${neo4j.driver.version}</version>
</dependency>

相关配置如下

@Configuration
public class Neo4jConfiguration {

    @Value("${spring.data.neo4j.storedir}")
    private String neo4jStoreDir = null;

    @Bean("graphDatabaseService")
    public GraphDatabaseService graphDatabaseService() {
        return new GraphDatabaseFactory().newEmbeddedDatabase(new File(neo4jStoreDir));
    }

}

相关示例代码如下:

import org.neo4j.graphdb.*;
import org.neo4j.graphdb.factory.GraphDatabaseBuilder;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;

import java.io.File;
import java.net.URL;
import java.util.Map;
import java.util.function.Consumer;

public class GraphUtils {

    public static GraphDatabaseService createGraphDatabaseService(String neo4jStoreDir) throws RuntimeException {
        return new GraphDatabaseFactory().newEmbeddedDatabase(new File(neo4jStoreDir));
    }

    public static GraphDatabaseService createGraphDatabaseService(String neo4jStoreDir, String fileName) throws RuntimeException {
        return new GraphDatabaseFactory().newEmbeddedDatabaseBuilder(new File(neo4jStoreDir))
            .loadPropertiesFromFile(fileName).newGraphDatabase();
    }

    public static GraphDatabaseService createGraphDatabaseService(String neo4jStoreDir, URL url) throws RuntimeException {
        return new GraphDatabaseFactory().newEmbeddedDatabaseBuilder(new File(neo4jStoreDir))
            .loadPropertiesFromURL(url).newGraphDatabase();
    }

    /**
     * @param neo4jStoreDir
     * @param configProps
     * dbms.security.procedures.unrestricted=apoc.*
     * @return
     * @throws RuntimeException
     */
    public static GraphDatabaseService createGraphDatabaseService(String neo4jStoreDir, Map<String, String> configProps) throws RuntimeException {
        GraphDatabaseBuilder graphDatabaseBuilder = new GraphDatabaseFactory().newEmbeddedDatabaseBuilder(new File(neo4jStoreDir));
        for (Map.Entry<String, String> entry : configProps.entrySet()) {
            graphDatabaseBuilder.setConfig(entry.getKey(), entry.getValue());
        }
        return graphDatabaseBuilder.newGraphDatabase();
    }

    public static Node createNode(GraphDatabaseService graphDatabaseService, Object... properties) throws RuntimeException {
        return setProperties(graphDatabaseService.createNode(), properties);
    }

    public static Node createNode(GraphDatabaseService graphDatabaseService, Label label, Object... properties) throws RuntimeException {
        return setProperties(graphDatabaseService.createNode(label), properties);
    }

    public static <T extends PropertyContainer> T setProperties(T entity, Object[] properties) throws RuntimeException {
        for (int i = 0, len = properties.length; i < len; i++) {
            String key = properties[i++].toString();
            Object value = properties[i];
            entity.setProperty(key, value);
        }
        return entity;
    }

    public static void query(String neo4jStoreDir, String query, Consumer<Result> consumer) {
        GraphDatabaseService graphDatabaseService = createGraphDatabaseService(neo4jStoreDir);
        try (Transaction tx = graphDatabaseService.beginTx()) {
            consumer.accept(graphDatabaseService.execute(query));
            tx.success();
        }
        GraphUtils.shutdownGraphDatabaseService(graphDatabaseService);
    }

    public static void query(String neo4jStoreDir, String query, Map<String, Object> parameters, Consumer<Result> consumer) {
        GraphDatabaseService graphDatabaseService = createGraphDatabaseService(neo4jStoreDir);
        try (Transaction tx = graphDatabaseService.beginTx()) {
            consumer.accept(graphDatabaseService.execute(query, parameters));
            tx.success();
        }
        GraphUtils.shutdownGraphDatabaseService(graphDatabaseService)
作者:Jeebiz  创建时间:2023-12-28 12:56
最后编辑:Jeebiz  更新时间:2023-12-28 12:58