我在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
第一条记录更新成功,但它正在存储{ {1}}非更新字段的值也是,但是当我更新第二次插入记录时,它只更新必须更新的字段而不影响未更新的字段。
这是我的插入和更新代码
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
作为{ {1}}。插入另一个segment
后再次运行null
查询,这次更新row
和update
而不会影响其他字段,即status
,type
和duration
答案 0 :(得分:2)
session.execute( update.setForceNoValues(false) ).wasApplied()
会有所帮助