Find distinct values (Java)
|
Tables with the Data API are currently in public preview. Development is ongoing, and the features and functionality are subject to change. Hyper-Converged Database (HCD), and the use of such, is subject to the DataStax Preview Terms. |
Finds the distinct values of a column for rows in a table.
This method finds all rows that match the filter, or all rows if no filter is applied. There can be performance, latency, and billing implications if there are many matching rows.
|
Ready to write code? See the examples for this method to get started. If you are new to the Data API, check out the quickstart. |
Result
Returns a list of the distinct values of the specified key.
Parameters
Use the distinct method, which belongs to the com.datastax.astra.client.tables.Table class.
Method signature
<R> List<R> distinct(
String fieldName,
Filter filter,
Class<R> resultClass
)
<R> List<R> distinct(
String fieldName,
Filter filter,
Class<R> resultClass,
TableDistinctOptions options
)
| Name | Type | Summary |
|---|---|---|
|
|
The column for which to find values. Map, set, and list columns are not supported. |
|
Optional. An object that defines filter criteria using the Data API filter syntax. Only rows that match this filter criteria will be inspected. For a list of available filter operators and more examples, see Filter operators for tables (Java). Default: No filter |
|
|
|
Optional.
Represents the expected type of the column returned.
For example, if the target column is |
|
General API options for this operation, including the timeout. |
Examples
The following examples demonstrate how to find distinct values of a column for rows in a table.
Find distinct values of a column for rows that match a filter
import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.core.query.Filters;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.definition.rows.Row;
import java.util.Set;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
DataAPIClients.clientHCD("USERNAME", "PASSWORD")
.getDatabase("API_ENDPOINT", "KEYSPACE_NAME")
.getTable("TABLE_NAME");
// Find distinct values
Filter filter =
Filters.and(Filters.eq("is_checked_out", false), Filters.lt("number_of_pages", 300));
Set<String> result = table.distinct("publication_year", filter, String.class);
for (String fieldValue : result) {
System.out.println(fieldValue);
}
}
}
Find distinct values of a column for all rows
To find the distinct values of a column for all rows in the table, use an empty filter.
You should avoid this if you have a large number of rows.
import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.core.query.Filter;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.definition.rows.Row;
import java.util.Set;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
DataAPIClients.clientHCD("USERNAME", "PASSWORD")
.getDatabase("API_ENDPOINT", "KEYSPACE_NAME")
.getTable("TABLE_NAME");
// Find distinct values
Filter filter = new Filter();
Set<String> result = table.distinct("publication_year", filter, String.class);
for (String fieldValue : result) {
System.out.println(fieldValue);
}
}
}
Find distinct values of an entry in a map column
The Java client does not support this method for map columns.
Find distinct values of an index in a list column
The Java client does not support this method for list columns.
Client reference
For more information, see the client reference.