如何获取cassandra中关键空间的所有表的列表

Sam*_*oud 4 c# database cassandra cqlsh datastax

我是cassandra的新手,我目前在虚拟应用程序中使用CassandraCSharpDriver.我想获取用户在给定密钥空间中描述的所有表的列表.

        Cluster cluster = Cluster.Builder().AddContactPoints("IPAddress").Build();
        Session session = cluster.Connect("MyKeySpace");
Run Code Online (Sandbox Code Playgroud)

在这段代码中,我想获得MyKeySpace的所有表的列表

ash*_*hic 5

你可以运行:

select * from system.schema_columnfamilies where keyspace_name='MyKeySpace';
Run Code Online (Sandbox Code Playgroud)

它针对"系统"键空间运行.


Aar*_*ron 5

我将介绍如何使用DataStax Cassandra C#驱动程序准备表列表:

连接到您的群集:

Cluster cluster = Cluster.Builder().AddContactPoints("IPAddress").Build();
Session session = cluster.Connect();
Run Code Online (Sandbox Code Playgroud)

我将创建一个List<String>并使用准备好的语句(因为这只是一个好主意)来绑定键空间的名称.我的CQL语句只是选择columnfamily_name,这应该是你需要的.

List<String> tableList = new List<String>();

String strCQL = "SELECT columnfamily_name "
    + "FROM system.schema_columnfamilies WHERE keyspace_name=? ";
PreparedStatement pStatement = _session.Prepare(strCQL);
BoundStatement boundStatement = new BoundStatement(pStatement);
boundStatement.Bind("MyKeySpace");
Run Code Online (Sandbox Code Playgroud)

现在我将执行语句,遍历结果,并将每个表名添加到上面创建的List中.

RowSet results = session.Execute(boundStatement);

foreach (Row result in results.GetRows())
{
    tableName = result.GetValue<String>("columnfamily_name");
    tableList.Add(tableName);
}
Run Code Online (Sandbox Code Playgroud)

现在您应该有一个可以添加到UI的表列表.