设置插件项目
您可以创建一个项目以通过用户自定义过程扩展 Neo4j,编译项目并将过程部署到 Neo4j 实例中。相同的步骤也适用于用户自定义函数。
|
本节中描述的示例可在 GitHub(neo4j-examples/neo4j-procedure-template) 上获取。 |
使用 Maven 设置项目
可以以任何方式建立项目,只要能够编译过程并生成 JAR 文件即可。
<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>org.neo4j.example</groupId>
<artifactId>procedure-template</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Neo4j Procedure Template</name>
<description>A template project for building a Neo4j Procedure</description>
<properties>
<java.version>21</java.version>
<maven.compiler.release>${java.version}</maven.compiler.release>
<neo4j.version>2026.03.1</neo4j.version>
</properties>
构建依赖
添加一个依赖段,其中包含过程和函数 API——这些 API 在运行时被过程和函数使用。
依赖范围设为 provided,因为过程部署到 Neo4j 实例后,该依赖由 Neo4j 提供。如果向项目中加入非 Neo4j 的依赖,通常应将其范围设为 compile。
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j</artifactId>
<version>${neo4j.version}</version>
<scope>provided</scope>
</dependency>
添加进行过程测试所必需的依赖。
Neo4j Harness,一个用于启动轻量级 Neo4j 实例的工具。它可以在部署了特定过程或函数的情况下启动 Neo4j,从而大幅简化测试。
Neo4j Java Driver,用于发送调用过程或函数的 Cypher 语句。
JUnit,常用的 Java 测试框架。
<dependency>
<groupId>org.neo4j.test</groupId>
<artifactId>neo4j-harness</artifactId>
<version>${neo4j.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.neo4j.driver</groupId>
<artifactId>neo4j-java-driver</artifactId>
<version>6.0.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.1</version>
<scope>test</scope>
</dependency>
构建步骤
Maven 为构建项目将执行的步骤。
目标是先 编译 源代码,然后 打包 成可部署到 Neo4j 实例的 JAR。
使用 Maven Shade 插件来打包已编译的过程。除非依赖范围被设为 test 或 provided,否则会把所有依赖一起打入包中。
一旦过程被部署到每个 Neo4j 实例的 plugins 目录并且实例重新启动,过程即可使用。
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>21</source>
<target>21</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
实现自定义种子提供者
Seed from URI 功能允许从类路径动态发现额外的种子提供者实现。
种子提供者应使用 Java 实现;本指南提供了使用 Maven 作为构建工具的实现步骤。
建立 Maven 项目
加入此依赖以使用 Neo4j 提供的 API 进行构建
<dependency>
<groupId>com.neo4j</groupId>
<artifactId>neo4j-dbms-api</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
实现 Java 类
DatabaseSeedProvider
import com.neo4j.dbms.seeding.DatabaseSeedProvider;
public class CustomDatabaseSeedProvider implements DatabaseSeedProvider {
@Override
public boolean matches(String uri) {
// Return true if uri is supported by this
// provider.
}
@Override
public InputStream stream(
String uri,
Map<String, Object> options) throws IOException {
// This method should obtain an input stream in an
// implementation specific way.
}
@Override
public void inject(Dependencies dependencies) {
// This method should provide implementation
// specific dependencies to the provider.
}
public static class CustomDependencies implements Dependencies {
@Override
public <T> T resolveDependency(Class<T> type) {
// This method should resolve dependencies
// required by the provider.
}
}
}
要实现自定义数据库种子提供者,必须在顶层 DatabaseSeedProvider 接口上定义三个方法
-
一个用于匹配提供者能够管理的 URI 的方法。
-
一个从指定 URI 流式传输备份或转储的方式。
-
一个在提供者中注入依赖的方式。
此外,还必须在嵌套的 Dependencies 接口上实现一个方法,以解析种子提供者实现所需的任何依赖。
通常,match 方法会检查 URI 的 scheme(即第一个冒号之前的部分),以判断是否支持该 URI,例如 file、http、https 等。
stream 方法应实现特定 scheme 的方式来获取备份或转储的输入流。
实现特定的种子配置可以通过在 CREATE DATABASE 命令中使用 seedConfig 指定的选项传递。
请注意,CREATE DATABASE 命令是企业版功能。