Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathExample.java
More file actions
Latest commit
88 lines (78 loc) · 3.85 KB
/
Copy pathExample.java
File metadata and controls
88 lines (78 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
packagecom.example;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.sql.Statement;
importjava.util.ArrayList;
importjava.util.Random;
importcom.pgvector.PGvector;
importorg.postgresql.copy.CopyIn;
importorg.postgresql.copy.CopyManager;
importorg.postgresql.core.BaseConnection;
publicclassExample {
publicstaticvoidmain(String[] args) throwsSQLException {
// generate data
introws = 1000000;
intdimensions = 128;
ArrayList<float[]> embeddings = newArrayList<>(rows);
ArrayList<Integer> categories = newArrayList<>(rows);
Randomrnd = newRandom();
for (inti = 0; i < rows; i++) {
float[] embedding = newfloat[dimensions];
for (intj = 0; j < dimensions; j++) {
embedding[j] = (float) Math.random();
}
embeddings.add(embedding);
categories.add(rnd.nextInt(100));
}
// enable extensions
Connectionconn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/pgvector_citus");
StatementsetupStmt = conn.createStatement();
setupStmt.executeUpdate("CREATE EXTENSION IF NOT EXISTS citus");
setupStmt.executeUpdate("CREATE EXTENSION IF NOT EXISTS vector");
// GUC variables set on the session do not propagate to Citus workers
// https://github.com/citusdata/citus/issues/462
// you can either:
// 1. set them on the system, user, or database and reconnect
// 2. set them for a transaction with SET LOCAL
setupStmt.executeUpdate("ALTER DATABASE pgvector_citus SET maintenance_work_mem = '512MB'");
setupStmt.executeUpdate("ALTER DATABASE pgvector_citus SET hnsw.ef_search = 20");
conn.close();
// reconnect for updated GUC variables to take effect
conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/pgvector_citus");
PGvector.addVectorType(conn);
System.out.println("Creating distributed table");
setupStmt = conn.createStatement();
setupStmt.executeUpdate("DROP TABLE IF EXISTS items");
setupStmt.executeUpdate(String.format("CREATE TABLE items (id bigserial, embedding vector(%d), category_id bigint, PRIMARY KEY (id, category_id))", dimensions));
setupStmt.executeUpdate("SET citus.shard_count = 4");
setupStmt.executeQuery("SELECT create_distributed_table('items', 'category_id')");
System.out.println("Loading data in parallel");
CopyManagercopyManager = newCopyManager((BaseConnection) conn);
// TODO use binary format
CopyIncopyIn = copyManager.copyIn("COPY items (embedding, category_id) FROM STDIN");
for (inti = 0; i < rows; i++) {
PGvectorembedding = newPGvector(embeddings.get(i));
byte[] bytes = String.format("%s\t%d\n", embedding.getValue(), categories.get(i)).getBytes();
copyIn.writeToCopy(bytes, 0, bytes.length);
}
copyIn.endCopy();
System.out.println("Creating index in parallel");
StatementcreateIndexStmt = conn.createStatement();
createIndexStmt.executeUpdate("CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)");
System.out.println("Running distributed queries");
for (inti = 0; i < 10; i++) {
PreparedStatementqueryStmt = conn.prepareStatement("SELECT id FROM items ORDER BY embedding <-> ? LIMIT 10");
queryStmt.setObject(1, newPGvector(embeddings.get(rnd.nextInt(rows))));
ResultSetrs = queryStmt.executeQuery();
ArrayList<Long> ids = newArrayList<>();
while (rs.next()) {
ids.add(rs.getLong("id"));
}
System.out.println(ids.toString());
}
conn.close();
}
}