gpt4 book ai didi

java - 用于获取插入值的 Cassandra 示例触发代码

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:27:29 25 4
gpt4 key购买 nike

我需要你的帮助来提取触发器扩充方法中的列名称和值。

表定义:

           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<RowMutation> 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;
}
}

我尽力在谷歌搜索示例代码。但是失败了。请帮我提供示例代码。提前致谢。

最佳答案

感谢 iamaleksey 的帮助。你的回复对我帮助很大。

下面是对触发器用户有用的代码片段,

public Collection<RowMutation> 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()。

关于java - 用于获取插入值的 Cassandra 示例触发代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20241570/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com