|
9 | 9 |
|
10 | 10 | public class InsertOne { |
11 | 11 | public static void main(String[] args) { |
12 | | - // Given an active db |
13 | | - AstraDB db = new AstraDB("<token>", "<api_endpoint>"); |
14 | 12 |
|
15 | | - /* |
16 | | - * Given a collection with vector (dimension 14) |
17 | | - * |
18 | | - * Can be created with: (but you will not create a collection each time) |
19 | | - * |
20 | | - * AstraDBCollection collection = db.createCollection("collection_vector1", 14); |
21 | | - */ |
22 | | - AstraDBCollection collection = db.collection("collection_vector1"); |
23 | | - collection.deleteAll(); |
| 13 | +// Given an active db |
| 14 | +AstraDB db = new AstraDB("<token>", "<api_endpoint>"); |
24 | 15 |
|
25 | | - // (1) You can insert records with key/value. |
26 | | - collection.insertOne(new JsonDocument() |
27 | | - .id("doc1") // uuid is generated if not explicitely set |
28 | | - .vector(new float[]{1f, 0f, 1f, 1f, 1f, 1f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f}) |
29 | | - .put("product_name", "HealthyFresh - Beef raw dog food") |
30 | | - .put("product_price", 12.99)); |
| 16 | +/* |
| 17 | + * Given a collection with vector (dimension 14) |
| 18 | + * Can be created with: |
| 19 | + * AstraDBCollection collection = db.createCollection("collection_vector1", 14); |
| 20 | + */ |
| 21 | +AstraDBCollection collection = db.collection("collection_vector1"); |
31 | 22 |
|
32 | | - // (2) You can insert records payload as a Json String |
33 | | - collection.insertOne(new JsonDocument() |
34 | | - .data("{" |
35 | | - +" \"_id\": \"doc2\", " |
36 | | - +" \"$vector\": [1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0], " |
37 | | - +" \"product_name\": \"HealthyFresh - Chicken raw dog food\", " |
38 | | - + " \"product_price\": 9.99" |
39 | | - + "}") |
40 | | - ); |
| 23 | +// (1) You can insert records with key/value. |
| 24 | +collection.insertOne(new JsonDocument() |
| 25 | + .id("doc1") // uuid is generated if not explicitely set |
| 26 | + .vector(new float[]{1f, 0f, 1f, 1f, 1f, 1f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f}) |
| 27 | + .put("product_name", "HealthyFresh - Beef raw dog food") |
| 28 | + .put("product_price", 12.99)); |
41 | 29 |
|
42 | | - // (3) You can also insert records payload as a Map |
43 | | - collection.insertOne(new JsonDocument() |
44 | | - .id("doc3") |
45 | | - .vector(new float[]{1f, 1f, 1f, 1f, 1f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f}) |
46 | | - .data(Map.of("product_name", "HealthyFresh - Chicken raw dog food")) |
47 | | - ); |
| 30 | +// (2) You can insert records payload as a Json String |
| 31 | +collection.insertOne(new JsonDocument() |
| 32 | + .data("{" |
| 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 | + + "}") |
| 38 | +); |
48 | 39 |
|
49 | | - // (4) All hybrid combination are possible (key/value, json, map) |
50 | | - collection.insertOne(new JsonDocument() |
51 | | - .id("doc4") |
52 | | - .vector(new float[]{1f, 1f, 1f, 1f, 1f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f}) |
53 | | - .data("{" |
54 | | - +" \"product_name\": \"HealthyFresh - Chicken raw dog food\", " |
55 | | - + " \"product_price\": 9.99" |
56 | | - + "}") |
57 | | - ); |
| 40 | +// (3) You can also insert records payload as a Map |
| 41 | +collection.insertOne(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")) |
| 45 | +); |
58 | 46 |
|
59 | | - // (5) You cannot insert a document with an existing id |
60 | | - try { |
61 | | - collection.insertOne(new JsonDocument("doc4")); |
62 | | - } catch(ApiException e) { |
63 | | - System.out.println("Expected ERROR: " + e.getMessage()); |
64 | | - } |
| 47 | +// (4) All hybrid combination are possible (key/value, json, map) |
| 48 | +collection.insertOne(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 | + + "}") |
| 55 | +); |
65 | 56 |
|
66 | | - /* |
67 | | - * (5) Insertion rules |
68 | | - * |
69 | | - * - all attributes are nullable |
70 | | - * - id is generated if needed, the id is returned by insertOne() |
71 | | - * - Add any property you want, but needs to use [A-Za-z_-.] and not starting by '$' |
72 | | - * - It is schema less, values can be any simple type |
73 | | - */ |
74 | | - String generatedId = collection.insertOne(new JsonDocument().put("demo", 1)); |
| 57 | +// (5) You cannot insert a document with an existing id |
| 58 | +try { |
| 59 | + collection.insertOne(new JsonDocument("doc4")); |
| 60 | +} catch(ApiException e) { |
| 61 | + System.out.println("Expected ERROR: " + e.getMessage()); |
| 62 | +} |
| 63 | + |
| 64 | +/* |
| 65 | +* (5) Insertion rules |
| 66 | +* |
| 67 | +* - all attributes are nullable |
| 68 | +* - id is generated if needed, the id is returned by insertOne() |
| 69 | +* - Add any property you want, but needs to use [A-Za-z_-.] and not starting by '$' |
| 70 | +* - It is schema less, values can be any simple type |
| 71 | +*/ |
| 72 | +String generatedId = collection.insertOne( |
| 73 | + new JsonDocument().put("demo", 1)); |
75 | 74 |
|
76 | | - // (6) can the payload be a bean/pojo ? |
77 | | - // Yes! check Object Mapping section |
| 75 | +// (6) can the payload be a bean/pojo ? |
| 76 | +// Yes! check Object Mapping section |
78 | 77 |
|
79 | 78 | } |
80 | 79 | } |
0 commit comments