Datastax 驱动程序映射 ORDER BY ASC/DESC
Datastax Driver Mapping ORDER BY ASC/DESC
假设下一个 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 驱动程序映射 (cassandra-driver-mapping-2.1.6)。我的辅助器是下一个:
@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);
工作正常。
我认为问题与 String 类型有关(也许是某些 ' 字符?)但我不确定。有什么办法可以解决吗?
提前致谢。
不幸的是,像使用 LIMIT 一样,在准备好的语句(这是访问器使用的)中绑定到 ORDER BY 方向是无效的 CQL。作为解决方法,您可以创建两个单独的访问器:
@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);
假设下一个 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 驱动程序映射 (cassandra-driver-mapping-2.1.6)。我的辅助器是下一个:
@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);
工作正常。
我认为问题与 String 类型有关(也许是某些 ' 字符?)但我不确定。有什么办法可以解决吗?
提前致谢。
不幸的是,像使用 LIMIT 一样,在准备好的语句(这是访问器使用的)中绑定到 ORDER BY 方向是无效的 CQL。作为解决方法,您可以创建两个单独的访问器:
@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);