Cassandra(版本 2)支持哪些类型的墓碑?根据 this 文章它支持(用 CQL 术语):
我是否错过了任何其他类型的墓碑?删除特定 (CQL) 行?是否有任何特殊的墓碑来支持删除集群键或类似的范围?在规划模式以避免太多墓碑时,此信息很有用。
最佳答案
墓碑是放置在指示删除的行中的标记。它们可以存在于不同的位置、一列或一系列列中,或者存在于整行中。下面的例子展示了普通类型的墓碑(这里不涉及范围类型)。
在规划模式时,您可以根据正在执行的查询类型对表进行建模,而不是只有一张表,您可能会发现数据在多个表中重复。这些表经过优化以服务于传入的读取和写入。下面的链接应该会给你一些关于使用 Cassandra 进行数据建模的良好背景:
http://www.datastax.com/resources/data-modeling
我的例子:我创建了一个表并插入了一些数据,然后使用 nodetool flush
生成了一些 sstables。使用 sstable2json
工具你可以看到被删除的行,如果它是一整行它看起来与单列略有不同,但本质上它仍然只是一个标记:
这是包含所有数据的表格:
$ ~/dse-4.5.1/resources/cassandra/bin/sstable2json ./dse-data/results/ts1/results-ts1-jb-1-Data.db
[
{"key": "3136","columns": [["","",1417814256390000], ["col2","26",1417814256390000], ["col3","36",1417814256390000], ["id","id16",1417814256390000]]},
{"key": "3133","columns": [["","",1417814218246000], ["col2","23",1417814218246000], ["col3","33",1417814218246000], ["id","id13",1417814218246000]]},
{"key": "3135","columns": [["","",1417814244766000], ["col2","25",1417814244766000], ["col3","35",1417814244766000], ["id","id15",1417814244766000]]},
{"key": "3134","columns": [["","",1417814230711000], ["col2","24",1417814230711000], ["col3","34",1417814230711000], ["id","id14",1417814230711000]]},
{"key": "3132","columns": [["","",1417814207910000], ["col2","22",1417814207910000], ["col3","32",1417814207910000], ["id","id12",1417814207910000]]},
{"key": "3131","columns": [["","",1417814197094000], ["col2","21",1417814197094000], ["col3","31",1417814197094000], ["id","id11",1417814197094000]]},
{"key": "31","columns": [["","",1417814185270000], ["col2","2",1417814185270000], ["col3","3",1417814185270000], ["id","id1",1417814185270000]]}
]
下面是 cqlsh 中的第一次删除:
cqlsh:results> delete from ts1 WHERE col1 = '1';
cqlsh:results> delete id from ts1 WHERE col1 = '11';
刷新后产生的 sstable 如下:
[datastax@DSE3 ~]$ ~/dse-4.5.1/resources/cassandra/bin/sstable2json ./dse-data/results/ts1/results-ts1-jb-2-Data.db
[
{"key": "3131","columns": [["id","54822130",1417814320400000,"d"]]},
{"key": "31","metadata": {"deletionInfo": {"markedForDeleteAt":1417814302304000,"localDeletionTime":1417814302}},"columns": []}
]
下面是 cqlsh 中的下一个删除:
cqlsh:results> delete col2 from ts1 WHERE col1 = '12';
刷新后产生的 sstable 如下:
[datastax@DSE3 ~]$ ~/dse-4.5.1/resources/cassandra/bin/sstable2json ./dse-data/results/ts1/results-ts1-jb-3-Data.db
[
{"key": "3132","columns": [["col2","5482220b",1417814539434000,"d"]]}
]
当压缩发生时,所有这些 sstable 被组合成一个单一的 sstable,然后删除的行仍然存在,但标记为删除,我们可以在运行压缩后再次看到这一点(查找带有时间戳的
d
标志):[datastax@DSE3 ~]$ ./dse-4.5.1/bin/nodetool compact
[datastax@DSE3 ~]$ ~/dse-4.5.1/resources/cassandra/bin/sstable2json ./dse-data/results/ts1/results-ts1-jb-4-Data.db
[
{"key": "3136","columns": [["","",1417814256390000], ["col2","26",1417814256390000], ["col3","36",1417814256390000], ["id","id16",1417814256390000]]},
{"key": "3133","columns": [["","",1417814218246000], ["col2","23",1417814218246000], ["col3","33",1417814218246000], ["id","id13",1417814218246000]]},
{"key": "3135","columns": [["","",1417814244766000], ["col2","25",1417814244766000], ["col3","35",1417814244766000], ["id","id15",1417814244766000]]},
{"key": "3134","columns": [["","",1417814230711000], ["col2","24",1417814230711000], ["col3","34",1417814230711000], ["id","id14",1417814230711000]]},
{"key": "3132","columns": [["","",1417814207910000], ["col2","5482220b",1417814539434000,"d"], ["col3","32",1417814207910000], ["id","id12",1417814207910000]]},
{"key": "3131","columns": [["","",1417814197094000], ["col2","21",1417814197094000], ["col3","31",1417814197094000], ["id","54822130",1417814320400000,"d"]]},
{"key": "31","metadata": {"deletionInfo": {"markedForDeleteAt":1417814302304000,"localDeletionTime":1417814302}},"columns": []}
]
现在这个表将保持这样,直到我们到达我们的
gc_grace_seconds
,然后在下一次压缩时,行实际上会消失,看着我们删除 gc_grace_seconds
然后运行压缩:cqlsh> ALTER TABLE results.ts1 WITH gc_grace_seconds=500;
cqlsh> exit
[datastax@DSE3 ~]$ ./dse-4.5.1/bin/nodetool compact results;
[datastax@DSE3 ~]$ ./dse-4.5.1/resources/cassandra/bin/sstable2json ./dse-data/results/ts1/results-ts1-jb-5-Data.db
[
{"key": "3136","columns": [["","",1417814256390000], ["col2","26",1417814256390000], ["col3","36",1417814256390000], ["id","id16",1417814256390000]]},
{"key": "3133","columns": [["","",1417814218246000], ["col2","23",1417814218246000], ["col3","33",1417814218246000], ["id","id13",1417814218246000]]},
{"key": "3135","columns": [["","",1417814244766000], ["col2","25",1417814244766000], ["col3","35",1417814244766000], ["id","id15",1417814244766000]]},
{"key": "3134","columns": [["","",1417814230711000], ["col2","24",1417814230711000], ["col3","34",1417814230711000], ["id","id14",1417814230711000]]},
{"key": "3132","columns": [["","",1417814207910000], ["col3","32",1417814207910000], ["id","id12",1417814207910000]]},
{"key": "3131","columns": [["","",1417814197094000], ["col2","21",1417814197094000], ["col3","31",1417814197094000]]}
]
注意键
31
的行是如何消失的,还有键 col1
的行中的 3132
和键 id
的行中的 3131
为清楚起见,我的表架构:
cqlsh:results> DESCRIBE TABLE ts1 ;
CREATE TABLE ts1 (
col1 text,
col2 text,
col3 text,
id text,
PRIMARY KEY ((col1))
) WITH
bloom_filter_fp_chance=0.010000 AND
caching='KEYS_ONLY' AND
comment='' AND
dclocal_read_repair_chance=0.100000 AND
gc_grace_seconds=864000 AND
index_interval=128 AND
read_repair_chance=0.000000 AND
replicate_on_write='true' AND
populate_io_cache_on_flush='false' AND
default_time_to_live=0 AND
speculative_retry='99.0PERCENTILE' AND
memtable_flush_period_in_ms=0 AND
compaction={'class': 'SizeTieredCompactionStrategy'} AND
compression={'sstable_compression': 'LZ4Compressor'};
作为脚注,
sstable2json
输出中的墓碑标记如下:e
- 过期的 TTLd
- 删除的值(墓碑)t
- 删除的值范围(范围墓碑)关于cassandra - Cassandra 支持哪些类型的墓碑?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27776337/