简体   繁体   English

使用IN子句过滤Spark Cassandra连接器

[英]Spark Cassandra connector filtering with IN clause

I am facing some issues with spark cassandra connector filtering for java. 我正面临着针对java的spark cassandra连接器过滤的一些问题。 Cassandra allows the filtering by last column of the partition key with IN clause. Cassandra允许使用IN子句过滤分区键的最后一列。 eg 例如

create table cf_text
(a varchar,b varchar,c varchar, primary key((a,b),c))

Query : select * from cf_text where a ='asdf' and b in ('af','sd');

sc.cassandraTable("test", "cf_text").where("a = ?", "af").toArray.foreach(println)

How count I specify the IN clause which is used in the CQL query in spark? 我如何指定在spark中的CQL查询中使用的IN子句? How range queries can be specified as well? 如何指定范围查询?

Just wondering, but does your Spark code above work? 只是想知道,但你的Spark代码上面有效吗? I thought that Spark won't allow a WHERE on partition keys ( a and b in your case), since it uses them under the hood (see last answer to this question): Spark Datastax Java API Select statements 我认为Spark不允许分区键上的WHERE (在你的情况下为ab ),因为它在引擎盖下使用它们(参见本问题的最后一个答案): Spark Datastax Java API Select语句

In any case, with the Cassandra Spark connector, you are allowed to stack your WHERE clauses, and an IN can be specified with a List<String> . 在任何情况下,使用Cassandra Spark连接器,您都可以堆叠WHERE子句,并且可以使用List<String>指定IN

List<String> valuesList = new ArrayList<String>();
valuesList.Add("value2");
valuesList.Add("value3");

sc.cassandraTable("test", "cf")
    .where("column1 = ?", "value1")
    .where("column2 IN ?", valuesList)
    .keyBy(new Function<MyCFClass, String>() {
                public String call(MyCFClass _myCF) throws Exception {
                    return _myCF.getId();
                }
            });

Note that the normal rules of using IN with Cassandra/CQL still apply here. 请注意, 使用IN和Cassandra / CQL常规规则仍适用于此处。

Range queries function in a similar manner: 范围查询以类似的方式运行:

sc.cassandraTable("test", "person")
    .where("age > ?", "15")
    .where("age < ?", "20")
    .keyBy(new Function<Person, String>() {
                public String call(Person _person) throws Exception {
                    return _person.getPersonid();
                }
            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM