首页 > 解决方案 > 有没有办法使用 spark-cassandra 连接器访问 Cassandra 模式信息?

问题描述

较新的 spark-cassandra 连接器已弃用/删除了允许执行 CQL 的 CassandraSQLContext。而且,现在,我找不到一种方法来查找目录信息,例如:键空间列表、键空间内的表或列元数据。

具体来说,我希望能够运行类似select keyspace_name, table_name, column_name, type from system_schema.columns where keyspace_name = 'test' 可能我错过了运行 CQL 的 API?(我使用的是 2.0 连接器)

标签: apache-sparkcassandraspark-cassandra-connector

解决方案


Spark Cassandra 连接器具有withSessionDo您可以使用的方法,与在 Java 驱动程序中使用的方法相同,如下所示(从文档中采用):

import com.datastax.spark.connector.cql.CassandraConnector

CassandraConnector(conf).withSessionDo { session =>
  session.execute("select keyspace_name, table_name, column_name, 
      type from system_schema.columns where keyspace_name = 'test';")
}

但是您可以使用更简单的 RDD 操作,如下所示:

sc.cassandraTable("system_schema", "columns").select("keyspace_name","table_name", 
     ...other columns...)

PS 另外,请注意,Metadata通过 Session->Cluster 可以获得的类访问是更便携的方式。


推荐阅读