For example, we can use
select count(*) from student_database;
to calculate the number of rows in a table.
But how do we calculate the number of tables in a keyspace?
DESCRIBE TABLES;
gives you the list of all tables in that keyspace.
And for a Cassandra 2.x (and lower) answer:
SELECT COUNT(*) FROM system.schema_columnfamilies
WHERE keyspace_name='your keyspace';
SELECT count(*) FROM system_schema.tables WHERE keyspace_name='your keyspace'
The above query will work in cassandra 3.0 and above
rows = session.execute("SELECT count(*) FROM system_schema.tables WHERE keyspace_name = 'your_keyspace_name'")
print(list(rows))
Result:
[Row(count=2)]
Related
I am trying to find a way to determine if the table is empty in Cassandra DB.
cqlsh> SELECT * from examples.basic ;
key | value
-----+-------
(0 rows)
I am running count(*) to get the value of the number of rows , but I am getting warning message, So I wanted to know if there is any better way to check if the table is empty(zero rows).
cqlsh> SELECT count(*) from examples.basic ;
count
-------
0
(1 rows)
Warnings :
Aggregation query used without partition key
cqlsh>
Aggregations, like count, can be an overkill for what you are trying to accomplish, specially with the star wildcard, as if there is any data on your table, the query will need to do a full table scan. This can be quite expensive if you have several records.
One way to get the result you are looking for is the query
cqlsh> SELECT key FROM keyspace1.table1 LIMIT 1;
Empty table:
The resultset will be empty
cqlsh> SELECT key FROM keyspace1.table1 LIMIT 1;
key
-----
(0 rows)
Table with data:
The resultset will have a record
cqlsh> SELECT key FROM keyspace1.table1 LIMIT 1;
key
----------------------------------
uL24bhnsHYRX8wZItWM6xKdS0WLvDsgi
(1 rows)
I have a cassandra database in which columns can be added or removed based on the application need. The column names start with the prefix RSSI. I was wondering if it is possible to select all columns where the column name is like %RSSI%. In MYSQL you can do something like select count(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name ='MACTrain' AND column_name LIKE '%RSSI%'. Is it possible in cassandra ? If not what can be a solution to select columns based on a specific wildcard.
You can obtain the columns metadata of a table by querying the system keyspace:
select * from system.schema_columns
where keyspace_name = 'yourks' and columnfamily_name = 'yourtable';
For Cassandra v3.0 and above, you can use the new system_schema keyspace:
select * from system_schema.columns
where keyspace_name = 'yourks' and table_name = 'yourtable';
I want to get data between two dates '2015-07-11 12:00:00' and '2015-07-12 15:00:00'. The column which stores date and time is timeuuid.
What will be the query? I am new to cassandra.
http://docs.datastax.com/en/cql/3.0/cql/cql_reference/timeuuid_functions_r.html
SELECT * FROM myTable
WHERE t > maxTimeuuid('2013-01-01 00:05+0000')
AND t < minTimeuuid('2013-02-02 10:00+0000')
I am NoSQL n00b, and just trying things out. I have the following keyspace with a single table in cassandra 2.0.2
CREATE KEYSPACE PersonDB WITH replication = {
'class': 'SimpleStrategy',
'replication_factor': '1'
};
USE PersonDB;
CREATE TABLE Persons (
id int,
lastname text,
firstname text,
PRIMARY KEY (id)
)
I have close to 500 entries in the Persons table. I want to select any random row from the table. Is there an efficient way to do it in CQL? I am using groovy to invoke APIs exposed by datastax.
If want to get "any" row you can just use LIMIT.
select * from persons LIMIT 1;
You would get the row with the lower hash of the partition key (id).
It will not be random, it will depend on your partitioner, but you would get A row.
I'm wondering if there is a query in CQL3 that allows you to get column names of a specific columnfamily that has a static schema?
Thanks in advance :)
If you want to get column names of a specific table with CQL3 then i guess try this
select * from system.schema_columns WHERE keyspace_name='#KS' AND columnfamily_name='#CF' allow filtering;
Note: keyspace_name in the where clause is optional. Its mainly used for better filtration purpose (say, table with the same name in multiple keyspace)
You could use the system keyspace to do this:
SELECT column_name FROM system.schema_columnfamilies
WHERE keyspace_name = 'testks' AND columnfamily_name = 'testcf';
Output in cqlsh (using cql3):
column_name
-------------
password
You can work out the key for the column by using:
SELECT key_aliases FROM system.schema_columnfamilies WHERE keyspace_name='testks'
AND columnfamily_name='testcf';
Output:
key_aliases
--------------
["username"]
From my latest test, we should use schema_columns, rather than schema_columnfamilies to get all the column names. schema_columnfamilies could be used for getting table names.
Get column names:
SELECT column_name FROM system.schema_columns WHERE keyspace_name = 'KeySpaceName' AND columnfamily_name = 'TableName';
Get column family names, i.e., table names:
select columnfamily_name from system.schema_columnfamilies where keyspace_name='KeySpaceName';
As per the latest Documentation of Cassandra 3.x, all above answers won't work and now the query to show columns would be like this.
SELECT * FROM system_schema.columns WHERE keyspace_name = 'xxxx' AND table_name = 'xxx';