Skip to content

Commit

Permalink
samples for updateOne
Browse files Browse the repository at this point in the history
  • Loading branch information
clun committed Dec 5, 2024
1 parent 241b110 commit ce35c3c
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.datastax.astra.client.core.options.BaseOptions;
import com.datastax.astra.client.core.paging.Page;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Projection;
import com.datastax.astra.client.databases.Database;
import com.datastax.astra.client.exceptions.DataAPIException;
import com.datastax.astra.client.tables.commands.AlterTableOperation;
Expand Down Expand Up @@ -730,18 +731,33 @@ public Page<T> findPage(Filter filter, TableFindOptions options) {
// --- distinct ----
// -------------------------

/*
Cursor could take a boolean in the constructor rto mark the distinct
public <R> TableCursor<T, R> distinct(String fieldName, TableFindOptions options, Class<R> resultClass) {
throw new UnsupportedOperationException("distinct is not implemented yet");
/**
* Return a list of distinct values for the given field name.
*
* @param fieldName
* name of the field
* @param filter
* filter to apply
* @param resultClass
* class of the result
* @return
* list of distinct values
* @param <R>
* type of the result
*/
public <R> List<R> distinct(String fieldName, Filter filter, Class<R> resultClass) {
Assert.hasLength(fieldName, "fieldName");
Assert.notNull(resultClass, "resultClass");
// Building a convenient find options
TableFindOptions options = new TableFindOptions()
.projection(Projection.include(fieldName));
// Exhausting the list of distinct values
return find(filter, options, Row.class).toList().stream()
.map(row -> row.get(fieldName, resultClass))
.distinct()
.toList();
}

public TableCursor<T, T> distinct(String fieldName, TableFindOptions options) {
return distinct(fieldName, null, getRowClass());
}*/

// -------------------------
// --- updateOne ----
// -------------------------
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.datastax.astra.client.tables;

import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.databases.Database;
import com.datastax.astra.client.tables.commands.options.TableDeleteManyOptions;
import com.datastax.astra.client.tables.commands.options.TableDeleteOneOptions;
import com.datastax.astra.client.tables.definition.rows.Row;

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.gte;

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

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

// Update
Filter filter = and(
eq("match_id", "fight7"),
eq("round", 2));

tableRow.deleteMany(filter);
tableRow.deleteMany(filter, new TableDeleteManyOptions()
.timeout(1000));

Filter filter2 = and(
eq("match_id", "fight5"),
gte("round", 5));
tableRow.deleteMany(filter2);

}

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

import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.databases.Database;
import com.datastax.astra.client.tables.commands.TableUpdateOperation;
import com.datastax.astra.client.tables.commands.options.TableDeleteOneOptions;
import com.datastax.astra.client.tables.commands.options.TableUpdateOneOptions;
import com.datastax.astra.client.tables.definition.rows.Row;

import java.util.Set;

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

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

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

// Update
Filter filter = and(
eq("match_id", "fight7"),
eq("round", 2));

tableRow.deleteOne(filter);
tableRow.deleteOne(filter, new TableDeleteOneOptions()
.timeout(1000));

}

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

import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Sort;
import com.datastax.astra.client.databases.Database;
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 static com.datastax.astra.client.core.query.Filters.eq;
import static com.datastax.astra.client.core.query.Projection.include;

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

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

// Show you all match id in the table
List<Row> matches = table.find(null, new TableFindOptions()
.projection(include("match_id"))).toList();
matches.forEach(System.out::println);

// Show you the distinct match id in the table
table.distinct("match_id", null, String.class)
.forEach(System.out::println);
}
}

0 comments on commit ce35c3c

Please sign in to comment.