Skip to content

Commit f042266

Browse files
committed
Moved examples to separate directory [skip ci]
1 parent 9a2b71f commit f042266

File tree

11 files changed

+246
-53
lines changed

11 files changed

+246
-53
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,8 @@ jobs:
2525
make
2626
sudo make install
2727
- run: psql -d pgvector_java_test -c "CREATE EXTENSION vector"
28-
# Hibernate 6.4 and HttpClient require Java 11+
28+
# Hibernate 6.4 requires Java 11+
2929
- if: ${{ matrix.java == 8 }}
3030
run: |
31-
rm src/test/java/com/pgvector/CohereTest.java
3231
rm src/test/java/com/pgvector/HibernateTest.java
33-
rm src/test/java/com/pgvector/OpenAITest.java
3432
- run: mvn -B -ntp test

README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ And follow the instructions for your database library:
3535

3636
Or check out some examples:
3737

38-
- [Embeddings](src/test/java/com/pgvector/OpenAITest.java) with OpenAI
39-
- [Binary embeddings](src/test/java/com/pgvector/CohereTest.java) with Cohere
40-
- [Horizontal scaling](src/test/java/com/pgvector/CitusTest.java) with Citus
41-
- [Bulk loading](src/test/java/com/pgvector/LoadingTest.java) with `COPY`
38+
- [Embeddings](examples/openai/src/main/java/com/example/Example.java) with OpenAI
39+
- [Binary embeddings](examples/cohere/src/main/java/com/example/Example.java) with Cohere
40+
- [Horizontal scaling](examples/citus/src/main/java/com/example/Example.java) with Citus
41+
- [Bulk loading](examples/loading/src/main/java/com/example/Example.java) with `COPY`
4242

4343
## JDBC (Java)
4444

