How to read the UDTs from collection(for ex:list) of UDTs with cassandra-driver in java?

萝らか妹 提交于 2019-12-12 21:05:37

问题


I have table emp(id,name,list<frozen<address>>). Here address is cassandra UDT defined as create TYPE address (hno int,street text);. I am trying to read all address's for a given id in emp using below code and I get the following error:

Exception in thread "main" com.datastax.driver.core.exceptions.CodecNotFoundException: Codec not found for requested operation: [frozen<'address'> <-> com.xyz.cassandra.address]

String query1="select * from iotbilling.emp where id=?";
PreparedStatement preparedStatement2=this.session.prepare(query1);
BoundStatement boundStatement2=preparedStatement2.bind(4);
ResultSet rs2=this.session.execute(boundStatement2);
Row row2=rs2.one();
List<address> addresses=row2.getList("adresses",address.class);
System.out.println("Addresses retrieved");
for(address adr:addresses)
    System.out.println(adr.toString());

`

Here, how to capture the list of frozen address in java code that is returned from cassandra?


回答1:


You can read the value from row and read the metadata row by row:

UDTValue udtData = row.getUDTValue(address);

For example:

udtData.getString("name");

Update with list example

For a list it should probably look like:

List<UDTValue> udtDataList = row.getList("adresses", UDTValue.class)

And then you can easily iterate through the list and access the fields of your data.

Best




回答2:


You might want to use datastax Mapper for this purpose. You have to declare your Java table and type Table using Datastax annotations like this:

    @Table(keyspace = "corporate", name = "emp")
     public class Employee{

          @PartitionKey(0)
          @Column(name = "id")
           private Integer id;

          @Column(name = "name")
           private String name;

          @Frozen
          @Column(name = "address_list")
          private List<Address> address;

          ...(Getter and Setters)
     }

And your UDT annotated as below:

     @UDT(keyspace = "corporate", name="address")
     public class Address{

          @Field(name="hno")
          private Integer hno;

          @Field(name="street")
          private String street;

          ...(Getters and Setters)
     }

You might want to add the following dependency to your pom.xml file beforehand if you haven't

    <dependency>
        <groupId>com.datastax.cassandra</groupId>
        <artifactId>cassandra-driver-core</artifactId>
        <version>3.3.0</version>
    </dependency>
    <dependency>
        <groupId>com.datastax.cassandra</groupId>
        <artifactId>cassandra-driver-mapping</artifactId>
        <version>3.3.0</version>
    </dependency>

After these steps you need to use Mapper object to get your results:

    MappingManager manager = new MappingManager(this.session);
    Mapper<Employee> mapper = manager.mapper(Employee.class);

And once you get resultSet use mapper to get:

    Result<Employee> list = mapper.map(rs2);
    Employee emp = list.one();

Now you can access emp as java object and use emp.getAddress to access your list.



来源:https://stackoverflow.com/questions/41589519/how-to-read-the-udts-from-collectionfor-exlist-of-udts-with-cassandra-driver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!