如何使用Java中的cassandra-driver从UDT集合(例如:list)中读取UDT? [英] How to read the UDTs from collection(for ex:list) of UDTs with cassandra-driver in java?
问题描述
我有一个表 emp(id,name,list< frozen< address>>))
。这里的地址是Cassandra UDT,定义为 create TYPE地址(hno int,街道文本);
。我正在尝试使用以下代码在emp中读取给定id的所有地址,并且出现以下错误:
线程 main中的异常 com.datastax.driver.core.exceptions.CodecNotFoundException:找不到所需操作的编解码器:[frozen<'地址'><-> com.xyz.cassandra.address]
字符串query1 = select * from iotbilling.emp,其中id =?;
PreparedStatement prepareStatement2 = this.session.prepare(query1);
BoundStatement boundStatement2 = preparedStatement2.bind(4);
ResultSet rs2 = this.session.execute(boundStatement2);
行row2 = rs2.one();
List< address>地址= row2.getList(地址,address.class);
System.out.println(检索到的地址);
for(address adr:addresses)
System.out.println(adr.toString());
`
这里,如何捕获从cassandra返回的Java代码中的冻结地址列表?
您可以从行中读取值并读取元数据
UDTValue udtData = row.getUDTValue(address);
例如:
udtData.getString( name);
使用列表示例更新
对于列表,它可能看起来应该像这样:
List< UDTValue> udtDataList = row.getList( adresses,UDTValue.class)
然后您可以轻松地遍历列表并访问数据字段。
最佳
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?
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
这篇关于如何使用Java中的cassandra-driver从UDT集合(例如:list)中读取UDT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!