scalaapache-sparkcassandraspark-cassandra-connector

How to list all Cassandra tables


There are many tables in a Cassandra database, which contain a column titled user_id. The values in user_id refer to users stored in the table users. As some users are deleted, I would like to delete orphan records in all tables that contain column a 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?


Solution

  • 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';