-
Notifications
You must be signed in to change notification settings - Fork 835
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Code sample - 23ai Vector Search with JDBC #353
Open
juarezjuniorgithub
wants to merge
5
commits into
oracle-samples:main
Choose a base branch
from
juarezjuniorgithub:23ai-jdbc-vector-search
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8e26291
Code sample - 23ai Vector Search with JDBC
juarezjuniorgithub ce82ff2
indentation adjustments
juarezjuniorgithub 2994de3
try-with-resources
juarezjuniorgithub 6e44bf1
Merge branch 'oracle-samples:main' into 23ai-jdbc-vector-search
juarezjuniorgithub 432273b
remove randomized ID and use autogenerated ID instead
juarezjuniorgithub File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[Oracle AI Vector Search for Java Developers with the Oracle Database 23ai](https://juarezjunior.medium.com/oracle-ai-vector-search-for-java-developers-with-the-oracle-database-23ai-e29321d23fa0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<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>com.oracle.dev.jdbc</groupId> | ||
<artifactId>23ai-jdbc-vector-search</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
<name>23ai-jdbc-vector-search</name> | ||
<description>Oracle AI Vector Search for Java Developers with the Oracle | ||
Database 23ai</description> | ||
<url> | ||
https://juarezjunior.medium.com/oracle-ai-vector-search-for-java-developers-with-the-oracle-database-23ai-e29321d23fa0</url> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<maven.compiler.source>21</maven.compiler.source> | ||
<maven.compiler.target>21</maven.compiler.target> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.oracle.database.jdbc</groupId> | ||
<artifactId>ojdbc11</artifactId> | ||
<version>23.4.0.24.05</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.oracle.database.jdbc</groupId> | ||
<artifactId>ucp</artifactId> | ||
<version>23.4.0.24.05</version> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
<pluginManagement><!-- lock down plugins versions to avoid using Maven | ||
defaults (may be moved to parent pom) --> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-clean-plugin</artifactId> | ||
<version>3.1.0</version> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-site-plugin</artifactId> | ||
<version>3.7.1</version> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-project-info-reports-plugin</artifactId> | ||
<version>3.0.0</version> | ||
</plugin> | ||
<!-- see | ||
http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging --> | ||
<plugin> | ||
<artifactId>maven-resources-plugin</artifactId> | ||
<version>3.0.2</version> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.8.0</version> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-surefire-plugin</artifactId> | ||
<version>2.22.1</version> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-jar-plugin</artifactId> | ||
<version>3.0.2</version> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-install-plugin</artifactId> | ||
<version>2.5.2</version> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-deploy-plugin</artifactId> | ||
<version>2.8.2</version> | ||
</plugin> | ||
</plugins> | ||
</pluginManagement> | ||
</build> | ||
|
||
</project> |
10 changes: 10 additions & 0 deletions
10
java/23ai-jdbc-vector-search/sql/23ai-jdbc-vector-search.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
CREATE USER VECTOR_USER IDENTIFIED BY <YOUR_PASSWORD> QUOTA UNLIMITED ON USERS; | ||
GRANT DB_DEVELOPER_ROLE TO VECTOR_USER; | ||
GRANT CREATE SESSION TO VECTOR_USER; | ||
GRANT SELECT ANY TABLE ON SCHEMA VECTOR_USER TO VECTOR_USER; | ||
GRANT SELECT ANY TABLE ON SCHEMA VECTOR_USER TO VECTOR_USER; | ||
GRANT SELECT ANY TABLE ON SCHEMA VECTOR_USER TO VECTOR_USER; | ||
GRANT SELECT ANY TABLE ON SCHEMA VECTOR_USER TO VECTOR_USER; | ||
ALTER SESSION SET CURRENT_SCHEMA = VECTOR_USER; | ||
CREATE TABLE VECTOR_USER.ORACLE_AI_VECTOR_SEARCH_DEMO (ID NUMBER PRIMARY KEY, VECTOR_DATA VECTOR(3, FLOAT64)); | ||
COMMIT; |
173 changes: 173 additions & 0 deletions
173
...ai-jdbc-vector-search/src/main/java/com/oracle/dev/jdbc/OracleAIVectorSearchWithJava.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
/* | ||
Copyright (c) 2024, Oracle and/or its affiliates. | ||
This software is dual-licensed to you under the Universal Permissive License | ||
(UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License | ||
2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose | ||
either license. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
https://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package com.oracle.dev.jdbc; | ||
|
||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.util.Arrays; | ||
import java.util.Properties; | ||
import java.util.concurrent.ThreadLocalRandom; | ||
|
||
import oracle.jdbc.OracleType; | ||
import oracle.ucp.jdbc.PoolDataSource; | ||
import oracle.ucp.jdbc.PoolDataSourceFactory; | ||
|
||
public class OracleAIVectorSearchWithJava { | ||
|
||
private final static String URL = "<JDBC_CONNECTION_URL>"; | ||
private final static String USERNAME = "<ORACLE_DATABASE_USERNAME>"; | ||
private final static String PASSWORD = "<ORACLE_DATABASE_PASSWORD>"; | ||
|
||
private String insertSql = "INSERT INTO ORACLE_AI_VECTOR_SEARCH_DEMO (ID, VECTOR_DATA) VALUES (?, ?)"; | ||
private String querySql = "SELECT ID, VECTOR_DATA FROM ORACLE_AI_VECTOR_SEARCH_DEMO"; | ||
private String querySqlWithBind = "SELECT ID, VECTOR_DATA FROM ORACLE_AI_VECTOR_SEARCH_DEMO ORDER BY VECTOR_DISTANCE(VECTOR_DATA, ?, DOT)"; | ||
|
||
public static void main(String[] args) throws SQLException { | ||
OracleAIVectorSearchWithJava oracleAIVectorSearch = new OracleAIVectorSearchWithJava(); | ||
oracleAIVectorSearch.execute(); | ||
} | ||
|
||
private void execute() throws SQLException { | ||
|
||
System.out.println("Starting JDBC connection with PooledDataSource..."); | ||
try (Connection conn = getConnectionFromPooledDataSource()) { | ||
System.out.println("Connected to Oracle Database 23ai! " + "\n"); | ||
|
||
System.out.println("Inserting VECTOR with oracle.jdbc.OracleType.VECTOR..."); | ||
insertVector(conn); | ||
System.out.println("VECTOR with oracle.jdbc.OracleType.VECTOR inserted!" + "\n"); | ||
|
||
System.out.println("Inserting VECTOR with oracle.jdbc.OracleType.VARCHAR2..."); | ||
insertVectorWithVarChar2(conn); | ||
System.out.println("VECTOR with oracle.jdbc.OracleType.VARCHAR2 inserted!" + "\n"); | ||
|
||
System.out.println("Inserting VECTOR with Batch API..."); | ||
insertVectorWithBatchAPI(conn); | ||
System.out.println("VECTOR with Batch API inserted!" + "\n"); | ||
|
||
System.out.println("Retrieving VECTOR as double array..."); | ||
retrieveVectorAsArray(conn); | ||
System.out.println("VECTOR retrieved!" + "\n"); | ||
|
||
System.out.println("Retrieving VECTOR as String..."); | ||
retrieveVectorAsString(conn); | ||
System.out.println("VECTOR retrieved!" + "\n"); | ||
|
||
System.out.println("Retrieving VECTOR with bound VECTOR (L2 - Euclidean distance)..."); | ||
retrieveVectorWithBoundVector(conn); | ||
System.out.println("VECTOR retrieved!"); | ||
|
||
} | ||
} | ||
|
||
private void insertVector(Connection connection) throws SQLException { | ||
PreparedStatement insertStatement = connection.prepareStatement(insertSql); | ||
double[] vector = { 1.1, 2.2, 3.3 }; | ||
System.out.println("SQL DML: " + insertSql); | ||
System.out.println("VECTOR to be inserted: " + Arrays.toString(vector)); | ||
insertStatement.setInt(1, randomize()); | ||
insertStatement.setObject(2, vector, OracleType.VECTOR); | ||
insertStatement.executeUpdate(); | ||
} | ||
|
||
private void insertVectorWithVarChar2(Connection connection) throws SQLException { | ||
PreparedStatement insertStatement = connection.prepareStatement(insertSql); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use try with resources in such methods as this code as-is leaks cursors (the statement isn't closed). |
||
double[] vector = { 1.1, 2.2, 3.3 }; | ||
System.out.println("SQL DML: " + insertSql); | ||
System.out.println("VECTOR to be inserted: " + Arrays.toString(vector)); | ||
insertStatement.setInt(1, randomize()); | ||
insertStatement.setObject(2, Arrays.toString(vector), OracleType.VARCHAR2); | ||
insertStatement.executeUpdate(); | ||
} | ||
|
||
private void insertVectorWithBatchAPI(Connection connection) throws SQLException { | ||
double[][] vectors = { { 1.1, 2.2, 3.3 }, { 1.3, 7.2, 4.3 }, { 5.9, 5.2, 7.3 } }; | ||
System.out.println("SQL DML: " + insertSql); | ||
System.out.println("VECTORs to be inserted as a batch: " + Arrays.toString(vectors[0]) + ", " | ||
+ Arrays.toString(vectors[1]) + ", " + Arrays.toString(vectors[2])); | ||
try (PreparedStatement insertStatement = connection.prepareStatement(insertSql)) { | ||
for (double[] vector : vectors) { | ||
insertStatement.setInt(1, randomize()); | ||
insertStatement.setObject(2, vector, OracleType.VECTOR); | ||
insertStatement.addBatch(); | ||
} | ||
insertStatement.executeBatch(); | ||
} | ||
} | ||
|
||
private void retrieveVectorAsArray(Connection connection) throws SQLException { | ||
PreparedStatement queryStatement = connection.prepareStatement(querySql); | ||
System.out.println("SQL DML: " + querySql); | ||
ResultSet resultSet = queryStatement.executeQuery(); | ||
double[] vector = null; | ||
while (resultSet.next()) { | ||
vector = resultSet.getObject(2, double[].class); | ||
} | ||
System.out.println("Retrieved VECTOR: " + Arrays.toString(vector)); | ||
} | ||
|
||
private void retrieveVectorAsString(Connection connection) throws SQLException { | ||
PreparedStatement queryStatement = connection.prepareStatement(querySql); | ||
System.out.println("SQL DML: " + querySql); | ||
ResultSet resultSet = queryStatement.executeQuery(); | ||
String vector = null; | ||
while (resultSet.next()) { | ||
vector = (String) resultSet.getObject(2); | ||
} | ||
System.out.println("Retrieved VECTOR: " + vector); | ||
} | ||
|
||
private void retrieveVectorWithBoundVector(Connection connection) throws SQLException { | ||
// Bind a Vector to a select | ||
PreparedStatement queryStatement = connection.prepareStatement(querySqlWithBind); | ||
double[] inputVector = { 1.0, 2.2, 3.3 }; | ||
System.out.println("SQL DML: " + querySqlWithBind); | ||
System.out.println("Bound VECTOR: " + Arrays.toString(inputVector)); | ||
queryStatement.setObject(1, inputVector, OracleType.VECTOR); | ||
ResultSet resultSet = queryStatement.executeQuery(); | ||
resultSet.next(); | ||
double[] outputVector = resultSet.getObject(2, double[].class); | ||
System.out.println("Retrieved VECTOR: " + Arrays.toString(outputVector)); | ||
} | ||
|
||
private Connection getConnectionFromPooledDataSource() throws SQLException { | ||
// Create pool-enabled data source instance | ||
PoolDataSource pds = PoolDataSourceFactory.getPoolDataSource(); | ||
// set connection properties on the data source | ||
pds.setConnectionFactoryClassName("oracle.jdbc.pool.OracleDataSource"); | ||
pds.setURL(URL); | ||
pds.setUser(USERNAME); | ||
pds.setPassword(PASSWORD); | ||
// Configure pool properties with a Properties instance | ||
Properties prop = new Properties(); | ||
prop.setProperty("oracle.jdbc.vectorDefaultGetObjectType", "String"); | ||
pds.setConnectionProperties(prop); | ||
// Override any pool properties directly | ||
pds.setInitialPoolSize(10); | ||
// Get a database connection from the pool-enabled data source | ||
Connection conn = pds.getConnection(); | ||
return conn; | ||
} | ||
|
||
private int randomize() { | ||
return ThreadLocalRandom.current().nextInt(); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use 2 space chars for padding.