Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list all cassandra tables

There are many tables in cassandra database, which contain column titled user_id. The values user_id are referred to user stored in table users. As some users are deleted, I would like to delete orphan records in all tables that contain column titled user_id.

Is there a way to list all tables using CassandraSQLContext or any other built-in method or custom procedure in order to avoid explicitly defining the list of tables?

like image 275
Niko Gamulin Avatar asked Aug 01 '16 10:08

Niko Gamulin


People also ask

How do you describe a table in Cassandra?

DESCRIBE TABLESOutput the names of all tables in the current keyspace, or in all keyspaces if there is no current keyspace. DESCRIBE TABLE [.] Output CQL commands that could be used to recreate the given table. In some cases, as above, there may be table metadata which is not representable and which will not be shown.


4 Answers

From cqlsh execute describe tables;

like image 126
Dan Borza Avatar answered Oct 26 '22 00:10

Dan Borza


There are system tables which can provide information about stored keyspaces, tables, columns.

Try run follows commands in cqlsh console:

  1. Get keyspaces info

    SELECT * FROM system.schema_keyspaces ;

  2. Get tables info

    SELECT columnfamily_name FROM system.schema_columnfamilies WHERE keyspace_name = 'keyspace name';

  3. Get table info

    SELECT column_name, type, validator FROM system.schema_columns WHERE keyspace_name = 'keyspace name' AND columnfamily_name = 'table name';

Since v 5.0.x Docs

  1. Get keyspaces info

    SELECT * FROM system_schema.keyspaces;

  2. Get tables info

    SELECT * FROM system_schema.tables WHERE keyspace_name = 'keyspace name';

  3. Get table info

    SELECT * FROM system_schema.columns WHERE keyspace_name = 'keyspace_name' AND table_name = 'table_name';

Since v 6.0 Docs

  1. Get keyspaces info

    SELECT * FROM system_schema.keyspaces

  2. Get tables info

    SELECT * FROM system_schema.tables WHERE keyspace_name = 'keyspace name';

  3. Get table info

    SELECT * FROM system_schema.columns WHERE keyspace_name = 'keyspace_name' AND table_name = 'table_name';

like image 37
Sabik Avatar answered Oct 25 '22 23:10

Sabik


desc keyspaces;  // list all databases/collections names
use anyKeyspace;  // select any database
desc tables;      // list all tables in collection/ database

like image 31
shubham kumar Avatar answered Oct 26 '22 00:10

shubham kumar


You can achieve what you want using datastax core driver and cluster metadata. Here is an example which will list all the tables in your keyspace and columns in each table:

 Cluster cluster= Cluster.builder().addContactPoint(clusterIp).build();
    Metadata metadata =cluster.getMetadata();

   Collection<TableMetadata> tablesMetadata= metadata.getKeyspace("mykeyspacename").getTables();
    for(TableMetadata tm:tablesMetadata){
        System.out.println("Table name:"+tm.getName());
        Collection<ColumnMetadata> columnsMetadata=tm.getColumns();            
        for(ColumnMetadata cm:columnsMetadata){
            String columnName=cm.getName();
            System.out.println("Column name:"+columnName);
        }
    }
like image 29
zoran jeremic Avatar answered Oct 26 '22 00:10

zoran jeremic