1
+ import com .datastax .astra .client .Collection ;
2
+ import com .datastax .astra .client .DataAPIClient ;
3
+ import com .datastax .astra .client .Database ;
4
+ import com .datastax .astra .client .model .Document ;
5
+ import com .datastax .astra .client .model .FindIterable ;
6
+
7
+ import static com .datastax .astra .client .model .SimilarityMetric .COSINE ;
8
+
9
+ public class Quickstart {
10
+
11
+ public static void main (String [] args ) {
12
+ // Loading Arguments
13
+ String astraToken = System .getenv ("ASTRA_DB_APPLICATION_TOKEN" );
14
+ String astraApiEndpoint = System .getenv ("ASTRA_DB_API_ENDPOINT" );
15
+
16
+ // Initialize the client. The keyspace parameter is optional if you use
17
+ // "default_keyspace".
18
+ DataAPIClient client = new DataAPIClient (astraToken );
19
+ System .out .println ("Connected to AstraDB" );
20
+
21
+ Database db = client .getDatabase (astraApiEndpoint , "default_keyspace" );
22
+ System .out .println ("Connected to Database." );
23
+ // end::init[]
24
+
25
+ // tag::collection[]
26
+ // Create a collection. The default similarity metric is cosine.
27
+ Collection <Document > collection = db
28
+ .createCollection ("vector_test" , 5 , COSINE );
29
+ System .out .println ("Created a collection" );
30
+ // end::collection[]
31
+
32
+ // tag::data[]
33
+ // Insert documents into the collection
34
+ collection .insertMany (
35
+ new Document ("1" )
36
+ .append ("text" , "ChatGPT integrated sneakers that talk to you" )
37
+ .vector (new float []{0.1f , 0.15f , 0.3f , 0.12f , 0.05f }),
38
+ new Document ("2" )
39
+ .append ("text" , "An AI quilt to help you sleep forever" )
40
+ .vector (new float []{0.45f , 0.09f , 0.01f , 0.2f , 0.11f }),
41
+ new Document ("3" )
42
+ .append ("text" , "A deep learning display that controls your mood" )
43
+ .vector (new float []{0.1f , 0.05f , 0.08f , 0.3f , 0.6f }));
44
+ System .out .println ("Inserted documents into the collection" );
45
+ // end::data[]
46
+
47
+ // tag::search[]
48
+ // Perform a similarity search
49
+ FindIterable <Document > resultsSet = collection .find (
50
+ new float []{0.15f , 0.1f , 0.1f , 0.35f , 0.55f },
51
+ 10
52
+ );
53
+ resultsSet .forEach (System .out ::println );
54
+ // end::search[]
55
+
56
+ // tag::cleanup[]
57
+ // Delete the collection
58
+ collection .drop ();
59
+ System .out .println ("Deleted the collection" );
60
+ // end::cleanup[]
61
+
62
+ // tag::end[]
63
+ }
64
+ }
65
+ // end::end[]
0 commit comments