Last active
March 5, 2025 15:03
-
-
Save juarezjuniorgithub/9a2a4b8aa69e4552f37c78ca469f0e42 to your computer and use it in GitHub Desktop.
R2dbcVectorDemo.java
This file contains hidden or 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
/* | |
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.r2dbc; | |
import java.sql.SQLException; | |
import io.r2dbc.spi.ConnectionFactory; | |
import oracle.sql.VECTOR; | |
import reactor.core.publisher.Mono; | |
public class R2dbcVectorDemo { | |
public static void main(String[] args) { | |
try { | |
R2dbcVectorDemo demo = new R2dbcVectorDemo(); | |
demo.testConnection(); | |
demo.insertVector(); | |
demo.retrieveVector(); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
// Gives time for the async ops to complete | |
Mono.never().block(); | |
} | |
private void testConnection() { | |
Mono.from(connectionFactory.create()) | |
.flatMapMany(connection -> connection.createStatement("SELECT 1 FROM DUAL").execute()) | |
.flatMap(result -> result.map((row, metadata) -> row.get(0, Integer.class))).subscribe(System.out::println); | |
} | |
private void insertVector() throws SQLException { | |
float[] embeddingArray = new float[] { 1.0f, 2.0f, 3.0f }; | |
VECTOR vectorValue = VECTOR.ofFloat32Values(embeddingArray); | |
String insertQuery = new StringBuilder("INSERT INTO ").append(DatabaseConfig.getUser()).append(".") | |
.append(DatabaseConfig.getDbTableName()).append(" (id, embedding) VALUES (:id, :embedding)").toString(); | |
Mono.from(connectionFactory.create()) | |
.flatMap(connection -> Mono | |
.from(connection.createStatement(insertQuery).bind("id", 1).bind("embedding", vectorValue).execute()) | |
.then(Mono.from(connection.commitTransaction()))) | |
.subscribe(); | |
} | |
private void retrieveVector() { | |
String selectQuery = new StringBuilder("SELECT id, embedding FROM ").append(DatabaseConfig.getUser()).append(".") | |
.append(DatabaseConfig.getDbTableName()).append(" WHERE id = :id").toString(); | |
Mono.from(connectionFactory.create()) | |
.flatMapMany(connection -> connection.createStatement(selectQuery).bind("id", 1).execute()) | |
.flatMap(result -> result.map((row, metadata) -> { | |
Integer id = row.get("id", Integer.class); | |
float[] vector = row.get("embedding", float[].class); | |
return "ID: " + id + ", Vector: [" + vector[0] + ", " + vector[1] + ", " + vector[2] + "]"; | |
})).subscribe(System.out::println); | |
} | |
public R2dbcVectorDemo() { | |
this.connectionFactory = DatabaseConfig.getConnectionFactory(); | |
} | |
private ConnectionFactory connectionFactory; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment