11package com .datastax .astra .client ;
22
3+ import com .datastax .astra .client .core .auth .AWSEmbeddingHeadersProvider ;
4+ import com .datastax .astra .client .core .auth .EmbeddingAPIKeyHeaderProvider ;
5+ import com .datastax .astra .client .core .auth .UsernamePasswordTokenProvider ;
6+ import com .datastax .astra .client .core .http .HttpClientOptions ;
7+ import com .datastax .astra .client .core .http .HttpProxy ;
38import com .datastax .astra .client .core .options .DataAPIClientOptions ;
9+ import com .datastax .astra .client .core .options .TimeoutOptions ;
410import com .datastax .astra .client .databases .Database ;
511import com .datastax .astra .client .databases .DatabaseOptions ;
12+ import com .datastax .astra .internal .command .CommandObserver ;
13+ import com .datastax .astra .internal .command .ExecutionInfos ;
614
15+ import java .net .http .HttpClient ;
16+ import java .time .Duration ;
717import java .util .UUID ;
818
919public class Connecting {
10- public static void main (String [] args ) {
11- // Preferred Access with DataAPIClient (default options)
12- DataAPIClient client = new DataAPIClient ("TOKEN" );
13-
14- // Overriding the default options
15- DataAPIClient client1 = new DataAPIClient ("TOKEN" , new DataAPIClientOptions ());
16-
17- // Access the Database from its endpoint
18- Database db1 = client1 .getDatabase ("*API_ENDPOINT*" );
19- Database db2 = client1 .getDatabase ("*API_ENDPOINT*" , new DatabaseOptions ().keyspace ("*KEYSPACE*" ));
20-
21- // Access the Database from its endpoint
22- UUID databaseId = UUID .fromString ("f5abf92f-ff66-48a0-bbc2-d240bc25dc1f" );
23- Database db3 = client .getDatabase (databaseId );
24- Database db4 = client .getDatabase (databaseId ,
25- new DatabaseOptions ().keyspace ("*KEYSPACE*" ));
26- Database db5 = client .getDatabase (databaseId , "us-east-2" ,
27- new DatabaseOptions ().keyspace ("*KEYSPACE*" ));
28- db5 .useKeyspace ("yet_another" );
29-
30- }
20+ public static void main (String [] args ) {
21+
22+ // Preferred Access with DataAPIClient (default options) ASTRA
23+ DataAPIClient clientWithAstra =
24+ new DataAPIClient ("AstraCS:TOKEN" );
25+
26+ // If you work locally, create a token from username and password
27+ String localToken =
28+ new UsernamePasswordTokenProvider ("username" , "password" ).getToken ();
29+ DataAPIClient clientlocal = new DataAPIClient (localToken );
30+
31+ // Specialization of the DataAPIClient
32+ DataAPIClientOptions options = new DataAPIClientOptions ()
33+ .destination (DataAPIDestination .ASTRA ) ; // HCD, DSE, CASSANDRA
34+
35+ // Specialization of the HTTP CLIENT
36+ HttpClientOptions httpClientOptions = new HttpClientOptions ()
37+ // RETRIES => default is not retry
38+ .retryCount (3 ).retryDelay (Duration .ofMillis (200 ))
39+ // Http Redirect
40+ .httpRedirect (HttpClient .Redirect .NORMAL )
41+ // Http version
42+ .httpVersion (HttpClient .Version .HTTP_2 )
43+ // default is no proxy
44+ .httpProxy (new HttpProxy ().hostname ("localhost" ).port (8080 ));
45+ options .httpClientOptions (httpClientOptions );
46+
47+ // Specialization of the TIMEOUTS
48+ TimeoutOptions timeoutsOptions = new TimeoutOptions ()
49+ // Collection Admin (DDL)
50+ .collectionAdminTimeoutMillis (5000 )
51+ .collectionAdminTimeout (Duration .ofMillis (5000 ))
52+ // Table Admin (DDL)
53+ .tableAdminTimeoutMillis (5000 )
54+ .tableAdminTimeout (Duration .ofMillis (5000 ))
55+ // Database Admin (DDL)
56+ .databaseAdminTimeoutMillis (15000 )
57+ .databaseAdminTimeout (Duration .ofMillis (15000 ))
58+ // Generation operation (DML)
59+ .generalMethodTimeoutMillis (1000 )
60+ .generalMethodTimeout (Duration .ofMillis (1000 ))
61+ // Specialization of 1 http request when multiple are done (insert Many)
62+ .requestTimeoutMillis (200 )
63+ .requestTimeout (Duration .ofMillis (200 ))
64+ //HTTP Connect delay
65+ .connectTimeoutMillis (100 )
66+ .connectTimeout (Duration .ofMillis (100 ));
67+ options .timeoutOptions (timeoutsOptions );
68+
69+ // Loggers and observers
70+ options .addObserver ("my_dummy_logger" , new CommandObserver () {
71+ @ Override
72+ public void onCommand (ExecutionInfos executionInfo ) {
73+ System .out .println ("Command executed: " + executionInfo .getCommand ().getName ());
74+ }
75+ });
76+ options .logRequests (); // <-- get you a sl4j logger at debug level
77+
78+ // Add your application in the chain of callers in the header
79+ options .addCaller ("MySampleApplication" , "1.0.0" );
80+
81+ // Add an header to computer embeddings externally (integration)
82+ options .embeddingAuthProvider (new EmbeddingAPIKeyHeaderProvider ("key_embeddings" ));
83+ options .embeddingAuthProvider (new AWSEmbeddingHeadersProvider ("aws_access_key" , "aws_secret_key" ));
84+
85+ // Add Headers to call for admin or database operations
86+ options .addAdminAdditionalHeader ("X-My-Header" , "MyValue" );
87+ options .addDatabaseAdditionalHeader ("X-My-Header" , "MyValue" );
88+
89+ // Create the client with the options
90+ DataAPIClient client1 = new DataAPIClient ("token" , options );
91+
92+ // -------------------------------
93+ // -- Initializing Database ------
94+ // -------------------------------
95+
96+ // Access the Database from its endpoint
97+ Database db1 = client1 .getDatabase ("*API_ENDPOINT*" );
98+ Database db2 = client1 .getDatabase ("*API_ENDPOINT*" , new DatabaseOptions ()
99+ .keyspace ("*KEYSPACE*" ));
100+
101+ // (ASTRA ONLY !) Access the Database from IDS
102+ UUID databaseId = UUID .fromString ("f5abf92f-ff66-48a0-bbc2-d240bc25dc1f" );
103+ Database db3 = client1 .getDatabase (databaseId );
104+ Database db4 = client1 .getDatabase (databaseId , new DatabaseOptions ()
105+ .keyspace ("*KEYSPACE*" ));
106+ Database db5 = client1 .getDatabase (databaseId , "us-east-2" , new DatabaseOptions ()
107+ .keyspace ("*KEYSPACE*" ));
108+ db5 .useKeyspace ("yet_another" );
109+
110+ }
31111}
0 commit comments