数据存储驱动程序映射ORDER BY ASC / DESC

时间:2015-07-09 09:56:05

标签: cassandra mapping cql datastax-java-driver

假设下一个Cassandra ColumnFamily:

CREATE TABLE myTable (
    A text,
    B text,
    time timestamp,
    PRIMARY KEY ((A, B), time)
) WITH CLUSTERING ORDER BY (time ASC)
    AND bloom_filter_fp_chance = 0.01
    AND caching = '{"keys":"ALL", "rows_per_partition":"NONE"}'
    AND comment = ''
    AND compaction = {'min_threshold': '4', 'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy', 'max_threshold': '32'}
    AND compression = {'sstable_compression': 'org.apache.cassandra.io.compress.LZ4Compressor'}
    AND dclocal_read_repair_chance = 0.1
    AND default_time_to_live = 0
    AND gc_grace_seconds = 864000
    AND max_index_interval = 2048
    AND memtable_flush_period_in_ms = 0
    AND min_index_interval = 128
    AND read_repair_chance = 0.0
    AND speculative_retry = '99.0PERCENTILE';

我需要执行下一个CQL 3查询:

SELECT * FROM myTable WHERE A='aaaaa' AND B='bbbbb' ORDER BY time DESC

(在CQL控制台中可以正常工作)使用Datastax Driver Mapping(cassandra-driver-mapping-2.1.6)。我的Accesor是下一个:

@Accessor
public interface myTableAccesor {  

    @Query("SELECT * FROM myTable WHERE A=:A AND B=:B ORDER BY time :order")
    Result<myTable> getAllByKey(@Param("A") String A, @Param("B") String B, @Param("order") String order);
}

但我得到了下一个例外:

java.util.concurrent.ExecutionException: com.datastax.driver.core.exceptions.SyntaxError: line 1:136 missing EOF at ':' (...=:B ORDER BY time [:]order)

执行时:

myTableAccesor accessor = manager.createAccessor(myTableAccesor.class);

然而,LIMIT案例:

@Query("SELECT * FROM myTable WHERE A=:A AND B=:B LIMIT :max")
        Result<myTable> getAllByKey(@Param("A") String A, @Param("B") String B, @Param("max") String max);

正常运作。

我认为问题与字符串类型有关(也许是某些'字符?)但我不确定。有什么方法可以解决这个问题吗?

提前谢谢。

1 个答案:

答案 0 :(得分:2)

不幸的是,在准备语句(这是访问者使用的)中的ORDER BY方向上绑定是无效的CQL,就像使用LIMIT一样。作为一种解决方法,您可以创建两个单独的访问器:

@Query("SELECT * FROM myTable WHERE A=:A AND B=:B ORDER BY time DESC")
Result<myTable> getAllByKeyDesc(@Param("A") String A, @Param("B") String B);

@Query("SELECT * FROM myTable WHERE A=:A AND B=:B ORDER BY time ASC")
Result<myTable> getAllByKeyAsc(@Param("A") String A, @Param("B") String B);