简体   繁体   中英

Caused by: com.datastax.driver.core.exceptions.InvalidQueryException: Expected 8 or 0 byte long for date (25)

I developing application using Cassandra and SpringBoot. I have written Cassandra query in Java;

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_updateddate is timestamp data type column in Cassandra. userName and last_updateddate columns are composite key in database and using latest version of Cassandra.

while executing getting the following error.

Caused by: com.datastax.driver.core.exceptions.InvalidQueryException: Expected 8 or 0 byte long for date (25)

but

Issue got resolved after below change.

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());

you have to check data type of Cassandra table and use corresponding datatype of java and use it in java code when declaring variable to pass value of column .

Example : In Cassandra LocalDate then define LocalDate of cassandra.core package in java instead Date or LocalDate of util package.

check this type mapping between Cassandra vs Java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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