Do you want to get a distinct count of records in Apache Cassandra over a time range? Well tough luck, unlike traditional SQL databases, Cassandra does not support COUNT(DISTINCT ...) queries efficiently, especially across time ranges.
What you want to do is use a command such as the following from within your Cassandra database server and export the output to a file.
cqlsh -u <username> -p '<password>' -e "SELECT id_column FROM <database_name>.<table_name>
WHERE timestamp_column > timestamp AND timestamp_column < timestamp ALLOW FILTERING" >
id_date.txt
Then you can use a Linux command such as the following to sort out the unique ID values and remove duplicates.
grep -Ev -- '---|id_column|^$' id_date.txt | awk -F'|' '{print $1}' | tr -d ' ' | sort -u >
unique_ids.txt
The command works as follows:
grep -Ev -- '---|id_column|^$'
Removes headers, separators, and empty lines.
awk -F'|'
Extracts the first column.
tr -d ' '
Removes unwanted spaces.
sort -u
Sorts the output and removes duplicate IDs.
You can then count those unique IDs with the following command:
cat unique_ids.txt | wc -l
And there you have it... a DISTINCT count of the ID column over your chosen time frame!
#cassandra #databases
0 Comments