如何检查 Cassandra 表是否存在 [英] How to check if a Cassandra table exists

查看:34
本文介绍了如何检查 Cassandra 表是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单的方法可以使用 CQL(或 API,使用 com.datastax.driver)检查 Cassandra 中是否定义了表(列族)?

Is there an easy way to check if table (column family) is defined in Cassandra using CQL (or API perhaps, using com.datastax.driver)?

现在我倾向于执行 SELECT 1 FROM table 并检查异常,但也许有更好的方法?

Right now I am leaning towards executing SELECT 1 FROM table and checking for exception but maybe there is a better way?

推荐答案

从 1.1 开始,您应该能够查询 system 键空间、schema_columnfamilies 列族.如果您知道要检查哪个键空间,则此 CQL 应列出键空间中的所有列族:

As of 1.1 you should be able to query the system keyspace, schema_columnfamilies column family. If you know which keyspace you want to check, this CQL should list all column families in a keyspace:

SELECT columnfamily_name
FROM schema_columnfamilies WHERE keyspace_name='myKeyspaceName';

描述此功能的报告在这里:https://issues.apache.org/jira/browse/CASSANDRA-2477

The report describing this functionality is here: https://issues.apache.org/jira/browse/CASSANDRA-2477

虽然,他们确实注意到一些系统列名称在 1.1 和 1.2 之间发生了变化.因此,您可能需要稍微处理一下才能获得想要的结果.

Although, they do note that some of the system column names have changed between 1.1 and 1.2. So you might have to mess around with it a little to get your desired results.

编辑 20160523 - Cassandra 3.x 更新:

请注意,对于 Cassandra 3.0 及更高版本,您需要对上述查询进行一些调整:

Note that for Cassandra 3.0 and up, you'll need to make a few adjustments to the above query:

SELECT table_name 
FROM system_schema.tables WHERE keyspace_name='myKeyspaceName';

这篇关于如何检查 Cassandra 表是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