|
14 | 14 | public class UpdateVectors { |
15 | 15 |
|
16 | 16 | public static void main(String[] args) throws Exception { |
17 | | - var connection = new Sql2o( |
18 | | - "jdbc:mariadb://127.0.0.1:3306/demo", "root", "password").open(); |
19 | | - |
20 | | - var table = connection.createQuery(""" |
21 | | - SELECT id, CONCAT( |
22 | | - "Product: ", title, ". Stars: ", stars, ". Price: $", price, ". Category: ", category_name, |
23 | | - ". Best seller: ", CASE WHEN is_best_seller THEN "Yes" ELSE "No" END |
24 | | - ) AS description |
25 | | - FROM products |
26 | | - WHERE embedding IS NULL |
27 | | - """).executeAndFetchTableLazy(); |
28 | | - |
29 | | - for (Row row : table.rows()) { |
30 | | - var id = row.getString("id"); |
31 | | - var description = row.getString("description"); |
32 | | - |
33 | | - var requestBody = """ |
34 | | - { "model": "bert-cpp-minilm-v6", "input": %s } |
35 | | - """.formatted(new ObjectMapper().writeValueAsString(description)); |
36 | | - |
37 | | - var response = Unirest.post("http://localhost:8080/v1/embeddings") |
38 | | - .header("Content-Type", "application/json") |
39 | | - .body(requestBody) |
40 | | - .asString().getBody(); |
41 | | - |
42 | | - connection.createQuery(""" |
43 | | - UPDATE products |
44 | | - SET embedding = VEC_FromText(JSON_EXTRACT(:response, '$.data[0].embedding')) |
45 | | - WHERE id = :id |
46 | | - """) |
47 | | - .addParameter("response", response) |
48 | | - .addParameter("id", id) |
49 | | - .executeUpdate(); |
50 | | - |
51 | | - System.out.println("Updated embedding for product ID: " + id); |
| 17 | + try (var connection = new Sql2o( |
| 18 | + "jdbc:mariadb://127.0.0.1:3306/demo", "root", "password").open()) { |
| 19 | + |
| 20 | + var table = connection.createQuery(""" |
| 21 | + SELECT id, CONCAT( |
| 22 | + "Product: ", title, ". Stars: ", stars, ". Price: $", price, ". Category: ", category_name, |
| 23 | + ". Best seller: ", CASE WHEN is_best_seller THEN "Yes" ELSE "No" END |
| 24 | + ) AS description |
| 25 | + FROM products |
| 26 | + WHERE embedding IS NULL |
| 27 | + """).executeAndFetchTableLazy(); |
| 28 | + |
| 29 | + for (Row row : table.rows()) { |
| 30 | + var id = row.getString("id"); |
| 31 | + var description = row.getString("description"); |
| 32 | + |
| 33 | + var requestBody = """ |
| 34 | + { "model": "bert-cpp-minilm-v6", "input": %s } |
| 35 | + """.formatted(new ObjectMapper().writeValueAsString(description)); |
| 36 | + |
| 37 | + var response = Unirest.post("http://localhost:8080/v1/embeddings") |
| 38 | + .header("Content-Type", "application/json") |
| 39 | + .body(requestBody) |
| 40 | + .asString().getBody(); |
| 41 | + |
| 42 | + connection.createQuery(""" |
| 43 | + UPDATE products |
| 44 | + SET embedding = VEC_FromText(JSON_EXTRACT(:response, '$.data[0].embedding')) |
| 45 | + WHERE id = :id |
| 46 | + """) |
| 47 | + .addParameter("response", response) |
| 48 | + .addParameter("id", id) |
| 49 | + .executeUpdate(); |
| 50 | + |
| 51 | + System.out.println("Updated embedding for product ID: " + id); |
| 52 | + } |
52 | 53 | } |
53 | 54 |
|
54 | | - connection.close(); |
| 55 | + System.out.println("All embeddings updated!"); |
55 | 56 | } |
56 | 57 | } |
0 commit comments