Skip to content

Commit 0b5fe65

Browse files
committed
test
1 parent a254e45 commit 0b5fe65

File tree

5 files changed

+101
-5
lines changed

5 files changed

+101
-5
lines changed

astra-db-client/src/test/java/com/dtsx/astra/sdk/documentation/CreateCollection.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,23 @@
33
import com.dtsx.astra.sdk.AstraDBCollection;
44
import io.stargate.sdk.json.domain.CollectionDefinition;
55
import io.stargate.sdk.json.domain.SimilarityMetric;
6+
import io.stargate.sdk.json.exception.ApiException;
7+
import io.stargate.sdk.json.exception.InvalidJsonApiArgumentException;
68

79
public class CreateCollection {
810
public static void main(String[] args) {
911
// Given an active db
1012
AstraDB db = new AstraDB("<token>", "<api_endpoint>");
1113

12-
// Create collection with no vector
14+
/*
15+
* Create collection with no vector.
16+
*/
1317
AstraDBCollection collection1 = db.createCollection("collection_simple");
1418

1519
// Create collection with vector
1620
AstraDBCollection collection2 = db.createCollection(
1721
"collection_vector1",
18-
1536,
22+
14,
1923
SimilarityMetric.cosine);
2024

2125
// Create collection with vector (builder)
@@ -24,5 +28,14 @@ public static void main(String[] args) {
2428
.name("collection_vector2")
2529
.vector(1536, SimilarityMetric.euclidean)
2630
.build());
31+
32+
/*
33+
* Collection name should follow [a-zA-Z][a-zA-Z0-9_]* pattern (snake case)
34+
*/
35+
try {
36+
db.createCollection("invalid.name");
37+
} catch(ApiException e) {
38+
// will fail
39+
}
2740
}
2841
}

astra-db-client/src/test/java/com/dtsx/astra/sdk/documentation/DeleteCollection.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66

77
public class DeleteCollection {
88
public static void main(String[] args) {
9+
910
// Given an active db
1011
AstraDB db = new AstraDB("<token>", "<api_endpoint>");
1112

1213
// Find a collection
13-
db.deleteCollection("tmp_collection");
14+
db.deleteCollection("collection_vector1");
1415
}
1516
}

astra-db-client/src/test/java/com/dtsx/astra/sdk/documentation/FindAllCollections.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
public class FindAllCollections {
66
public static void main(String[] args) {
7+
78
// Given an active db
89
AstraDB db = new AstraDB("<token>", "<api_endpoint>");
910

astra-db-client/src/test/java/com/dtsx/astra/sdk/documentation/FindCollection.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66

77
public class FindCollection {
88
public static void main(String[] args) {
9+
910
// Given an active db
1011
AstraDB db = new AstraDB("<token>", "<api_endpoint>");
1112

1213
// Find a collection
13-
Optional<CollectionDefinition> collection = db.findCollection("collection1");
14+
Optional<CollectionDefinition> collection = db.findCollection("collection_vector1");
1415

1516
// Another test with a collection that does not exist
16-
boolean collectionExists = db.isCollectionExists("collection1");
17+
boolean collectionExists = db.isCollectionExists("collection_vector2");
1718
}
1819
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.dtsx.astra.sdk.documentation;
2+
3+
import com.dtsx.astra.sdk.AstraDB;
4+
import com.dtsx.astra.sdk.AstraDBCollection;
5+
import io.stargate.sdk.json.domain.JsonDocument;
6+
import io.stargate.sdk.json.exception.ApiException;
7+
8+
import java.util.Map;
9+
10+
public class InsertOne {
11+
public static void main(String[] args) {
12+
// Given an active db
13+
AstraDB db = new AstraDB("<token>", "<api_endpoint>");
14+
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();
24+
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));
31+
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+
);
41+
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+
);
48+
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+
);
58+
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+
}
65+
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));
75+
76+
// (6) can the payload be a bean/pojo ?
77+
// Yes! check Object Mapping section
78+
79+
}
80+
}

0 commit comments

Comments
 (0)