Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cassandra 2 - list existing indexes with CQL 3

Tags:

cassandra

cql

Is there a CQL query to list all existing indexes for particular key space, or column family?

like image 881
Maciej Miklas Avatar asked Jan 13 '14 13:01

Maciej Miklas


4 Answers

Assuming you have a table named moviesongs, firing a statement as DESCRIBE moviesongs; you will get the following output:

cqlsh:practice> describe moviesongs ;

CREATE TABLE practice.moviesongs (
    moviename text,
    year int,
    songname text,
    songnum int,
    PRIMARY KEY (moviename, year)
) WITH CLUSTERING ORDER BY (year DESC)
    AND bloom_filter_fp_chance = 0.01
    AND caching = {'keys': 'ALL', 'rows_per_partition': 'NONE'}
    AND comment = ''
    AND compaction = {'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy', 'max_threshold': '32', 'min_threshold': '4'}
    AND compression = {'chunk_length_in_kb': '64', 'class': 'org.apache.cassandra.io.compress.LZ4Compressor'}
    AND crc_check_chance = 1.0
    AND dclocal_read_repair_chance = 0.1
    AND default_time_to_live = 0
    AND gc_grace_seconds = 864000
    AND max_index_interval = 2048
    AND memtable_flush_period_in_ms = 0
    AND min_index_interval = 128
    AND read_repair_chance = 0.0
    AND speculative_retry = '99PERCENTILE';
CREATE INDEX index1 ON practice.moviesongs (year);

Look at the last line, it describes the index on the moviesongs table.

like image 67
tallboredpanda Avatar answered Oct 03 '22 17:10

tallboredpanda


You can retrieve primary keys and secondary indexes using the system keyspace:

SELECT column_name, index_name, index_options, index_type, component_index 
FROM system.schema_columns 
WHERE keyspace_name='samplekp'AND columnfamily_name='sampletable';

Taking, for example, the following table declaration:

CREATE TABLE sampletable (
key text,
date timestamp,
value1 text,
value2 text,
PRIMARY KEY(key, date));

CREATE INDEX ix_sample_value2 ON sampletable (value2);

The query mentioned above would get something this results:

 column_name | index_name       | index_options | index_type | component_index
-------------+------------------+---------------+------------+-----------------
        date |             null |          null |       null |               0
         key |             null |          null |       null |            null
      value1 |             null |          null |       null |               1
      value2 | ix_sample_value2 |            {} | COMPOSITES |               1
like image 18
jorgebg Avatar answered Oct 13 '22 21:10

jorgebg


Simplest way would be to use DESC command.

DESC TABLE "Table_Name" gets what you want

like image 10
Umesh S Padashetty Avatar answered Oct 13 '22 21:10

Umesh S Padashetty


cqlsh:system> show version
[cqlsh 5.0.1 | Cassandra 2.1.2-SNAPSHOT | CQL spec 3.2.0 | Native protocol v3]


cqlsh:system> desc table "IndexInfo"

CREATE TABLE system."IndexInfo" (
table_name text,
index_name text,
"" blob,
PRIMARY KEY (table_name, index_name)
) WITH COMPACT STORAGE
AND CLUSTERING ORDER BY (index_name ASC)
AND bloom_filter_fp_chance = 0.01
AND caching = '{"keys":"ALL", "rows_per_partition":"NONE"}'
AND comment = 'indexes that have been completed'
AND compaction = {'min_threshold': '4', 'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy', 'max_threshold': '32'}
AND compression = {'sstable_compression': 'org.apache.cassandra.io.compress.LZ4Compressor'}
AND dclocal_read_repair_chance = 0.0
AND default_time_to_live = 0
AND gc_grace_seconds = 0
AND max_index_interval = 2048
AND memtable_flush_period_in_ms = 3600000
AND min_index_interval = 128
AND read_repair_chance = 0.0
AND speculative_retry = '99.0PERCENTILE';

cqlsh:system> select * from "IndexInfo";

 table_name | index_name
------------+---------------------------------
    musicdb | performer.performer_country_key
    musicdb |   performer.performer_style_key
    musicdb |       user.user_preferences_key

(3 rows)
like image 2
Franco Sonaglioni Avatar answered Oct 13 '22 21:10

Franco Sonaglioni