Skip to content

Commit

Permalink
Add in Java demo
Browse files Browse the repository at this point in the history
  • Loading branch information
rgward committed Feb 21, 2019
1 parent 02dce89 commit 04cd0db
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 1 deletion.
12 changes: 12 additions & 0 deletions demos/sqlserver/Java/InputRow.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package pkg;

//This object represents one input row
public class InputRow {
public final int id;
public final String text;

public InputRow(final int id, final String text) {
this.id = id;
this.text = text;
}
}
19 changes: 19 additions & 0 deletions demos/sqlserver/Java/Javangramdemo.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
USE JavaTest
GO
DECLARE @myClassPath nvarchar(50)
DECLARE @n int
--This is where you store your classes or jars.
--Update this to your own classpath
SET @myClassPath = N'C:\java'
--This is the size of the ngram
SET @n = 3
EXEC sp_execute_external_script
@language = N'Java'
, @script = N'pkg.Ngram.getNGrams'
, @input_data_1 = N'SELECT id, text FROM reviews'
, @parallel = 0
, @params = N'@CLASSPATH nvarchar(50), @param1 INT'
, @CLASSPATH = @myClassPath
, @param1 = @n
with result sets ((ID int, ngram varchar(20)))
GO
91 changes: 91 additions & 0 deletions demos/sqlserver/Java/Ngram.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
//We will package our classes in a package called pkg
//Packages are option in Java-SQL, but required for this sample.
package pkg;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class Ngram {

//Required: This is only required if you are passing data in @input_data_1
//from SQL Server in sp_execute_external_script
public static int[] inputDataCol1 = new int[1];
public static String[] inputDataCol2 = new String[1];

//Required: Input null map. Size just needs to be set to "1"
public static boolean[][] inputNullMap = new boolean[1][1];

//Required: Output data columns returned back to SQL Server
public static int[] outputDataCol1;
public static String[] outputDataCol2;

//Required: Output null map. Is populated with true or false values
//to indicate nulls
public static boolean[][] outputNullMap;

//Optional: This is only required if parameters are passed with @params
// from SQL Server in sp_execute_external_script
// n is giving us the size of ngram substrings
public static int param1;

//Optional: The number of rows we will be returning
public static int numberOfRows;

//Required: Number of output columns returned
public static short numberOfOutputCols;

/*Java main method - Only for testing purposes outside of SQL Server
public static void main(String... args) {
//getNGrams();
}*/

//This is the method we will be calling from SQL Server
public static void getNGrams() {

System.out.println("inputDataCol1.length= "+ inputDataCol1.length);
if (inputDataCol1.length == 0 ) {
// TODO: Set empty return
return;
}
//Using a stream to "loop" over the input data inputDataCol1.length. You can also use a for loop for this.
final List<InputRow> inputDataSet = IntStream.range(0, inputDataCol1.length)
.mapToObj(i -> new InputRow(inputDataCol1[i], inputDataCol2[i]))
.collect(Collectors.toList());


//Again, we are using a stream to loop over data
final List<OutputRow> outputDataSet = inputDataSet.stream()
// Generate ngrams of size n for each incoming string
// Each invocation of ngrams returns a list. flatMap flattens
// the resulting list-of-lists to a flat list.
.flatMap(inputRow -> ngrams(param1, inputRow.text).stream().map(s -> new OutputRow(inputRow.id, s)))
.collect(Collectors.toList());

//Print the outputDataSet
System.out.println(outputDataSet);

//Set the number of rows and columns we will be returning
numberOfOutputCols = 2;
numberOfRows = outputDataSet.size();
outputDataCol1 = new int[numberOfRows]; // ID column
outputDataCol2 = new String[numberOfRows]; //The ngram column
outputNullMap = new boolean[2][numberOfRows];// output null map

//Since we don't have any null values, we will populate all values in the outputNullMap to false
IntStream.range(0, numberOfRows).forEach(i -> {
final OutputRow outputRow = outputDataSet.get(i);
outputDataCol1[i] = outputRow.id;
outputDataCol2[i] = outputRow.ngram;
outputNullMap[0][i] = false;
outputNullMap[1][i] = false;
});
}

// Example: ngrams(3, "abcde") = ["abc", "bcd", "cde"].
private static List<String> ngrams(int n, String text) {
return IntStream.range(0, text.length() - n + 1)
.mapToObj(i -> text.substring(i, i + n))
.collect(Collectors.toList());
}
}
15 changes: 15 additions & 0 deletions demos/sqlserver/Java/OutputRow.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package pkg;

