我正在评估 Spark-cassandra-connector,并且正在努力尝试对分区键进行范围查询。
根据连接器的文档,似乎可以使用相等或 IN 运算符对分区键进行服务器端过滤,但不幸的是,我的分区键是时间戳,因此我无法使用它。
因此,我尝试使用 Spark SQL 进行以下查询(“时间戳”是分区键):
select * from datastore.data where timestamp >= '2013-01-01T00:00:00.000Z' and timestamp < '2013-12-31T00:00:00.000Z'
尽管作业生成了 200 个任务,但查询未返回任何数据。
此外,我可以确保自从在 cqlsh 上运行查询(使用“ token ”函数进行适当的转换)后确实会返回数据。
我正在使用独立模式的 Spark 1.1.0。 Cassandra 是 2.1.2,连接器版本是“b1.1”分支。 Cassandra 驱动程序是 DataStax 的“主”分支。 Cassandra集群覆盖在spark集群上,有3台服务器,复制因子为1。
有人有任何线索吗?
更新:当尝试基于分区键进行服务器端过滤(使用 CassandraRDD.where 方法)时,出现以下异常:
Exception in thread "main" java.lang.UnsupportedOperationException: Range predicates on partition key columns (here: timestamp) are not supported in where. Use filter instead.
但不幸的是我不知道“过滤器”是什么......
最佳答案
我认为 CassandraRDD 错误表明您尝试执行的查询在 Cassandra 中是不允许的,您必须加载 CassandraRDD 中的所有表,然后对此 CassandraRDD 进行 Spark 过滤操作。
所以你的代码(在scala中)应该是这样的:
val cassRDD= sc.cassandraTable("keyspace name", "table name").filter(row=> row.getDate("timestamp")>=DateFormat('2013-01-01T00:00:00.000Z')&&row.getDate("timestamp") < DateFormat('2013-12-31T00:00:00.000Z'))
如果您有兴趣进行此类查询,您可能需要看看其他 Cassandra 连接器,例如 Stratio 开发的连接器
关于cassandra - Spark Cassandra 连接器 - 分区键的范围查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27028926/