SQL Server 的用于 Java 的 Microsoft 扩展性 SDKMicrosoft Extensibility SDK for Java for SQL Server

适用于:Applies to: 是SQL Server 2019 (15.x)SQL Server 2019 (15.x)yesSQL Server 2019 (15.x)SQL Server 2019 (15.x)适用于:Applies to: 是SQL Server 2019 (15.x)SQL Server 2019 (15.x)yesSQL Server 2019 (15.x)SQL Server 2019 (15.x)

了解如何才能使用用于 Java 的 Microsoft 扩展性 SDK 为 SQL Server 实现 Java 程序。Learn how you can implement a Java program for SQL Server using the Microsoft Extensibility SDK for Java. SDK 是用于与 SQL Server 交换数据并从 SQL Server 执行 Java 代码的 Java 语言扩展的接口。The SDK is an interface for the Java language extension that is used to exchange data with SQL Server and to execute Java code from SQL Server.

SDK 作为 SQL Server 2019 候选发布版 1 的一部分安装在 Windows 和 Linux 上:The SDK is installed as part of SQL Server 2019 Release Candidate 1 on both Windows as Linux:

  • Windows 上的默认安装路径:[实例安装主目录]\MSSQL\Binn\mssql-java-lang-extension.jarDefault installation path on Windows: [instance installation home directory]\MSSQL\Binn\mssql-java-lang-extension.jar
  • Linux 上的默认安装路径:/opt/mssql/lib/mssql-java-lang-extension.jarDefault installation path on Linux: /opt/mssql/lib/mssql-java-lang-extension.jar

该代码是开放源代码,可在 SQL Server 语言扩展 GitHub 存储库中找到。The code is open source and can be found on the SQL Server Language Extensions GitHub repository.

实现要求Implementation requirements

SDK 接口定义了一组要求,SQL Server 需要满足这些要求才能与 Java 运行时通信。The SDK interface defines a set of requirements that need to be fulfilled for SQL Server to communicate with the Java runtime. 若要使用 SDK,需要在主类中遵循一些实现规则。To use the SDK, you need to follow some implementation rules in your main class. SQL Server 随后可以执行 Java 类中的特定方法,并使用 Java 语言扩展交换数据。SQL Server can then execute a specific method in the Java class and exchange data using the Java language extension.

有关如何使用 SDK 的示例,请参阅教程:在 Java 中使用正则表达式 (regex) 搜索字符串For an example of how you can use the SDK, see Tutorial: Search for a string using regular expressions (regex) in Java.

SDK 类SDK Classes

SDK 由三个类组成。The SDK consists of three classes.

定义 Java 扩展用于与 SQL Server 交换数据的接口的两个抽象类:Two abstract classes that define the interface the Java extension uses to exchange data with SQL Server:

  • AbstractSqlServerExtensionExecutorAbstractSqlServerExtensionExecutor
  • AbstractSqlServerExtensionDatasetAbstractSqlServerExtensionDataset

第三个类是帮助程序类,包含数据集对象的实现。The third class is a helper class, which contains an implementation of a data set object. 它是可用使用的可选类,这样可以更轻松地开始使用。It is an optional class you can use, which makes it easier to get started. 还可以改为使用你自己对这样一个类的实现。You can also use your own implementation of such a class instead.

  • PrimitiveDatasetPrimitiveDataset

下面提供 SDK 中每个类的说明。Below you will find descriptions of each class in the SDK. SDK 类的源代码在 SQL Server 语言扩展 GitHub 存储库中提供。The source code of the SDK classes is available in the SQL Server Language Extensions GitHub repository.

类:AbstractSqlServerExtensionExecutorClass: AbstractSqlServerExtensionExecutor

抽象类 AbstractSqlServerExtensionExecutor 包含由 SQL Server 的 Java 语言扩展用于执行 Java 代码的接口。The abstract class AbstractSqlServerExtensionExecutor contains the interface used to execute Java code by the Java language extension for SQL Server.

主 Java 类需要从此类继承。Your main Java class needs to inherit from this class. 从此类继承表示类中有一些需要在自己的类中实现的特定方法。Inheriting from this class means that there are certain methods in the class you need to implement in your own class.

若要从此抽象类继承,请在类声明中使用抽象类名进行扩展:To inherit from this abstract class, you extend with the abstract class name in the class declaration:

public class <MyClass> extends AbstractSqlServerExtensionExecutor {}

主类至少需要实现 execute(...) 方法。At a minimum, your main class needs to implement the execute(...) method.

execute 方法Method execute

execute 方法是通过 Java 语言扩展从 SQL Server 进行调用,以便从 SQL Server 调用 Java 代码的方法。The execute method is the method that is called from SQL Server via the Java language extension, to invoke Java code from SQL Server. 这是一个关键方法,其中包含要从 SQL Server 执行的主要操作。It is a key method where you include the main operations you wish to execute from SQL Server.

若要从 SQL Server 向 Java 传递方法参数,请在 sp_execute_external_script 中使用 @param 参数。To pass method arguments to Java from SQL Server, use the @param parameter in sp_execute_external_script. execute 方法通过此方式获取其参数。The method execute takes its arguments that way.

public AbstractSqlServerExtensionDataset execute(AbstractSqlServerExtensionDataset input, LinkedHashMap<String, Object> params)  {}

init 方法Method init

Init 方法在构造函数之后,execute 方法之前执行。The init method is executed after the constructor and before the execute method. 需要在 execute(...) 之前执行的任何操作都可以在此方法中进行。Any operations that need to be performed prior to execute(...) can be done in this method.

public void init(String sessionId, int taskId, int numtask) {}

类:AbstractSqlServerExtensionDatasetClass: AbstractSqlServerExtensionDataset

抽象类 AbstractSqlServerExtensionDataset 包含用于处理 Java 扩展所使用的输入和输出数据的接口。The abstract class AbstractSqlServerExtensionDataset contains the interface for handling input and output data used by the Java extension.

类:PrimitiveDatasetClass: PrimitiveDataset

类 PrimitiveDataset 是将简单类型存储为基元数组的 AbstractSqlServerExtensionDataset 的实现。The class PrimitiveDataset is an implementation of AbstractSqlServerExtensionDataset that stores simple types as primitives arrays.

它在 SDK 中仅作为可选的帮助程序类而提供。It is provided in the SDK simply as an optional helper class. 如果不使用此类,则需要实现从 AbstractSqlServerExtensionDataset 继承的自己的类。If you don't use this class, you need to implement your own class that inherits from AbstractSqlServerExtensionDataset.

后续步骤Next steps