//This object represents one output row
public class OutputRow {
public final int id;
public final String ngram;

public OutputRow(final int id, final String ngram) {
this.id = id;
this.ngram = ngram;
}

@Override
public String toString() { return id + ":" + ngram; }
}
4 changes: 4 additions & 0 deletions demos/sqlserver/Java/buildjava8.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
rmdir /s /q c:\java\pkg
mkdir c:\java\pkg
"C:\Program Files\Java\jdk1.8.0_181\bin\javac" Ngram.java InputRow.java OutputRow.java
copy *.class c:\java\pkg
27 changes: 27 additions & 0 deletions demos/sqlserver/Java/javasetup.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-- Enable external scripts.
-- No restart is required in SQL Server 2019!
EXEC sp_configure 'ahow advanced options', 1
GO
RECONFIGURE
EXEC sp_configure 'external scripts enabled', 1
GO
RECONFIGURE
GO
-- Create a database and populate some data
--
DROP DATABASE IF EXISTS JavaTest
GO
CREATE DATABASE JavaTest
GO
USE JavaTest
GO
DROP TABLE IF exists reviews;
GO
CREATE TABLE reviews(
id int NOT NULL,
"text" nvarchar(30) NOT NULL)

INSERT INTO reviews(id, "text") VALUES (1, 'AAA BBB CCC DDD EEE FFF')
INSERT INTO reviews(id, "text") VALUES (2, 'GGG HHH III JJJ KKK LLL')
INSERT INTO reviews(id, "text") VALUES (3, 'MMM NNN OOO PPP QQQ RRR')
GO
25 changes: 25 additions & 0 deletions demos/sqlserver/Java/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# SQL Server Demo for the Java Extension

This is a demo to show the Java Extension capability in SQL Server 2019 and later. This demo shows how to run Java code on SQL Server on Windows. The same code will work with SQL Server Java on Linux. Demo for this scenario coming soon.

## Requirements

1. Install SQL Server 2019 CTP 2.0 on Windows or later following the guidance on this page for Java.

https://docs.microsoft.com/en-us/sql/advanced-analytics/java/extension-java?view=sqlallproducts-allversions#install-on-windows

2. Follow the rest of the steps on this page to install all dependencies

https://docs.microsoft.com/en-us/sql/advanced-analytics/java/extension-java?view=sqlallproducts-allversions#install-on-windows

## Demo Steps

1. I used the sample code from https://docs.microsoft.com/en-us/sql/advanced-analytics/java/java-first-sample?view=sqlallproducts-allversions and included the 3 Java class source: **InputRow.java**, **OutputRow.java**, **Ngram.java**

2. I compiled the Java classes using the script **buildjava8.cmd** with Java 8 SDK (so it will be compatible with Linux). The compiled code is placed into C:\java\pkg. You can adjust this to whatever directory you need but you will need to make edits in the following steps.

3. You need to setup permissions for the folder for your compiled classes. Use the instructions per the documentation at https://docs.microsoft.com/en-us/sql/advanced-analytics/java/java-first-sample?view=sqlallproducts-allversions#6---set-permissions

4. Run the **javasetup.sql** script to create the database and objects.

5. Run the **javangramdemo.sql** to execute the Java code. This script assumes the class files are in c:\java\pkg. I recommend you keep the pkg subdirectory. If you need to have the classes in a different parent folder, modify this part of the script SET @myClassPath = N'C:\java'
6 changes: 5 additions & 1 deletion demos/sqlserver/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ This is a demo for the Intelligent Query Processing feature of SQL Server 2019 o

## staticmask

This is a demo for the Static Data Masking feature with SSMS 18.0 and SQL Server
This is a demo for the Static Data Masking feature with SSMS 18.0 and SQL Server

## java

This is a demo to show the Java extensibility capabilities in SQL Server 2019

0 comments on commit 04cd0db

Please sign in to comment.