将空值插入cassandra [英] Inserting null values into cassandra
问题描述
我有一些字段要存储到Cassandra中,但是其中某些字段在任何给定点都可以为null.因为它们很多,所以如果我在将每个代码添加到INSERT之前不检查每个代码是否为null的话,它将使代码更具可读性.
I have some fields that I am storing into Cassandra, but some of them could be null at any given point. As there are quite a lot of them, it makes the code much more readable if I don’t check each one for null before adding it to the INSERT.
这样做有什么害处吗?
编辑!
我找到了一张吉拉车票.但是我不知道最终从票证中实现了什么解决方案. https://issues.apache.org/jira/browse/CASSANDRA-7304
There is a jira ticket that I found. But I am unable to understand what solution was finally implemented from the ticket. https://issues.apache.org/jira/browse/CASSANDRA-7304
推荐答案
Cassandra的新存储引擎的美丽之处在于能够 NOT 存储值.它的意思是它的本意是: null 值只是一个不应该存在的值.
The beautiful thing about Cassandra's new storage engine is the ability to NOT store values. What it means is what it was meant to be: a null value is simply a value that should not be there.
这提供了很大的灵活性,因为未显式(或隐式地看到,请稍后参阅)插入的空值不会占用存储空间,也不会使用处理能力和IO带宽.
This gives great flexibility, because a null value not explicitly (or implicitly, see later) inserted won't take storage space, nor use processing power and IO bandwidth.
实际上,用空值填充行非常容易:
Indeed, it is pretty easy to populate a row with null values:
INSERT INTO mytable (pk, c2, c3) VALUES (0x1234, null, null);
这样,您可以明确地告诉C *在c2和c3中都存储一个空值.但是,您可以获得与宏观相同的效果:
This way you are explicitly telling C* to store a null value in both c2 and c3. However, you could get the same macroscopic effect with:
INSERT INTO mytable (pk) VALUES (0x1234);
请注意,我说的是宏观效果,因为当您显式插入空值时,C * 将插入引擎盖下的墓碑.从长远来看,由于C *如何执行搜索,压缩等操作,这会咬住您.因此,您应尽可能避免使用第二个版本,效果会更好.
Notice that I say macroscopic effect, because when you explicitly insert a null value, C* will insert a tombstone under the hood. In the long run this will bite you, due to how C* perform searches, compactions, etc... so you should avoid whenever possible, the second version will perform much better.
现在,还有一个陷阱:您还可以隐式创建墓碑.当您使用Cassandra内置的 TTL 功能时,会发生这种 情况.
Now, there is also a trap: you can also create tombstones implicitly. This will happen when you use the TTL features builtin in Cassandra.
最后,如果您关心自己,我建议不要执行任何空值插入.在应用程序级别进行检查,这样您以后可以节省时间(和金钱),例如在读取过程中.
In conclusion, if you care about yourself I'd suggest to NOT performing any null value inserts. Do a check at application level, you'll save time (and money) later, eg during reads.
这篇关于将空值插入cassandra的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!