scala 如何列出所有 cassandra 表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38696316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to list all cassandra tables
提问by Niko Gamulin
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.
cassandra 数据库中有很多表,其中包含标题为 user_id 的列。值 user_id 是指存储在表 users 中的用户。由于一些用户被删除,我想删除包含标题为 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?
有没有办法使用 CassandraSQLContext 或任何其他内置方法或自定义过程列出所有表,以避免显式定义表列表?
回答by Dan Borza
From cqlshexecute describe tables;
从cqlsh执行describe tables;
回答by Sabik
There are system tables which can provide information about stored keyspaces, tables, columns.
有系统表可以提供有关存储的键空间、表、列的信息。
Try run follows commands in cqlsh console:
尝试在 cqlsh 控制台中运行以下命令:
Get keyspaces info
SELECT * FROM system.schema_keyspaces ;Get tables info
SELECT columnfamily_name FROM system.schema_columnfamilies WHERE keyspace_name = 'keyspace name';Get table info
SELECT column_name, type, validator FROM system.schema_columns WHERE keyspace_name = 'keyspace name' AND columnfamily_name = 'table name';
获取键空间信息
SELECT * FROM system.schema_keyspaces ;获取表信息
SELECT columnfamily_name FROM system.schema_columnfamilies WHERE keyspace_name = 'keyspace name';获取表信息
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
从 v 5.0.x 文档开始
Get keyspaces info
SELECT * FROM system.schema_keyspaces;Get tables info
SELECT * FROM system_schema.tables WHERE keyspace_name = 'keyspace name';Get table info
SELECT * FROM system_schema.columns WHERE keyspace_name = 'keyspace_name' AND table_name = 'table_name';
获取键空间信息
SELECT * FROM system.schema_keyspaces;获取表信息
SELECT * FROM system_schema.tables WHERE keyspace_name = 'keyspace name';获取表信息
SELECT * FROM system_schema.columns WHERE keyspace_name = 'keyspace_name' AND table_name = 'table_name';
Since v 6.0 Docs
自 v 6.0 文档
Get keyspaces info
SELECT * FROM system_schema.keyspacesGet tables info
SELECT * FROM system_schema.tables WHERE keyspace_name = 'keyspace name';Get table info
SELECT * FROM system_schema.columns WHERE keyspace_name = 'keyspace_name' AND table_name = 'table_name';
获取键空间信息
SELECT * FROM system_schema.keyspaces获取表信息
SELECT * FROM system_schema.tables WHERE keyspace_name = 'keyspace name';获取表信息
SELECT * FROM system_schema.columns WHERE keyspace_name = 'keyspace_name' AND table_name = 'table_name';
回答by zoran jeremic
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:
您可以使用 datastax 核心驱动程序和集群元数据来实现您想要的。这是一个示例,它将列出您的键空间中的所有表和每个表中的列:
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);
}
}
回答by mipsbuster
For DSE. If later release check for system_schema keyspace. from
对于 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
if you see 'system_schema' then the metadata for tables is in this keyspace.
如果您看到“system_schema”,则表的元数据在此键空间中。
cqlsh>use system_schema;
cqlsh>select keyspace_name,table_name from tables where keyspace_name = 'system';
回答by Devy
To list all the tables (only the table names),
要列出所有表(仅表名),
DESC tables;
DESC tables;
To list all the tables and the schemas (CQL creation statements),
要列出所有表和模式(CQL 创建语句),
DESC {$KEYSPACE_NAME}whereas {$KEYSPACE_NAME}is the name of the keyspace.
DESC {$KEYSPACE_NAME}而{$KEYSPACE_NAME}是键空间的名称。
I am using Apache Cassandra 3.11.4 binary package downloaded from the official site:
我使用的是从官方网站下载的 Apache Cassandra 3.11.4 二进制包:
cql> show version;
[cqlsh 5.0.1 | Cassandra 3.11.4 | CQL spec 3.4.4 | Native protocol v4]
The Apache Cassandra's CQL reference is here: https://cassandra.apache.org/doc/cql3/CQL-3.0.html
Apache Cassandra 的 CQL 参考在这里:https: //cassandra.apache.org/doc/cql3/CQL-3.0.html
回答by Ruslan Gafiullin
try: cqlsh: describe keyspace <keyspace_name>;
尝试:cqlsh:描述键空间<keyspace_name>;

![Spark Scala:如何将 Dataframe[vector] 转换为 DataFrame[f1:Double, ..., fn: Double)]](/res/img/loading.gif)