Find distinct values

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

  • Python

  • TypeScript

  • Java

  • curl

Returns a list of the distinct values of the specified key.

The TypeScript client does not support this method. Instead, you can use Find rows, and then extract the desired values from the response.

Returns a list of the distinct values of the specified key.

This method has no literal equivalent in HTTP. Instead, you can use Find rows, and then extract the desired values from the response.

Parameters

  • Python

  • TypeScript

  • Java

  • curl

Use the distinct method, which belongs to the astrapy.Table class.

Method signature
distinct(
  key: str,
  *,
  filter: Dict[str, Any],
  general_method_timeout_ms: int,
  request_timeout_ms: int,
  timeout_ms: int,
) -> list[Any]
Name Type Summary

key

str

The key for which to find values. This can be:

  • A column name

  • An entry in a map column, such as metadata.language

  • An index in a list column, such as genres.2

See Examples for usage.

filter

dict

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.

Default: No filter

general_method_timeout_ms

int

Optional. The maximum time, in milliseconds, that the whole operation, which might involve multiple HTTP requests, can take.

This parameter is aliased as timeout_ms.

Default: The default value for the table. This default is 30 seconds unless you specified a different default when you initialized the Table or DataAPIClient object. For more information, see Timeout options.

request_timeout_ms

int

Optional. The maximum time, in milliseconds, that the client should wait for each underlying HTTP request.

Default: The default value for the table. This default is 30 seconds unless you specified a different default when you initialized the Table or DataAPIClient object. For more information, see Timeout options.

The TypeScript client does not support this method. Instead, you can use Find rows, and then extract the desired values from the response.

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.

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.

This method has no literal equivalent in HTTP. Instead, you can use Find rows, and then extract the desired values from the response.

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

  • Python

  • TypeScript

  • Java

  • curl

from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment

# Get an existing table
client = DataAPIClient(environment=Environment.HCD)
database = client.get_database(
    "API_ENDPOINT",
    token=UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
)
table = database.get_table("TABLE_NAME", keyspace="KEYSPACE_NAME")

# Find distinct values
result = table.distinct(
    "publication_year",
    filter={
        "$and": [
            {"is_checked_out": False},
            {"number_of_pages": {"$lt": 300}},
        ]
    },
)

print(result)

The TypeScript client does not support this method. Instead, you can use Find rows, and then extract the desired values from the response.

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;
import com.datastax.astra.client.databases.Database;
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
    DataAPIClient client = DataAPIClients.clientHCD("USERNAME", "PASSWORD");
    Database database = client.getDatabase("API_ENDPOINT", "KEYSPACE_NAME");
    Table<Row> table = database.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);
    }
  }
}

This method has no literal equivalent in HTTP. Instead, you can use Find rows, and then extract the desired values from the response.

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.

  • Python

  • TypeScript

  • Java

  • curl

from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment

# Get an existing table
client = DataAPIClient(environment=Environment.HCD)
database = client.get_database(
    "API_ENDPOINT",
    token=UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
)
table = database.get_table("TABLE_NAME", keyspace="KEYSPACE_NAME")

# Find distinct values
result = table.distinct("publication_year", filter={})

print(result)

The TypeScript client does not support this method. Instead, you can use Find rows, and then extract the desired values from the response.

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;
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
    DataAPIClient client = DataAPIClients.clientHCD("USERNAME", "PASSWORD");
    Database database = client.getDatabase("API_ENDPOINT", "KEYSPACE_NAME");
    Table<Row> table = database.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);
    }
  }
}

This method has no literal equivalent in HTTP. Instead, you can use Find rows, and then extract the desired values from the response.

Find distinct values of an entry in a map column

  • Python

  • TypeScript

  • Java

  • curl

To find distinct values for an entry in a map column, use dot notation.

from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment

# Get an existing table
client = DataAPIClient(environment=Environment.HCD)
database = client.get_database(
    "API_ENDPOINT",
    token=UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
)
table = database.get_table("TABLE_NAME", keyspace="KEYSPACE_NAME")

# Find distinct values
result = table.distinct(
    "metadata.language",
    filter={
        "$and": [
            {"is_checked_out": False},
            {"number_of_pages": {"$lt": 300}},
        ]
    },
)

print(result)

The TypeScript client does not support this method. Instead, you can use Find rows, and then extract the desired values from the response.

The Java client does not support this method for map columns.

This method has no literal equivalent in HTTP. Instead, you can use Find rows, and then extract the desired values from the response.

Find distinct values of an index in a list column

  • Python

  • TypeScript

  • Java

  • curl

To find distinct values for an index in a list column, use dot notation with the desired index.

from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment

# Get an existing table
client = DataAPIClient(environment=Environment.HCD)
database = client.get_database(
    "API_ENDPOINT",
    token=UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
)
table = database.get_table("TABLE_NAME", keyspace="KEYSPACE_NAME")

# Find distinct values
result = table.distinct(
    "genres.2",
    filter={
        "$and": [
            {"is_checked_out": False},
            {"number_of_pages": {"$lt": 300}},
        ]
    },
)

print(result)

The TypeScript client does not support this method. Instead, you can use Find rows, and then extract the desired values from the response.

The Java client does not support this method for list columns.

This method has no literal equivalent in HTTP. Instead, you can use Find rows, and then extract the desired values from the response.

Client reference

  • Python

  • TypeScript

  • Java

  • curl

For more information, see the client reference.

The TypeScript client does not support this method. Instead, you can use Find rows, and then extract the desired values from the response.

For more information, see the client reference.

Client reference documentation is not applicable for HTTP.

Was this helpful?

Give Feedback

How can we improve the documentation?

© 2025 DataStax, an IBM Company | 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: +1 (650) 389-6000, info@datastax.com