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

fieldName

String

The column for which to find values. Map, set, and list columns are not supported.

filter

Filter

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

columnClass

Class<?>

Optional. Represents the expected type of the column returned. For example, if the target column is text or ascii, then the returned value type is String.class.

options

TableDistinctOptions

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.

Was this helpful?

Give Feedback

How can we improve the documentation?

© Copyright IBM Corporation 2026 | Privacy policy | Terms of use Manage Privacy Choices

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries. Kubernetes is the registered trademark of the Linux Foundation.

General Inquiries: Contact IBM