如何使用Java在Cassandra中检索表名? [英] How do I retrieve table names in Cassandra using Java?

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

问题描述

如果在CQL shell中使用此代码,则将获得该键空间中所有表的名称。

If is use this code in a CQL shell , I get all the names of table(s) in that keyspace.

DESCRIBE TABLES;

我想使用 ResulSet 检索相同的数据。以下是我的Java代码。

I want to retrieve the same data using ResulSet . Below is my code in Java.

String query = "DESCRIBE TABLES;";

ResultSet rs = session.execute(query);
for(Row row  : rs) {
    System.out.println(row);
}   

会话和群集早先已初始化为:

While session and cluster has been initialized earlier as:

Cluster cluster = Cluster.builder().addContactPoint("127.0.0.1").build();
Session session = cluster.connect("keyspace_name");

或者我想知道Java代码来检索键空间中的表名。

推荐答案

系统表的架构在不同版本之间会有很大变化。最好依赖于将内置特定于版本的解析的驱动程序元数据。从Java驱动程序使用

The schema for the system tables change between versions quite a bit. It is best to rely on drivers Metadata that will have version specific parsing built in. From the Java Driver use

Cluster cluster = Cluster.builder().addContactPoint("127.0.0.1").build();
Collection<TableMetadata> tables = cluster.getMetadata()
    .getKeyspace("keyspace_name")
    .getTables(); // TableMetadata has name in getName(), along with lots of other info

// to convert to list of the names
List<String> tableNames = tables.stream()
        .map(tm -> tm.getName())
        .collect(Collectors.toList());

这篇关于如何使用Java在Cassandra中检索表名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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