Skip to content

Commit 63ed919

Browse files
committed
Started R2DBC PostgreSQL example
1 parent 124bf84 commit 63ed919

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ For other build tools, see [this page](https://central.sonatype.com/artifact/com
2828

2929
And follow the instructions for your database library:
3030

31-
- Java - [JDBC](#jdbc-java), [Spring JDBC](#spring-jdbc), [Hibernate](#hibernate)
31+
- Java - [JDBC](#jdbc-java), [Spring JDBC](#spring-jdbc), [Hibernate](#hibernate), [R2DBC](#r2dbc)
3232
- Kotlin - [JDBC](#jdbc-kotlin)
3333
- Groovy - [JDBC](#jdbc-groovy), [Groovy SQL](#groovy-sql)
3434
- Scala - [JDBC](#jdbc-scala), [Slick](#slick)
@@ -204,6 +204,20 @@ List<Item> items = entityManager
204204

205205
See a [full example](src/test/java/com/pgvector/HibernateTest.java)
206206

207+
## R2DBC
208+
209+
R2DBC PostgreSQL 1.0.3+ supports the [vector type](https://github.com/pgjdbc/r2dbc-postgresql#data-type-mapping) (use this instead of `com.pgvector.pgvector`).
210+
211+
For Maven, add to `pom.xml` under `<dependencies>`:
212+
213+
```xml
214+
<dependency>
215+
<groupId>org.postgresql</groupId>
216+
<artifactId>r2dbc-postgresql</artifactId>
217+
<version>1.0.3.RELEASE</version>
218+
</dependency>
219+
```
220+
207221
## JDBC (Kotlin)
208222

209223
Import the `PGvector` class

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@
101101
<version>6.4.0.Final</version>
102102
<scope>test</scope>
103103
</dependency>
104+
<dependency>
105+
<groupId>org.postgresql</groupId>
106+
<artifactId>r2dbc-postgresql</artifactId>
107+
<version>1.0.3.RELEASE</version>
108+
</dependency>
104109
<dependency>
105110
<groupId>com.fasterxml.jackson.core</groupId>
106111
<artifactId>jackson-databind</artifactId>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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

Comments
 (0)