@@ -519,3 +519,11 @@ cd pgvector-java
519519
createdb pgvector_java_test
520520
mvn test
521521
```
522+
523+
To run an example:
524+
525+
```sh
526+
cd examples/loading
527+
mvn package
528+
java -jar target/example.jar
529+
```

examples/citus/pom.xml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.example</groupId>
5+
<artifactId>example</artifactId>
6+
<version>1</version>
7+
<properties>
8+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
9+
</properties>
10+
<dependencies>
11+
<dependency>
12+
<groupId>org.postgresql</groupId>
13+
<artifactId>postgresql</artifactId>
14+
<version>42.7.3</version>
15+
</dependency>
16+
<dependency>
17+
<groupId>com.pgvector</groupId>
18+
<artifactId>pgvector</artifactId>
19+
<version>0.1.6</version>
20+
</dependency>
21+
</dependencies>
22+
<build>
23+
<plugins>
24+
<plugin>
25+
<artifactId>maven-assembly-plugin</artifactId>
26+
<version>3.7.1</version>
27+
<configuration>
28+
<descriptorRefs>
29+
<descriptorRef>jar-with-dependencies</descriptorRef>
30+
</descriptorRefs>
31+
<archive>
32+
<manifest>
33+
<mainClass>com.example.Example</mainClass>
34+
</manifest>
35+
</archive>
36+
<finalName>example</finalName>
37+
<appendAssemblyId>false</appendAssemblyId>
38+
</configuration>
39+
<executions>
40+
<execution>
41+
<id>make-assembly</id>
42+
<phase>package</phase>
43+
<goals>
44+
<goal>single</goal>
45+
</goals>
46+
</execution>
47+
</executions>
48+
</plugin>
49+
</plugins>
50+
</build>
51+
</project>

src/test/java/com/pgvector/CitusTest.java renamed to examples/citus/src/main/java/com/example/Example.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
package com.pgvector;
1+
package com.example;
22

3-
import java.io.UnsupportedEncodingException;
43
import java.sql.Connection;
54
import java.sql.DriverManager;
65
import java.sql.PreparedStatement;
@@ -10,19 +9,12 @@
109
import java.util.ArrayList;
1110
import java.util.Random;
1211
import com.pgvector.PGvector;
13-
import org.postgresql.PGConnection;
1412
import org.postgresql.copy.CopyIn;
1513
import org.postgresql.copy.CopyManager;
1614
import org.postgresql.core.BaseConnection;
17-
import org.junit.jupiter.api.Test;
18-
19-
public class CitusTest {
20-
@Test
21-
void example() throws SQLException {
22-
if (System.getenv("TEST_CITUS") == null) {
23-
return;
24-
}
2515

16+
public class Example {
17+
public static void main(String[] args) throws SQLException {
2618
// generate data
2719
int rows = 1000000;
2820
int dimensions = 128;

examples/cohere/pom.xml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.example</groupId>
5+
<artifactId>example</artifactId>
6+
<version>1</version>
7+
<properties>
8+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
9+
</properties>
10+
<dependencies>
11+
<dependency>
12+
<groupId>org.postgresql</groupId>
13+
<artifactId>postgresql</artifactId>
14+
<version>42.7.3</version>
15+
</dependency>
16+
<dependency>
17+
<groupId>com.pgvector</groupId>
18+
<artifactId>pgvector</artifactId>
19+
<version>0.1.6</version>
20+
</dependency>
21+
<dependency>
22+
<groupId>com.fasterxml.jackson.core</groupId>
23+
<artifactId>jackson-databind</artifactId>
24+
<version>2.16.0</version>
25+
</dependency>
26+
</dependencies>
27+
<build>
28+
<plugins>
29+
<plugin>
30+
<artifactId>maven-assembly-plugin</artifactId>
31+
<version>3.7.1</version>
32+
<configuration>
33+
<descriptorRefs>
34+
<descriptorRef>jar-with-dependencies</descriptorRef>
35+
</descriptorRefs>
36+
<archive>
37+
<manifest>
38+
<mainClass>com.example.Example</mainClass>
39+
</manifest>
40+
</archive>
41+
<finalName>example</finalName>
42+
<appendAssemblyId>false</appendAssemblyId>
43+
</configuration>
44+
<executions>
45+
<execution>
46+
<id>make-assembly</id>
47+
<phase>package</phase>
48+
<goals>
49+
<goal>single</goal>
50+
</goals>
51+
</execution>
52+
</executions>
53+
</plugin>
54+
</plugins>
55+
</build>
56+
</project>

src/test/java/com/pgvector/CohereTest.java renamed to examples/cohere/src/main/java/com/example/Example.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.pgvector;
1+
package com.example;
22

33
import java.io.IOException;
44
import java.net.URI;
@@ -18,16 +18,15 @@
1818
import com.fasterxml.jackson.databind.ObjectMapper;
1919
import com.fasterxml.jackson.databind.JsonNode;
2020
import com.fasterxml.jackson.databind.node.ObjectNode;
21+
import com.pgvector.PGbit;
2122
import com.pgvector.PGvector;
22-
import org.postgresql.PGConnection;
23-
import org.junit.jupiter.api.Test;
2423

25-
public class CohereTest {
26-
@Test
27-
void example() throws IOException, InterruptedException, SQLException {
24+
public class Example {
25+
public static void main(String[] args) throws IOException, InterruptedException, SQLException {
2826
String apiKey = System.getenv("CO_API_KEY");
2927
if (apiKey == null) {
30-
return;
28+
System.out.println("Set CO_API_KEY");
29+
System.exit(1);
3130
}
3231

3332
Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/pgvector_example");
@@ -67,7 +66,7 @@ void example() throws IOException, InterruptedException, SQLException {
6766
}
6867

6968
// https://docs.cohere.com/reference/embed
70-
private List<byte[]> fetchEmbeddings(String[] texts, String inputType, String apiKey) throws IOException, InterruptedException {
69+
private static List<byte[]> fetchEmbeddings(String[] texts, String inputType, String apiKey) throws IOException, InterruptedException {
7170
ObjectMapper mapper = new ObjectMapper();
7271
ObjectNode root = mapper.createObjectNode();
7372
for (String v : texts) {

examples/loading/pom.xml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.example</groupId>
5+
<artifactId>example</artifactId>
6+
<version>1</version>
7+
<properties>
8+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
9+
</properties>
10+
<dependencies>
11+
<dependency>
12+
<groupId>org.postgresql</groupId>
13+
<artifactId>postgresql</artifactId>
14+
<version>42.7.3</version>
15+
</dependency>
16+
<dependency>
17+
<groupId>com.pgvector</groupId>
18+
<artifactId>pgvector</artifactId>
19+
<version>0.1.6</version>
20+
</dependency>
21+
</dependencies>
22+
<build>
23+
<plugins>
24+
<plugin>
25+
<artifactId>maven-assembly-plugin</artifactId>
26+
<version>3.7.1</version>
27+
<configuration>
28+
<descriptorRefs>
29+
<descriptorRef>jar-with-dependencies</descriptorRef>
30+
</descriptorRefs>
31+
<archive>
32+
<manifest>
33+
<mainClass>com.example.Example</mainClass>
34+
</manifest>
35+
</archive>
36+
<finalName>example</finalName>
37+
<appendAssemblyId>false</appendAssemblyId>
38+
</configuration>
39+
<executions>
40+
<execution>
41+
<id>make-assembly</id>
42+
<phase>package</phase>
43+
<goals>
44+
<goal>single</goal>
45+
</goals>
46+
</execution>
47+
</executions>
48+
</plugin>
49+
</plugins>
50+
</build>
51+
</project>

src/test/java/com/pgvector/LoadingTest.java renamed to examples/loading/src/main/java/com/example/Example.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,17 @@
1-
package com.pgvector;
1+
package com.example;
22

3-
import java.io.UnsupportedEncodingException;
43
import java.sql.Connection;
54
import java.sql.DriverManager;
6-
import java.sql.PreparedStatement;
7-
import java.sql.ResultSet;
85
import java.sql.SQLException;
96
import java.sql.Statement;
107
import java.util.ArrayList;
118
import com.pgvector.PGvector;
12-
import org.postgresql.PGConnection;
139
import org.postgresql.copy.CopyIn;
1410
import org.postgresql.copy.CopyManager;
1511
import org.postgresql.core.BaseConnection;
16-
import org.junit.jupiter.api.Test;
17-
18-
public class LoadingTest {
19-
@Test
20-
void example() throws SQLException {
21-
if (System.getenv("TEST_LOADING") == null) {
22-
return;
23-
}
2412

13+
public class Example {
14+
public static void main(String[] args) throws SQLException {
2515
// generate random data
2616
int rows = 1000000;
2717
int dimensions = 128;

examples/openai/pom.xml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.example</groupId>
5+
<artifactId>example</artifactId>
6+
<version>1</version>
7+
<properties>
8+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
9+
</properties>
10+
<dependencies>
11+
<dependency>
12+
<groupId>org.postgresql</groupId>
13+
<artifactId>postgresql</artifactId>
14+
<version>42.7.3</version>
15+
</dependency>
16+
<dependency>
17+
<groupId>com.pgvector</groupId>
18+
<artifactId>pgvector</artifactId>
19+
<version>0.1.6</version>
20+
</dependency>
21+
<dependency>
22+
<groupId>com.fasterxml.jackson.core</groupId>
23+
<artifactId>jackson-databind</artifactId>
24+
<version>2.16.0</version>
25+
</dependency>
26+
</dependencies>
27+
<build>
28+
<plugins>
29+
<plugin>
30+
<artifactId>maven-assembly-plugin</artifactId>
31+
<version>3.7.1</version>
32+
<configuration>
33+
<descriptorRefs>
34+
<descriptorRef>jar-with-dependencies</descriptorRef>
35+
</descriptorRefs>
36+
<archive>
37+
<manifest>
38+
<mainClass>com.example.Example</mainClass>
39+
</manifest>
40+
</archive>
41+
<finalName>example</finalName>
42+
<appendAssemblyId>false</appendAssemblyId>
43+
</configuration>
44+
<executions>
45+
<execution>
46+
<id>make-assembly</id>
47+
<phase>package</phase>
48+
<goals>
49+
<goal>single</goal>
50+
</goals>
51+
</execution>
52+
</executions>
53+
</plugin>
54+
</plugins>
55+
</build>
56+
</project>

src/test/java/com/pgvector/OpenAITest.java renamed to examples/openai/src/main/java/com/example/Example.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.pgvector;
1+
package com.example;
22

33
import java.io.IOException;
44
import java.net.URI;
@@ -19,15 +19,13 @@
1919
import com.fasterxml.jackson.databind.JsonNode;
2020
import com.fasterxml.jackson.databind.node.ObjectNode;
2121
import com.pgvector.PGvector;
22-
import org.postgresql.PGConnection;
23-
import org.junit.jupiter.api.Test;
2422

25-
public class OpenAITest {
26-
@Test
27-
void example() throws IOException, InterruptedException, SQLException {
23+
public class Example {
24+
public static void main(String[] args) throws IOException, InterruptedException, SQLException {
2825
String apiKey = System.getenv("OPENAI_API_KEY");
2926
if (apiKey == null) {
30-
return;
27+
System.out.println("Set OPENAI_API_KEY");
28+
System.exit(1);
3129
}
3230

3331
Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/pgvector_example");
@@ -67,7 +65,7 @@ void example() throws IOException, InterruptedException, SQLException {
6765
conn.close();
6866
}
6967

70-
private List<float[]> fetchEmbeddings(String[] input, String apiKey) throws IOException, InterruptedException {
68+
private static List<float[]> fetchEmbeddings(String[] input, String apiKey) throws IOException, InterruptedException {
7169
ObjectMapper mapper = new ObjectMapper();
7270
ObjectNode root = mapper.createObjectNode();
7371
for (String v : input) {

0 commit comments

Comments
 (0)