|
| 1 | +package com.pgvector; |
| 2 | + |
| 3 | +import java.sql.SQLException; |
| 4 | +import io.r2dbc.postgresql.codec.Vector; |
| 5 | +import io.r2dbc.spi.ConnectionFactories; |
| 6 | +import io.r2dbc.spi.ConnectionFactory; |
| 7 | +import io.r2dbc.spi.ConnectionFactoryOptions; |
| 8 | +import io.r2dbc.spi.Statement; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | +import reactor.core.publisher.Mono; |
| 11 | + |
| 12 | +public class R2DBCTest { |
| 13 | + @Test |
| 14 | + void example() throws SQLException { |
| 15 | + ConnectionFactory connectionFactory = ConnectionFactories.get( |
| 16 | + ConnectionFactoryOptions.builder() |
| 17 | + .option(ConnectionFactoryOptions.DRIVER, "postgresql") |
| 18 | + .option(ConnectionFactoryOptions.HOST, "localhost") |
| 19 | + .option(ConnectionFactoryOptions.USER, System.getenv("USER")) |
| 20 | + .option(ConnectionFactoryOptions.PASSWORD, "") |
| 21 | + .option(ConnectionFactoryOptions.DATABASE, "pgvector_java_test") |
| 22 | + .build() |
| 23 | + ); |
| 24 | + |
| 25 | + Mono.from(connectionFactory.create()) |
| 26 | + .flatMapMany(connection -> connection |
| 27 | + .createStatement("SELECT $1 AS embedding") |
| 28 | + .bind("$1", Vector.of(1, 2, 3)) |
| 29 | + .execute()) |
| 30 | + .flatMap(result -> result |
| 31 | + .map((row, rowMetadata) -> row.get("embedding", Vector.class))) |
| 32 | + .doOnNext(System.out::println) |
| 33 | + .blockLast(); |
| 34 | + } |
| 35 | +} |
0 commit comments