What, where and how should I register something as a codec for a custom type

determined the type

@UDT(name = "bucket", keyspace = "us") public class BucketType { @Field(name = "event_time") private Date eventTime; @Field(name = "product_id") private String productId; //setters and getters } 

table structure

 CREATE TABLE keyspace.shops ( user_id stext, buckets list<frozen <bucket>> PRIMARY KEY (user_id)); 

Dependencies

  compile group: 'com.datastax.cassandra', name: 'cassandra-driver-mapping', version: '3.5.0' compile group: 'com.datastax.cassandra', name: 'cassandra-driver-core', version: '3.5.0' 

I make a request to cassandra

 Session session = cluster.connect(); ResultSet resultSet = session.execute("select * from keyspace.shops;"); resultSet.forEach(row -> { List<BucketType> list1 = row.getList(1, BucketType.class); } 

Tried different methods get, getValue, and indices different. It gives an error.

  com.datastax.driver.core.exceptions.CodecNotFoundException: Codec not found for requested operation: [ups.cart_add <-> com.test.entity.type.BucketType] at com.datastax.driver.core.CodecRegistry.notFound(CodecRegistry.java:741) ~[cassandra-driver-core-3.5.0.jar:na] at com.datastax.driver.core.CodecRegistry.createCodec(CodecRegistry.java:588) ~[cassandra-driver-core-3.5.0.jar:na] at com.datastax.driver.core.CodecRegistry.findCodec(CodecRegistry.java:558) ~[cassandra-driver-core-3.5.0.jar:na] 
  • try row.getList("buckets", BucketType.class) - Senior Pomidor
  • I tried the same mistake - J Mas
  • and if you try row.getList("buckets", TypeToken.of(BucketType.class)) - Senior Pomidor
  • Exactly the same mistake - J Mas
  • maybe you are not connected to that keyspace? try specifying cluster.connect("ups") . - Senior Pomidor

1 answer 1

When using declarations, it is better to use the mapper API , rather than standard functions ...

But you can work with the result obtained through them, only you need to add a mapping for the shop table itself (let it be a class Shop , and a wrapper:

 Mapper<Shop> mapper = manager.mapper(Shop.class); ResultSet resultSet = session.execute("select * from keyspace.shops;"); Result<Shop> shops = mapper.map(resultSet); ... 

Relevant documentation ...

Or you can make access through Accessors (I do not know how to translate) ...