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 |
---|---|---|
|
|
The key for which to find values. This can be:
See Examples for usage. |
|
|
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 |
|
|
Optional. The maximum time, in milliseconds, that the whole operation, which might involve multiple HTTP requests, can take. This parameter is aliased as Default: The default value for the table.
This default is 30 seconds unless you specified a different default when you initialized the |
|
|
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 |
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 |
---|---|---|
|
|
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. 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. |
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.