【发布时间】:2018-08-03 05:21:27
【问题描述】:
我在 Cassandra 中的表结构:
CREATE TABLE test(
id text,
location text,
status text,
type text,
duration double,
threshold double,
timestamp timestamp,
segment text,
PRIMARY KEY (id, location, timestamp)
) WITH CLUSTERING ORDER BY (location ASC, timestamp ASC)
AND bloom_filter_fp_chance = 0.01
AND caching = {'keys': 'ALL', 'rows_per_partition': 'NONE'}
AND comment = ''
AND compaction = {'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy', 'max_threshold': '32', 'min_threshold': '4'}
AND compression = {'chunk_length_in_kb': '64', 'class': 'org.apache.cassandra.io.compress.LZ4Compressor'}
AND crc_check_chance = 1.0
AND dclocal_read_repair_chance = 0.1
AND default_time_to_live = 0
AND gc_grace_seconds = 864000
AND max_index_interval = 2048
AND memtable_flush_period_in_ms = 0
AND min_index_interval = 128
AND read_repair_chance = 0.0
AND speculative_retry = '99PERCENTILE';
我正在使用Java 将数据插入此表,然后经过一些计算尝试更新同一行。该过程是插入更新然后插入更新.........这里的问题是当我尝试使用java更新时,即com.datastax.driver.core.querybuilder.Update第一条记录更新成功但它正在存储null的值也未更新的字段,但是当我更新第二次插入的记录时,它仅更新必须更新的字段而不影响未更新的字段。
这是我的插入和更新代码
String names[] = {
"id ",
"location",
"status",
"type",
"duration",
"threshold",
"timestamp",
"segment"
};
Object values[] = {
obj.getId(),
obj.getLocation(),
obj.getStatus(),
obj.gettype(),
obj.getDuration(),
obj.getThreshold(),
obj.getTimestamp(),
obj.getSegment(),
};
try{
Insert insert = QueryBuilder.insertInto("test");
insert.values( names, values );
return session.execute( insert ).wasApplied();
} catch( Exception ex ) {
logger.error( "Exception while inserting ", ex );
return false;
}
更新方法如下
try {
Update update = QueryBuilder.update("test");
update.with( QueryBuilder.set("status", obj.getStatus() ) );
update.with( QueryBuilder.set("type", obj.getType() ) );
update.where( QueryBuilder.eq("id", obj.getId()) )
.and( QueryBuilder.eq("location", obj.getLocation() ) )
.and( QueryBuilder.eq("timestamp", obj.getTimestamp() ) );
return session.execute( update ).wasApplied();
} catch( Exception ex ) {
logger.error( "Exception while updating", ex );
return false;
}
当我第一次运行更新方法时,它按预期存储status 和type 值,但将其他duration、threshold 和segment 设置为null。在插入另一个row 之后再次运行update 查询,这次更新status 和type 而不影响其他字段,即duration、threshold 和segment
【问题讨论】: