Cassandra Sample Trigger Code获取插入值
我需要您在触发器扩充方法中提取列名称和值的帮助。
表格定义:
create table dy_data ( id timeuuid, data_key text, time timestamp, data text,primary key((id,data_key),time) ) with clustering order by (time desc);
触发代码:
public class ArchiveTrigger implements ITrigger { public Collection augment(ByteBuffer key, ColumnFamily cf) { try { // Below loop only has 2 columns ( one is "data" and another one may be "time" but i am not sure, because i cannot get value. for (Column cell : cf) { //Got Exception if I try to get column name String name = ByteBufferUtil.string(cell.name()); //Got only "data" column value and empty value for another column may be "time". If I try ByteBufferUtil.toLong(cell.value()) it throws exception String value = ByteBufferUtil.string(cell.value()); log(" name = " + name); log(" value = " + value); } } catch (Exception e) { logger.warn("Exception ", e); } return null; } }
我尽力在谷歌搜索示例代码。 但失败了。 请帮我提供示例代码。 提前致谢。
要获得’id’和’data_key’,你必须拆分密钥(ByteBuffer密钥,第一个参数来扩充)。 ‘time’将在cell.name()的第一部分 – 你仍然需要拆分它。 ‘data’将是cell.value(),不需要进行任何拆分。
感谢iamaleksey为您提供帮助。 你的回复对我很有帮助。
以下是对触发器用户有用的代码段,
public Collection augment(ByteBuffer key, ColumnFamily cf) { try { ByteBuffer id_bb = CompositeType.extractComponent(key, 0); UUID id=TimeUUIDType.instance.compose(id_bb); ByteBuffer data_key_bb = CompositeType.extractComponent(key, 1); String data_key=UTF8Type.instance.compose(data_key_bb); Iterator col_itr=cf.iterator(); Column ts_col=(Column)col_itr.next(); ByteBuffer time_bb=CompositeType.extractComponent(ts_col.name(),0); long time=(TimestampType.instance.compose(time_bb)).getTime(); Column data_bb=(Column)col_itr.next(); String data=UTF8Type.instance.compose(data_bb.value()); log(" id --> "+id.toString()); log(" data_key-->"+data_key); log(" time == "+time); log(" data == "+data); } catch (Exception e) { logger.warn("Exception ", e); } return null; }
PS:因为我知道我的表格式,所以我硬编码了列比较器类型。 如果我们想编写通用触发器代码,我们可以使用cf.getComponentComparator()/ getKeyValidator()。
- 如何使用Spark DataFrame计算Cassandra表的汇总统计量?
- Cassandra NoHostAvailableException Java-CQLDriver
- Spark与Cassandra输入/输出
- cassandra的cqlsh控制台中的操作超时错误
- 如何将Java Long转换为Cassandra的byte ?
- 使用Datastax API(使用新的二进制协议)升级/读入/读取Cassandra数据库
- 压缩是否应该在gc_grace_seconds内保留墓碑?
- Cassandra如何选择节点发送请求?
- Cassandra使用TokenAwarePolicy和shuffleReplicas进行负载均衡