如何列出所有卡桑德拉表

How to list all cassandra tables

cassandra 数据库中有很多 table,其中包含标题为 user_id 的列。值 user_id 指的是存储在 table 用户中的用户。由于删除了一些用户,我想删除所有 table 中包含标题为 user_id 的列的孤立记录。

有没有办法使用 CassandraSQLContext 或任何其他 built-in 方法或自定义过程列出所有 table,以避免显式定义 table 列表?

有系统 tables 可以提供有关存储的键空间、tables、列的信息。

尝试 运行 按照 cqlsh 控制台中的命令进行操作:

  1. 获取键空间信息

    SELECT * FROM system.schema_keyspaces ;

  2. 获取 tables 信息

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

  3. 获取table信息

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

从第 5 版开始0.x Docs

  1. 获取键空间信息

    SELECT * FROM system_schema.keyspaces;

  2. 获取 tables 信息

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

  3. 获取table信息

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

从 6.0 版开始 Docs

  1. 获取键空间信息

    SELECT * FROM system_schema.keyspaces

  2. 获取 tables 信息

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

  3. 获取table信息

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

您可以使用 Datastax 核心驱动程序和集群元数据实现您想要的。这是一个示例,它将列出您的键空间中的所有 table 和每个 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);
        }
    }

cqlsh执行describe tables;

对于 DSE。如果稍后发布,请检查 system_schema 键空间。 来自

cqlsh > desc keyspaces;

spark_system  system_schema  "OpsCenter"  cfs_archive         "HiveMetaStore"
system_auth    cfs          demobeta            dsefs          
dse_security  hypermedia     dse_leases   system_traces       dse_perf       
solr_admin    system         system_distributed  dse_system

如果您看到 'system_schema',那么表的元数据就在这个键空间中。

cqlsh>use system_schema;

cqlsh>select keyspace_name,table_name from tables where keyspace_name = 'system';

要列出所有 table(仅 table 姓名),

DESC tables;

列出所有 table 和模式(CQL 创建语句),

DESC {$KEYSPACE_NAME}{$KEYSPACE_NAME} 是键空间的名称。

我使用的是官网下载的Apache Cassandra 3.11.4二进制包:

cql> show version;
[cqlsh 5.0.1 | Cassandra 3.11.4 | CQL spec 3.4.4 | Native protocol v4]

Apache Cassandra 的 CQL 参考在这里:https://cassandra.apache.org/doc/cql3/CQL-3.0.html

尝试运行以下查询:

cqlsh> describe keyspace <keyspace_name>;

只需连接 CQLSH 并尝试以下命令

描述表格;或者

描述架构;

您还可以在 'desc keyspaces;' 和 'use keyspace;' 中查看带有 'describe tables;'

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