|
4 | 4 | import com.dtsx.astra.sdk.AstraDBCollection; |
5 | 5 | import io.stargate.sdk.json.domain.JsonDocument; |
6 | 6 | import io.stargate.sdk.json.exception.JsonApiException; |
7 | | - |
8 | 7 | import java.util.Map; |
9 | 8 |
|
10 | 9 | public class InsertOne { |
11 | | - public static void main(String[] args) { |
12 | | - |
13 | | -// Given an active db |
14 | | -AstraDB db = new AstraDB("<token>", "<api_endpoint>"); |
| 10 | + public static void main(String[] args) { |
| 11 | + AstraDB db = new AstraDB("<token>", "<api_endpoint>"); |
15 | 12 |
|
16 | | -/* |
17 | | - * Given a collection with vector (dimension 14) |
18 | | - * Can be created with: |
19 | | - * AstraDBCollection collection = db |
20 | | - * .createCollection("collection_vector1", 14); |
21 | | - */ |
22 | | -AstraDBCollection collection = db.collection("collection_vector1"); |
23 | | -// Adding this list to allow the test to be re-runnable |
24 | | -collection.deleteAll(); |
| 13 | + // Assumes a collection with a vector field of dimension 14 |
| 14 | + AstraDBCollection collection = db.collection("collection_vector1"); |
25 | 15 |
|
26 | | -// (1) You can insert records with key/value. |
27 | | -collection.insertOne(new JsonDocument() |
28 | | - .id("doc1") // uuid is generated if not explicitely set |
29 | | - .vector(new float[]{1f, 0f, 1f, 1f, 1f, 1f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f}) |
30 | | - .put("product_name", "HealthyFresh - Beef raw dog food") |
31 | | - .put("product_price", 12.99)); |
| 16 | + // You must delete any existing rows with the same IDs as the |
| 17 | + // rows you want to insert |
| 18 | + collection.deleteAll(); |
32 | 19 |
|
33 | | -// (2) You can insert records payload as a Json String |
34 | | -collection.insertOne(new JsonDocument() |
35 | | - .data("{" |
36 | | - +" \"_id\": \"doc2\", " |
37 | | - +" \"$vector\": [1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0], " |
38 | | - +" \"product_name\": \"HealthyFresh - Chicken raw dog food\", " |
39 | | - + " \"product_price\": 9.99" |
40 | | - + "}") |
41 | | -); |
| 20 | + // Insert rows defined by key/value |
| 21 | + collection.insertOne( |
| 22 | + new JsonDocument() |
| 23 | + .id("doc1") // uuid is generated if not explicitely set |
| 24 | + .vector(new float[]{1f, 0f, 1f, 1f, 1f, 1f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f}) |
| 25 | + .put("product_name", "HealthyFresh - Beef raw dog food") |
| 26 | + .put("product_price", 12.99)); |
| 27 | + |
| 28 | + // Insert rows defined as a JSON String |
| 29 | + collection.insertOne( |
| 30 | + new JsonDocument() |
| 31 | + .data( |
| 32 | + "{" + |
| 33 | + "\"_id\": \"doc2\", " + |
| 34 | + "\"$vector\": [1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0], " + |
| 35 | + "\"product_name\": \"HealthyFresh - Chicken raw dog food\", " + |
| 36 | + "\"product_price\": 9.99" + |
| 37 | + "}")); |
42 | 38 |
|
43 | | -// (3) You can also insert records payload as a Map |
44 | | -collection.insertOne(new JsonDocument() |
45 | | - .id("doc3") |
46 | | - .vector(new float[]{1f, 1f, 1f, 1f, 1f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f}) |
47 | | - .data(Map.of("product_name", "HealthyFresh - Chicken raw dog food")) |
48 | | -); |
| 39 | + // Insert rows defined as a Map |
| 40 | + collection.insertOne( |
| 41 | + new JsonDocument() |
| 42 | + .id("doc3") |
| 43 | + .vector(new float[]{1f, 1f, 1f, 1f, 1f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f}) |
| 44 | + .data(Map.of("product_name", "HealthyFresh - Chicken raw dog food"))); |
49 | 45 |
|
50 | | -// (4) Any combination is possible (key/value, json, map) |
51 | | -collection.insertOne(new JsonDocument() |
52 | | - .id("doc4") |
53 | | - .vector(new float[]{1f, 1f, 1f, 1f, 1f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f}) |
54 | | - .data("{" |
55 | | - +" \"product_name\": \"HealthyFresh - Chicken raw dog food\", " |
56 | | - + " \"product_price\": 9.99" |
57 | | - + "}") |
58 | | -); |
| 46 | + // Insert rows defined as a combination of key/value, JSON, and Map |
| 47 | + collection.insertOne( |
| 48 | + new JsonDocument() |
| 49 | + .id("doc4") |
| 50 | + .vector(new float[]{1f, 1f, 1f, 1f, 1f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f}) |
| 51 | + .data("{" + |
| 52 | + "\"product_name\": \"HealthyFresh - Chicken raw dog food\", " + |
| 53 | + "\"product_price\": 9.99" + |
| 54 | + "}")); |
59 | 55 |
|
60 | | -// (5) You cannot insert a document with an existing id |
61 | | -try { |
62 | | - collection.insertOne(new JsonDocument("doc4")); |
63 | | -} catch(JsonApiException e) { |
64 | | - System.out.println("Expected ERROR: " + e.getMessage()); |
65 | | -} |
| 56 | + // You cannot insert a document with an existing ID |
| 57 | + try { |
| 58 | + collection.insertOne(new JsonDocument("doc4")); |
| 59 | + } catch(JsonApiException e) { |
| 60 | + System.out.println("Expected ERROR: " + e.getMessage()); |
| 61 | + } |
66 | 62 |
|
67 | | -/* |
68 | | -* (6) If not provided id is generated and returned |
69 | | -*/ |
70 | | -String generatedId = collection.insertOne( |
| 63 | + // If you do not provide an ID, they are generated automatically |
| 64 | + String generatedId = collection.insertOne( |
71 | 65 | new JsonDocument().put("demo", 1)); |
72 | | - |
73 | | -// (6) can the payload be a bean/pojo ? |
74 | | -// Yes! check Object Mapping section |
75 | | - |
76 | | - } |
| 66 | + } |
77 | 67 | } |
0 commit comments