1
1
package com .dtsx .astra .sdk .vector ;
2
2
3
3
import com .dtsx .astra .sdk .db .domain .CloudProviderType ;
4
+ import com .fasterxml .jackson .annotation .JsonProperty ;
4
5
import io .stargate .sdk .core .domain .ObjectMap ;
6
+ import io .stargate .sdk .json .domain .JsonDocument ;
7
+ import io .stargate .sdk .json .domain .odm .Document ;
8
+ import io .stargate .sdk .json .vector .JsonVectorStore ;
5
9
import io .stargate .sdk .json .vector .VectorStore ;
10
+ import lombok .AllArgsConstructor ;
11
+ import lombok .Data ;
12
+ import lombok .NoArgsConstructor ;
6
13
14
+ import java .util .Map ;
7
15
import java .util .UUID ;
8
16
9
17
public class AstraVectorQuickStart {
@@ -12,14 +20,66 @@ public void quickStartTest() {
12
20
String databaseName = "vector_client_test" ;
13
21
String astraToken = System .getenv ("ASTRA_DB_APPLICATION_TOKEN" );
14
22
15
- UUID dbId = new AstraVectorClient (astraToken )
16
- .createDatabase (databaseName , CloudProviderType .GCP , "us-east1" );
23
+ AstraVectorClient vectorClient = new AstraVectorClient (astraToken );
24
+ if (!vectorClient .isDatabaseExists (databaseName )) {
25
+ vectorClient .createDatabase (databaseName );
26
+ }
17
27
18
- AstraVectorDatabaseClient vectorDb = new AstraVectorClient (astraToken )
19
- .database (databaseName );
28
+ // Without ODM Accessing the Vector DB with JSON-ISH
29
+ AstraVectorDatabaseClient vectorDb = vectorClient .database (databaseName );
30
+ JsonVectorStore jsonVectorStore =
31
+ vectorDb .createVectorStore ("demo_product" , 14 );
20
32
21
- DefaultVectorStore vectorStore = vectorDb . createVectorStore ( "demo_product" , 14 );
33
+ // ======== INSERTIONS =========
22
34
35
+ jsonVectorStore .insert (new JsonDocument ("doc1" )
36
+ .put ("product_name" , "HealthyFresh - Beef raw dog food" )
37
+ .put ("product_price" , 12.99 )
38
+ .vector (new float []{1f , 0f , 1f , 1f , 1f , 1f , 0f , 0f , 0f , 0f , 0f , 0f , 0f , 0f }));
23
39
40
+ jsonVectorStore .insert (new JsonDocument ("doc2" )
41
+ .data (Map .of ("product_name" , "HealthyFresh - Chicken raw dog food" ))
42
+ .vector (new float []{1f , 1f , 1f , 1f , 1f , 0f , 0f , 0f , 0f , 0f , 0f , 0f , 0f , 0f }));
43
+
44
+ jsonVectorStore .insert (new JsonDocument ("doc3" )
45
+ .data ("{"
46
+ +" \" product_name\" : \" HealthyFresh - Chicken raw dog food\" , "
47
+ + " \" product_price\" : 9.99, "
48
+ + "}" )
49
+ .vector (new float []{1f , 1f , 1f , 1f , 1f , 0f , 0f , 0f , 0f , 0f , 0f , 0f , 0f , 0f }));
50
+
51
+ //jsonVectorStore.insert("{"
52
+ // + " \"_id\":\"doc4\","
53
+ // + " \"$vector\":[1f, 1f, 1f, 1f, 1f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f],"
54
+ // + " \"product_name\": \"HealthyFresh - Chicken raw dog food\", "
55
+ // + " \"product_price\": 9.99, "
56
+ // + "}");
57
+
58
+ // With ODM
59
+ VectorStore <Product > productVectorStore =
60
+ vectorDb .createVectorStore ("demo_product" , 14 , Product .class );
61
+
62
+ // 3 fields: id, payload, vector
63
+ productVectorStore .insert ("doc5" ,
64
+ new Product ("HealthyFresh - Beef raw dog food" , 12.99 ),
65
+ new float []{1f , 1f , 1f , 1f , 1f , 0f , 0f , 0f , 0f , 0f , 0f , 0f , 0f , 0f });
66
+
67
+ // build the "document" and insert the document
68
+ Document <Product > doc6 = new Document <>("doc6" ,
69
+ new Product ("HealthyFresh - Beef raw dog food" , 12.99 ),
70
+ new float []{1f , 1f , 1f , 1f , 1f , 0f , 0f , 0f , 0f , 0f , 0f , 0f , 0f , 0f });
71
+ productVectorStore .insert (doc6 );
72
+
73
+ }
74
+
75
+
76
+ @ Data
77
+ @ NoArgsConstructor
78
+ @ AllArgsConstructor
79
+ static class Product {
80
+ @ JsonProperty ("product_name" )
81
+ private String name ;
82
+ @ JsonProperty ("product_price" )
83
+ private Double price ;
24
84
}
25
85
}
0 commit comments