Skip to content

Commit 820775a

Browse files
committed
Added JDBC example for Java
1 parent 0b2ced3 commit 820775a

File tree

6 files changed

+71
-16
lines changed

6 files changed

+71
-16
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
distribution: temurin
1212
- uses: ankane/setup-postgres@v1
1313
with:
14-
database: pgvector_scala_test
14+
database: pgvector_java_test
1515
dev-files: true
1616
- run: |
1717
cd /tmp

README.md

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,54 @@
1-
# pgvector-scala
1+
# pgvector-java
22

3-
[pgvector](https://github.com/pgvector/pgvector) examples for Scala
3+
[pgvector](https://github.com/pgvector/pgvector) examples for Java and Scala
44

55
Supports [JDBC](https://docs.oracle.com/javase/tutorial/jdbc/basics/index.html) and [Slick](https://github.com/slick/slick)
66

7-
[![Build Status](https://github.com/pgvector/pgvector-scala/workflows/build/badge.svg?branch=master)](https://github.com/pgvector/pgvector-scala/actions)
7+
[![Build Status](https://github.com/pgvector/pgvector-java/workflows/build/badge.svg?branch=master)](https://github.com/pgvector/pgvector-java/actions)
88

99
## Getting Started
1010

1111
Follow the instructions for your database library:
1212

13-
- [JDBC](#jdbc)
13+
- [JDBC (Java)](#jdbc-java)
14+
- [JDBC (Scala)](#jdbc-scala)
1415
- [Slick](#slick)
1516

16-
## JDBC
17+
## JDBC (Java)
18+
19+
Create a table
20+
21+
```java
22+
Statement stmt = conn.createStatement();
23+
stmt.executeUpdate("CREATE TABLE items (embedding vector(3))");
24+
```
25+
26+
Insert a vector
27+
28+
```java
29+
stmt.executeUpdate("INSERT INTO items (embedding) VALUES ('[1,1,1]')");
30+
```
31+
32+
Get the nearest neighbors
33+
34+
```java
35+
ResultSet rs = stmt.executeQuery("SELECT * FROM items ORDER BY embedding <-> '[1,1,1]' LIMIT 5");
36+
while (rs.next()) {
37+
System.out.println(rs.getString("embedding"));
38+
}
39+
```
40+
41+
Add an approximate index
42+
43+
```java
44+
stmt.executeUpdate("CREATE INDEX my_index ON items USING ivfflat (embedding vector_l2_ops)");
45+
```
46+
47+
Use `vector_ip_ops` for inner product and `vector_cosine_ops` for cosine distance
48+
49+
See a [full example](src/main/java/JDBCJava.java)
50+
51+
## JDBC (Scala)
1752

1853
Create a table
1954

@@ -45,7 +80,7 @@ stmt.executeUpdate("CREATE INDEX my_index ON items USING ivfflat (embedding vect
4580

4681
Use `vector_ip_ops` for inner product and `vector_cosine_ops` for cosine distance
4782

48-
See a [full example](src/main/scala/JDBC.scala)
83+
See a [full example](src/main/scala/JDBCScala.scala)
4984

5085
## Slick
5186

@@ -86,16 +121,16 @@ See a [full example](src/main/scala/Slick.scala)
86121

87122
Everyone is encouraged to help improve this project. Here are a few ways you can help:
88123

89-
- [Report bugs](https://github.com/pgvector/pgvector-scala/issues)
90-
- Fix bugs and [submit pull requests](https://github.com/pgvector/pgvector-scala/pulls)
124+
- [Report bugs](https://github.com/pgvector/pgvector-java/issues)
125+
- Fix bugs and [submit pull requests](https://github.com/pgvector/pgvector-java/pulls)
91126
- Write, clarify, or fix documentation
92127
- Suggest or add new features
93128

94129
To get started with development:
95130

96131
```sh
97-
git clone https://github.com/pgvector/pgvector-scala.git
98-
cd pgvector-scala
99-
createdb pgvector_scala_test
132+
git clone https://github.com/pgvector/pgvector-java.git
133+
cd pgvector-java
134+
createdb pgvector_java_test
100135
sbt run
101136
```

src/main/java/JDBCJava.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import java.sql.*;
2+
3+
class JDBCJava {
4+
public static void example() throws SQLException {
5+
Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/pgvector_java_test");
6+
Statement stmt = conn.createStatement();
7+
stmt.executeUpdate("CREATE EXTENSION IF NOT EXISTS vector");
8+
stmt.executeUpdate("DROP TABLE IF EXISTS jdbc_items");
9+
stmt.executeUpdate("DROP TABLE IF EXISTS jdbc_items");
10+
stmt.executeUpdate("CREATE TABLE jdbc_items (embedding vector(3))");
11+
stmt.executeUpdate("INSERT INTO jdbc_items (embedding) VALUES ('[1,1,1]'), ('[2,2,2]'), ('[1,1,2]')");
12+
ResultSet rs = stmt.executeQuery("SELECT * FROM jdbc_items ORDER BY embedding <-> '[1,1,1]' LIMIT 5");
13+
while (rs.next()) {
14+
System.out.println(rs.getString("embedding"));
15+
}
16+
stmt.executeUpdate("CREATE INDEX jdbc_index ON jdbc_items USING ivfflat (embedding vector_l2_ops)");
17+
conn.close();
18+
}
19+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import java.sql.DriverManager
22

3-
object JDBC {
3+
object JDBCScala {
44
def example(): Unit = {
5-
val conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/pgvector_scala_test")
5+
val conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/pgvector_java_test")
66
val stmt = conn.createStatement()
77
stmt.executeUpdate("CREATE EXTENSION IF NOT EXISTS vector")
88
stmt.executeUpdate("DROP TABLE IF EXISTS jdbc_items")

src/main/scala/Main.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
object Main {
22
def main(args: Array[String]): Unit = {
3-
JDBC.example()
3+
JDBCJava.example()
4+
JDBCScala.example()
45
Slick.example()
56
}
67
}

src/main/scala/Slick.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Items(tag: Tag) extends Table[(String)](tag, "slick_items") {
1010

1111
object Slick {
1212
def example(): Unit = {
13-
val db = Database.forURL("jdbc:postgresql://localhost:5432/pgvector_scala_test", driver="org.postgresql.Driver")
13+
val db = Database.forURL("jdbc:postgresql://localhost:5432/pgvector_java_test", driver="org.postgresql.Driver")
1414

1515
try {
1616
val items = TableQuery[Items]

0 commit comments

Comments
 (0)