I使用Cassandra和SpringBoot开发应用程序。我用Java编写了Cassandra查询;
String userName="testUser";
String lastUpdatedDate="2018-11-29 13:00:43.400";
String tenantName="demo";
Select select = QueryBuilder.select().all()
.from(tenantName,getGenericClass().getSimpleName())
.where(QueryBuilder.eq("user_Name", userName))
.and(QueryBuilder.gt("last_updateddate", lastUpdatedDate))
.allowFiltering()
.limit(100);
return (List<T>) cassandraOperations.select(select, getGenericClass());
last_updatedate是Cassandra中的时间戳数据类型列。userName和last_updatedate列是数据库中的组合键,使用最新版本的Cassandra。
执行时得到以下错误。
引起原因:com.datatax.river.core.exceptions.InvalidQueryException:日期(25(应为8或0字节长
但是
问题在以下更改后得到解决。
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = sdf.parse(lastUpdatedDate);
long timeInSec = date.getTime();
Timestamp ts=new Timestamp(timeInSec);
Date date1=ts;
Select select = QueryBuilder.select().all()
.from(tenantName,getGenericClass().getSimpleName())
.where(QueryBuilder.eq("user_Name", userName))
.and(QueryBuilder.gt("last_updateddate", date1))
.allowFiltering()
.limit(100);
return (List<T>) cassandraOperations.select(select, getGenericClass());
您必须检查Cassandra表的数据类型,并使用相应的java数据类型,在声明变量以传递列值时在java代码中使用它。
示例:在Cassandra LocalDate中,用java定义Cassandra.core包的LocalDate,而不是util包的Date或LocalDate。
检查Cassandra与Java 之间的这种类型映射