In this article, we will discuss SQL Server 2019 new feature—Java Language Extensions. With the advent of SQL Server 2019, Microsoft always strives to expand its footprint on the capabilities of MLS (Machine Learning Services). It builds a deeper integration between the data-platform and data science under the data science umbrella. The Microsoft SQL Server 2019 MLS extensibility framework provides a solid base for allowing extensions in R, Python, and now Java.
In this article, we will discuss the following:
- Understand Machine Language Service
- Discuss JAVA SDK installation and configuration
- Configure Java Language Extension API
- Prepare the sample JAVA code using Eclipse IDE
- Deploy JAR file in SQL Server
- Run the JAVA program
- And more…
Get Started
In the last three major releases of SQL Server, SQL Server 2016, SQL Server 2017, and SQL Server 2019—integrated a framework that supports running an external programming language capability in the database server. SQL Server 2016 introduced R language; SQL Server 2017 supported Python and continued to support R. These two releases coined as the biggest game-changer in the field of data science, and these are categorized under MLS (Machine Language Service). Now, Microsoft strategically added another external language support under the Machine Language (ML). The trend continued in SQL Server 2019 with an addition of JAVA to the list of externally supported languages. In addition, SQL Server 2019 in-houses a wide range of new features and enhancements to the existing feature.
The extensibility framework APIs reference for running external scripts such as R and Python are supported in SQL Server 2017. This extensibility framework API now exposed to run JAVA programs using the sp_execute_external_script system stored procedure.
Pre-requisites
As we know that to run the Java program, you need JVM (Java Virtual Machine).
The pre-requisites to writing and running the Java program are as follows:
- Java SE Development Kit( JDK)
- Java Runtime Environment (JRE) is one of the components of the Java SDK. It enables and provides a platform to Java programs
Install JAVA SDK
In this section, we will discuss the steps to install and configure JAVA.
-
Click the installation library
-
After the installation, configure the environment variable. Follow the step-by-step details to configure environment variables:
- Go to computer properties
- Click the Advanced system settings
- Click Environment Variables
-
Specify the value of the PATH environment variable
- C:\Program Files\Zulu\zulu-8\bin\
- C:\Program Files\Zulu\zulu-8\jre\bin\server
Note: By default, SQL Server installs Zulu Open JRE (Java Runtime Environment) version 11.0.3. It is also possible to install the full JDK (Java Development Kit) for compilers and/or any other java distribution of your own choice.
Note: It is highly recommended to use Oracle Java SE and Zulu OpenJDK. In addition, during the installation process, it is advised to use the default Windows Program Files folder.
Configure Java Language Extensions in SQL Server
SQL Server 2019 provides a platform to integrate the Java language extension library to leverage data exchange and communication using Extensibility Framework APIs. The language extensions allow you to extend SQL Server to use external code such as Python, R, and JAVA as part of SQL executions and operations.
Let us enable “external scripts” parameters using sp_configure option. Let us run the following T-SQL to validate the external scripts feature in the SQL Server configuration.
1 |
SELECT * FROM sys.configurations WHERE name= N'external scripts enabled' |
The value “0” indicates in the below output depicts the external scripts are disabled.
Now, run the following command to enable the external scripts
1 2 |
EXEC sp_configure 'external scripts enabled', 1 RECONFIGURE WITH OVERRIDE |
Let us re-run the aforementioned SQL to validate the “external scripts enabled” parameter
In the following output, we can see that R and Python are referenced in the SQL Server.
This indicates that the Java program is not registered. In the next section, we can discuss how to register the Java libraries in SQL Server.
1 |
SELECT * FROM sys.external_language_files |
To register the Java external language, walk-through the following step-by-step instruction.
-
In the SQL Server installation directory, you will find the Microsoft Extensibility SDK—mssql-java-lang-extension.jar. Let us locate the java-lang-extension.zip. In this case, the SQL Server installation folder G:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\Binn. Now, browse the folder and locate the java language extension
-
Copy the java-lang-extension.zip to another folder. In this case, the java-lang-extension.zip is copied to G:\SQL
-
Run the following SQL to register Java external language
1CREATE EXTERNAL LANGUAGE Java FROM (CONTENT = N'G:\SQL\java-lang-extension.zip', FILE_NAME = 'javaextension.dll');In the following output, you can see that the Java external language registered successfully.
-
Let us verify the Java language registration using the following SQL
1SELECT * FROM sys.external_language_filesWe have successfully registered the Java library. We are not done yet….
- As I mentioned above, you still need to reference the library to build a jar file. In this case, I use Eclipse editor to compile and build the java file(JAR file)
Build the JAR file
In this section, let us walk-through step-by-step instructions to build the JAR file. In this case, we will create the Java SQL2019 Demo Package and SQL2019 class.
Let us write the simple Java code in a .class file and save it in the folder G:\SQL.
Note: Ensure you have the full permissions on the folder G:\SQL folder.
Import the following packages in the class:
import com.microsoft.sqlserver.javalangextension.PrimitiveDataset;
import com.microsoft.sqlserver.javalangextension. AbstractSqlServerExtensionExecutor;
- Right-click SQL2019 project and go to the Properties to open the Properties dialog
-
Click Java Build Path on the left navigation tree
- Select the Libraries right
-
Click Add JARs
- Now, browse G:\SQL and select mssql-java-lang-extension.jar
-
Copy the below code and paste it in the editor, or you can also define your own code
123456789101112131415161718192021222324252627package SQL2019Demo;import java.util.LinkedHashMap;import com.microsoft.sqlserver.javalangextension.*;public class sql2019 extends com.microsoft.sqlserver.javalangextension.AbstractSqlServerExtensionExecutor{public sql2019() {executorExtensionVersion = SQLSERVER_JAVA_LANG_EXTENSION_V1;executorInputDatasetClassName = PrimitiveDataset.class.getName();executorOutputDatasetClassName = PrimitiveDataset.class.getName();}public static void main(String[] args) {// TODO Auto-generated method stub}public AbstractSqlServerExtensionDataset execute(AbstractSqlServerExtensionDataset arg0,LinkedHashMap<String, Object> arg1){System.out.println("Welcome to SQL Server 2019");return null;}}Please refer to the editor for more details:
- Next, compile the java program
-
Now, to export the SQL2019Demo project, right-click and select Export
- Select Java
-
Select Runnable JAR file and click Next
-
Type in the export destination and click finish
-
Now, create an EXTERNAL library from your program:
123CREATE EXTERNAL LIBRARY javaSDK FROM (CONTENT = 'G:\SQL\java-lang-extension\mssql-java-lang-extension.jar') WITH (LANGUAGE = 'Java');GOCREATE EXTERNAL LIBRARY test FROM (CONTENT = 'g:\SQL\welcome.jar') WITH (LANGUAGE = 'Java'); -
Let us verify the external references using the following SQL:
1SELECT * FROM sys.external_libraries -
Finally, we are ready to run the Java program in SQL Server. The @script parameter is a combination of the package name and the class name. In this case, the package name is SQL2019Demo, and the Class name is SQL2019
12EXEC sp_execute_external_script @language = N'Java' , @script = N'SQL2019Demo.sql2019'GO
Summary
SQL Server 2019 extensibility language framework provides a platform for allowing extensions in R, Python, and now Java. So far, we discussed Machine Language Service (MLS); Configure JAVA external language; run the JAVA program in SQL Server. In addition, we discussed the use of the Eclipse editor—Integrated Development Environment (IDE) to run and compile the JAVA programs. Also, we discussed the JAVA integration in SQL Server 2019 Machine Learning Services. SQL Server 2019 supports Python, R, and Java. Now, the support of Java language extension in SQL Server 2019 allows data scientists or data engineers to run the precompiled Java programs (JAR) and securely execute JAR code on SQL Server.
Note: SQL Server 2019 supports Java on both Windows and Linux platforms.
You can also refer to the following table to understand more about the Java Language extensions
New feature or update | Details |
The support of Java SDK | It provides a platform and support to run Java programs. Please refer to the following link for more information |
The support of open source | The open-source Microsoft Extensibility SDK for Java is made available in GitHub |
The Supported data types | Please refer to below link to understand the supported data types |
The Java Runtime | The installation library includes Azul Systems Zulu Embedded for Java support is made available in the product |
The Language Extensions | It supports the execution of the external code with the extensibility framework |
https://docs.microsoft.com/sql/language-extensions/language-extensions-overview | |
Register external language references | To register to Java language refer to the below link |
- Stairway to SQL essentials - April 7, 2021
- A quick overview of database audit in SQL - January 28, 2021
- How to set up Azure Data Sync between Azure SQL databases and on-premises SQL Server - January 20, 2021