Skip to content

Commit

Permalink
update samples
Browse files Browse the repository at this point in the history
  • Loading branch information
clun committed Dec 10, 2024
1 parent 15c5e86 commit 78c7aea
Show file tree
Hide file tree
Showing 19 changed files with 145 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ public void should_insert_many() {
@Test
public void should_delete_many() {
Table<TableCompositeRow> t = getDatabase().getTable(TABLE_COMPOSITE, TableCompositeRow.class);
/*

for(int i = 0; i < 1000; i+=10) {
t.insertMany(
new TableCompositeRow(i, "a", "b"),
Expand All @@ -484,14 +484,14 @@ public void should_delete_many() {
new TableCompositeRow(i+8, "c", "c"),
new TableCompositeRow(i+9, "a", "b"));
}
*/

// Position a retry count at 3 default is 1
HttpClientOptions httpClientOptions = new HttpClientOptions()
.retryCount(3)
.retryDelay(Duration.ofMillis(100));
TableDeleteManyOptions deleteManyOptions = new TableDeleteManyOptions()
.httpClientOptions(httpClientOptions)
.timeout(Duration.ofMillis(10));
.timeout(Duration.ofMillis(1000));

t.deleteMany(null, deleteManyOptions);
}
Expand Down
122 changes: 101 additions & 21 deletions examples/src/main/java/com/datastax/astra/client/Connecting.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,111 @@
package com.datastax.astra.client;

import com.datastax.astra.client.core.auth.AWSEmbeddingHeadersProvider;
import com.datastax.astra.client.core.auth.EmbeddingAPIKeyHeaderProvider;
import com.datastax.astra.client.core.auth.UsernamePasswordTokenProvider;
import com.datastax.astra.client.core.http.HttpClientOptions;
import com.datastax.astra.client.core.http.HttpProxy;
import com.datastax.astra.client.core.options.DataAPIClientOptions;
import com.datastax.astra.client.core.options.TimeoutOptions;
import com.datastax.astra.client.databases.Database;
import com.datastax.astra.client.databases.DatabaseOptions;
import com.datastax.astra.internal.command.CommandObserver;
import com.datastax.astra.internal.command.ExecutionInfos;

import java.net.http.HttpClient;
import java.time.Duration;
import java.util.UUID;

public class Connecting {
public static void main(String[] args) {
// Preferred Access with DataAPIClient (default options)
DataAPIClient client = new DataAPIClient("TOKEN");

// Overriding the default options
DataAPIClient client1 = new DataAPIClient("TOKEN", new DataAPIClientOptions());

// Access the Database from its endpoint
Database db1 = client1.getDatabase("*API_ENDPOINT*");
Database db2 = client1.getDatabase("*API_ENDPOINT*", new DatabaseOptions().keyspace("*KEYSPACE*"));

// Access the Database from its endpoint
UUID databaseId = UUID.fromString("f5abf92f-ff66-48a0-bbc2-d240bc25dc1f");
Database db3 = client.getDatabase(databaseId);
Database db4 = client.getDatabase(databaseId,
new DatabaseOptions().keyspace("*KEYSPACE*"));
Database db5 = client.getDatabase(databaseId, "us-east-2",
new DatabaseOptions().keyspace("*KEYSPACE*"));
db5.useKeyspace("yet_another");

}
public static void main(String[] args) {

// Preferred Access with DataAPIClient (default options) ASTRA
DataAPIClient clientWithAstra =
new DataAPIClient("AstraCS:TOKEN");

// If you work locally, create a token from username and password
String localToken =
new UsernamePasswordTokenProvider("username", "password").getToken();
DataAPIClient clientlocal = new DataAPIClient(localToken);

// Specialization of the DataAPIClient
DataAPIClientOptions options = new DataAPIClientOptions()
.destination(DataAPIDestination.ASTRA) ; // HCD, DSE, CASSANDRA

// Specialization of the HTTP CLIENT
HttpClientOptions httpClientOptions = new HttpClientOptions()
// RETRIES => default is not retry
.retryCount(3).retryDelay(Duration.ofMillis(200))
// Http Redirect
.httpRedirect(HttpClient.Redirect.NORMAL)
// Http version
.httpVersion(HttpClient.Version.HTTP_2)
// default is no proxy
.httpProxy(new HttpProxy().hostname("localhost").port(8080));
options.httpClientOptions(httpClientOptions);

// Specialization of the TIMEOUTS
TimeoutOptions timeoutsOptions = new TimeoutOptions()
// Collection Admin (DDL)
.collectionAdminTimeoutMillis(5000)
.collectionAdminTimeout(Duration.ofMillis(5000))
// Table Admin (DDL)
.tableAdminTimeoutMillis(5000)
.tableAdminTimeout(Duration.ofMillis(5000))
// Database Admin (DDL)
.databaseAdminTimeoutMillis(15000)
.databaseAdminTimeout(Duration.ofMillis(15000))
// Generation operation (DML)
.generalMethodTimeoutMillis(1000)
.generalMethodTimeout(Duration.ofMillis(1000))
// Specialization of 1 http request when multiple are done (insert Many)
.requestTimeoutMillis(200)
.requestTimeout(Duration.ofMillis(200))
//HTTP Connect delay
.connectTimeoutMillis(100)
.connectTimeout(Duration.ofMillis(100));
options.timeoutOptions(timeoutsOptions);

// Loggers and observers
options.addObserver("my_dummy_logger", new CommandObserver() {
@Override
public void onCommand(ExecutionInfos executionInfo) {
System.out.println("Command executed: " + executionInfo.getCommand().getName());
}
});
options.logRequests(); // <-- get you a sl4j logger at debug level

// Add your application in the chain of callers in the header
options.addCaller("MySampleApplication", "1.0.0");

// Add an header to computer embeddings externally (integration)
options.embeddingAuthProvider(new EmbeddingAPIKeyHeaderProvider("key_embeddings"));
options.embeddingAuthProvider(new AWSEmbeddingHeadersProvider("aws_access_key", "aws_secret_key"));

// Add Headers to call for admin or database operations
options.addAdminAdditionalHeader("X-My-Header", "MyValue");
options.addDatabaseAdditionalHeader("X-My-Header", "MyValue");

// Create the client with the options
DataAPIClient client1 = new DataAPIClient("token", options);

// -------------------------------
// -- Initializing Database ------
// -------------------------------

// Access the Database from its endpoint
Database db1 = client1.getDatabase("*API_ENDPOINT*");
Database db2 = client1.getDatabase("*API_ENDPOINT*", new DatabaseOptions()
.keyspace("*KEYSPACE*"));

// (ASTRA ONLY !) Access the Database from IDS
UUID databaseId = UUID.fromString("f5abf92f-ff66-48a0-bbc2-d240bc25dc1f");
Database db3 = client1.getDatabase(databaseId);
Database db4 = client1.getDatabase(databaseId, new DatabaseOptions()
.keyspace("*KEYSPACE*"));
Database db5 = client1.getDatabase(databaseId, "us-east-2", new DatabaseOptions()
.keyspace("*KEYSPACE*"));
db5.useKeyspace("yet_another");

}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.datastax.astra.client.tables;

import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.databases.Database;
import com.datastax.astra.client.tables.commands.options.CreateIndexOptions;
Expand All @@ -10,8 +11,8 @@

public class CreateIndex {
public static void main(String[] args) {
//Database db = new DataAPIClient("token").getDatabase("endpoint");
Database db = DataAPIClients.localDbWithDefaultKeyspace();
Database db = new DataAPIClient("token").getDatabase("endpoint");

Table<Row> tableGames = db.getTable("games");

tableGames.createIndex("score_index","score");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@

public class CreateVectorIndex {
public static void main(String[] args) {
//Database db = new DataAPIClient("token").getDatabase("endpoint");
Database db = DataAPIClients.localDbWithDefaultKeyspace();
Database db = new DataAPIClient("token").getDatabase("endpoint");
Table<Row> tableGames = db.getTable("games");

//tableGames.createVectorIndex("m_vector_index", "m_vector");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.datastax.astra.client.tables;

import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.databases.Database;
Expand All @@ -13,8 +14,7 @@

public class DeleteMany {
public static void main(String[] args) {
Database db = DataAPIClients.localDbWithDefaultKeyspace();
// Database astraDb = new DataAPIClient(token).getDatabase(endpoint);
Database db = new DataAPIClient("token").getDatabase("endpoint");

Table<Row> tableRow = db.getTable("games");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.datastax.astra.client.tables;

import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.databases.Database;
Expand All @@ -15,8 +16,7 @@

public class DeleteOne {
public static void main(String[] args) {
Database db = DataAPIClients.localDbWithDefaultKeyspace();
// Database astraDb = new DataAPIClient(token).getDatabase(endpoint);
Database db = new DataAPIClient("token").getDatabase("endpoint");

Table<Row> tableRow = db.getTable("games");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.datastax.astra.client.tables;

import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Filters;
Expand All @@ -17,8 +18,7 @@

public class Distinct {
public static void main(String[] args) {
Database db = DataAPIClients.localDbWithDefaultKeyspace();
// Database astraDb = new DataAPIClient(token).getDatabase(endpoint);
Database db = new DataAPIClient("token").getDatabase("endpoint");

Table<Row> table = db.getTable("games");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.datastax.astra.client.tables;

import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.databases.Database;
import com.datastax.astra.client.tables.commands.options.CreateIndexOptions;
Expand All @@ -11,8 +12,7 @@

public class DropIndex {
public static void main(String[] args) {
//Database db = new DataAPIClient("token").getDatabase("endpoint");
Database db = DataAPIClients.localDbWithDefaultKeyspace();
Database db = new DataAPIClient("token").getDatabase("endpoint");

db.dropTableIndex("score_index");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
package com.datastax.astra.client.tables;

import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Sort;
import com.datastax.astra.client.core.vector.DataAPIVector;
import com.datastax.astra.client.databases.Database;
import com.datastax.astra.client.tables.commands.options.TableFindOneOptions;
import com.datastax.astra.client.tables.commands.options.TableFindOptions;
import com.datastax.astra.client.tables.cursor.TableCursor;
import com.datastax.astra.client.tables.definition.rows.Row;

import java.util.List;
import java.util.Optional;

import static com.datastax.astra.client.core.query.Filters.and;
import static com.datastax.astra.client.core.query.Filters.eq;
import static com.datastax.astra.client.core.query.Filters.gt;
import static com.datastax.astra.client.core.query.Projection.include;

public class FindMany {
public static void main(String[] args) {
Database db = DataAPIClients.localDbWithDefaultKeyspace();
// Database astraDb = new DataAPIClient(token).getDatabase(endpoint);
Database db = new DataAPIClient("token").getDatabase("endpoint");

Table<Row> table = db.getTable("games");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.datastax.astra.client.tables;

import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Projection;
Expand All @@ -19,8 +20,7 @@

public class FindOne {
public static void main(String[] args) {
Database db = DataAPIClients.localDbWithDefaultKeyspace();
// Database astraDb = new DataAPIClient(token).getDatabase(endpoint);
Database db = new DataAPIClient("token").getDatabase("endpoint");

Table<Row> tableRow = db.getTable("games");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.datastax.astra.client.tables;

import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Filters;
Expand All @@ -22,7 +23,7 @@
public class FindWithVectorize {
public static void main(String[] args) {
// Database astraDb = new DataAPIClient(token).getDatabase(endpoint);
Database db = DataAPIClients.localDbWithDefaultKeyspace();
Database db = new DataAPIClient("token").getDatabase("endpoint");

// Create a table with a vector column (+vectorize)
VectorServiceOptions openAI = new VectorServiceOptions()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package com.datastax.astra.client.tables;

import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.DataAPIDestination;
import com.datastax.astra.client.core.auth.UsernamePasswordTokenProvider;
import com.datastax.astra.client.core.options.DataAPIClientOptions;
import com.datastax.astra.client.core.vector.DataAPIVector;
import com.datastax.astra.client.databases.Database;
import com.datastax.astra.client.databases.DatabaseOptions;
import com.datastax.astra.client.tables.commands.options.TableInsertManyOptions;
import com.datastax.astra.client.tables.commands.results.TableInsertManyResult;
import com.datastax.astra.client.tables.commands.results.TableInsertOneResult;
Expand All @@ -18,7 +23,8 @@

public class InsertManyRows {
public static void main(String[] args) {
Database db = DataAPIClients.localDbWithDefaultKeyspace();
Database db = new DataAPIClient("token").getDatabase("endpoint");

Table<Row> table = db.getTable("games");

TableInsertManyOptions options = new TableInsertManyOptions()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.datastax.astra.client.tables;

import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.core.query.Filters;
import com.datastax.astra.client.core.vectorize.VectorServiceOptions;
Expand All @@ -16,8 +17,7 @@

public class InsertOneWithVectorize {
public static void main(String[] args) {
// Database astraDb = new DataAPIClient(token).getDatabase(endpoint);
Database db = DataAPIClients.localDbWithDefaultKeyspace();
Database db = new DataAPIClient("token").getDatabase("endpoint");

// Create a table with a vector column (+vectorize)
VectorServiceOptions openAI = new VectorServiceOptions()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.datastax.astra.client.tables;

import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.core.vector.DataAPIVector;
import com.datastax.astra.client.databases.Database;
Expand All @@ -15,7 +16,7 @@

public class InsertRow {
public static void main(String[] args) {
Database db = DataAPIClients.localDbWithDefaultKeyspace();
Database db = new DataAPIClient("token").getDatabase("endpoint");
Table<Row> table = db.getTable("games");
TableInsertOneResult result = table.insertOne(new Row()
.addText("match_id", "mtch_0")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.datastax.astra.client.tables;

import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.core.vector.SimilarityMetric;
import com.datastax.astra.client.databases.Database;
Expand All @@ -16,8 +17,7 @@

public class ListIndexes {
public static void main(String[] args) {
//Database db = new DataAPIClient("token").getDatabase("endpoint");
Database db = DataAPIClients.localDbWithDefaultKeyspace();
Database db = new DataAPIClient("token").getDatabase("endpoint");
Table<Row> tableGames = db.getTable("games");

//List<TableIndexDescriptor> indexes = tableGames.listIndexes();
Expand Down
Loading

0 comments on commit 78c7aea

Please sign in to comment.