关于Cassandra中的删除和墓碑(四)

栏目: 数据库 · 发布时间: 8年前

内容简介:关于Cassandra中的删除和墓碑(四)

让我们现在看看各种类型的删除:

cell删除

在cassandra存储引擎里,一指定行里面的一列就叫做cell。

删除某一行的某一个cell如下:

DELETE crates FROM tlp_lab.tombstones WHERE fruit=’apple’ AND date =’20160617′;

这一行的crates列就会显示为“null”:

alain$ echo “SELECT * FROM tlp_lab.tombstones LIMIT 100;” | cqlsh

fruit | date | crates

———+———-+—————–

apple | 20160616 | {1, 2, 3, 4, 5}

apple | 20160617 | null

pickles | 20160616 | {6, 7, 8}

(3 rows)

执行flush以后,我们会得到一个新的sstable在磁盘上: mb-6-big

alain$ ll /Users/alain/.ccm/Cassa-3.7/node1/data/tlp_lab/tombstones-c379952033d311e6aa4261d6a7221ccb/

total 144

drwxr-xr-x 19 alain staff 646 Jun 16 21:12 .

drwxr-xr-x 3 alain staff 102 Jun 16 20:25 ..

drwxr-xr-x 2 alain staff 68 Jun 16 17:05 backups

-rw-r–r– 1 alain staff 43 Jun 16 20:53 mb-5-big-CompressionInfo.db

-rw-r–r– 1 alain staff 127 Jun 16 20:53 mb-5-big-Data.db

-rw-r–r– 1 alain staff 10 Jun 16 20:53 mb-5-big-Digest.crc32

-rw-r–r– 1 alain staff 16 Jun 16 20:53 mb-5-big-Filter.db

-rw-r–r– 1 alain staff 20 Jun 16 20:53 mb-5-big-Index.db

-rw-r–r– 1 alain staff 4740 Jun 16 20:53 mb-5-big-Statistics.db

-rw-r–r– 1 alain staff 61 Jun 16 20:53 mb-5-big-Summary.db

-rw-r–r– 1 alain staff 92 Jun 16 20:53 mb-5-big-TOC.txt

-rw-r–r– 1 alain staff 43 Jun 16 21:12 mb-6-big-CompressionInfo.db

-rw-r–r– 1 alain staff 43 Jun 16 21:12 mb-6-big-Data.db

-rw-r–r– 1 alain staff 10 Jun 16 21:12 mb-6-big-Digest.crc32

-rw-r–r– 1 alain staff 16 Jun 16 21:12 mb-6-big-Filter.db

-rw-r–r– 1 alain staff 9 Jun 16 21:12 mb-6-big-Index.db

-rw-r–r– 1 alain staff 4701 Jun 16 21:12 mb-6-big-Statistics.db

-rw-r–r– 1 alain staff 59 Jun 16 21:12 mb-6-big-Summary.db

-rw-r–r– 1 alain staff 92 Jun 16 21:12 mb-6-big-TOC.txt

下面是mb-6-big的内容:

alain$ SSTabledump /Users/alain/.ccm/Cassa-3.7/node1/data/tlp_lab/tombstones-c379952033d311e6aa4261d6a7221ccb/mb-6-big-Data.db

[

{

“partition” : {

“key” : [ “apple” ],

“position” : 0

},

“rows” : [

{

“type” : “row”,

“position” : 19,

“clustering” : [ “20160617” ],

“cells” : [

{ “name” : “crates”, “deletion_info” : { “marked_deleted” : “2016-06-16T19:10:53.267240Z”, “local_delete_time” : “2016-06-16T19:10:53Z” } }

]

}

]

}

]

看这个删除cell的目标和插入的一行cells长的多像,分区信息partition,row和cells都有,只是没有liveness_info。删除信息的时间戳也记录了下来(deletion_info记录在cell级别)。对了,这就是一个cell墓碑。

行删除(Row delete)

从一个分区删除一行用下面的语句:

DELETE FROM tlp_lab.tombstones WHERE fruit=’apple’ AND date =’20160617′;

删除以后,这一行和想的一样就查不到了:

alain$ echo “SELECT * FROM tlp_lab.tombstones LIMIT 100;” | cqlsh

fruit | date | crates

———+———-+—————–

apple | 20160616 | {1, 2, 3, 4, 5}

pickles | 20160616 | {6, 7, 8}

(2 rows)

flush以后得到另外一个SSTable在磁盘上:mb-7-big,内容如下:

alain$ SSTabledump /Users/alain/.ccm/Cassa-3.7/node1/data/tlp_lab/tombstones-c379952033d311e6aa4261d6a7221ccb/mb-7-big-Data.db

[

{

“partition” : {

“key” : [ “apple” ],

“position” : 0

},

“rows” : [

{

“type” : “row”,

“position” : 19,

“clustering” : [ “20160617” ],

“deletion_info” : { “marked_deleted” : “2016-06-16T19:31:41.142454Z”, “local_delete_time” : “2016-06-16T19:31:41Z” },

“cells” : [ ]

}

]

}

]

花点时间看看cells(或者columns)的值是空的数组。这就是一个row级别的墓碑,没有cells,同样没有liveness_info信息。删除信息(deletion_info)和期望的一样记录在row级别。

范围删除(Range delete)

从一个分区中删除一个范围(即多行)使用下面的语句:

DELETE FROM tlp_lab.tombstones WHERE fruit=’apple’ AND date > ‘20160615’;

这个分区的数据就不会返回了,因为没有rows存在了,如果我们有小于20160616的数据,我们就会得到下面的内容:

echo “SELECT * FROM tlp_lab.tombstones LIMIT 100;” | cqlsh

fruit | date | crates

———+———-+———–

pickles | 20160616 | {6, 7, 8}

(1 rows)

flush以后得到mb-8-big,内容如下:

alain$ SSTabledump /Users/alain/.ccm/Cassa-3.7/node1/data/tlp_lab/tombstones-c379952033d311e6aa4261d6a7221ccb/mb-8-big-Data.db

[

{

“partition” : {

“key” : [ “apple” ],

“position” : 0

},

“rows” : [

{

“type” : “range_tombstone_bound”,

“start” : {

“type” : “exclusive”,

“clustering” : [ “20160615” ],

“deletion_info” : { “marked_deleted” : “2016-06-16T19:53:21.133300Z”, “local_delete_time” : “2016-06-16T19:53:21Z” }

}

},

{

“type” : “range_tombstone_bound”,

“end” : {

“type” : “inclusive”,

“deletion_info” : { “marked_deleted” : “2016-06-16T19:53:21.133300Z”, “local_delete_time” : “2016-06-16T19:53:21Z” }

}

}

]

}

]

就像我们看到的,现在我们看到一个新的特别的写入,不再是一个row类型,而是range_tombstone_bound,有一个start和end。从clustering key=20160615(排除)到无穷。这个range_tombstone_bound类型的墓碑属于分区key为apple的。所以从磁盘空间的角度来看,一个删除范围是非常有效的手段,我们不需要把没个cell的删除信息都写入,而是只写删除边界。

分区删除(Partition delete)

删除整个分区使用:

DELETE FROM tlp_lab.tombstones WHERE fruit=’pickles’;

删除以后,这个分区的数据就不会再显示了,现在这个表空了:

a lain$ echo “SELECT * FROM tlp_lab.tombstones LIMIT 100;” | cqlsh

fruit | date | crates

——-+——+——–

(0 rows)

flush以后得到一个新的sstable:mb-9-big,内容如下:

alain$ SSTabledump /Users/alain/.ccm/Cassa-3.7/node1/data/tlp_lab/tombstones-c379952033d311e6aa4261d6a7221ccb/mb-9-big-Data.db

[

{

“partition” : {

“key” : [ “pickles” ],

“position” : 0,

“deletion_info” : { “marked_deleted” : “2016-06-17T09:38:52.550841Z”, “local_delete_time” : “2016-06-17T09:38:52Z” }

}

}

]

然后同样,我们插入一个特殊标记,一个partition类型的墓碑,有一个deletion_info,没有rows信息。

注意:当使用集合类型,当你INSERT或者UPDATE整个集合而不是部分更新的时候都会产生了一个range墓碑,插入一个已经集合覆盖之前的数据,而不是追加和更新里面的item,会导致一个range墓碑插入在新数据插入之前。因为这个删除操作是隐藏暗含的操作,所以会导致奇怪的、让人沮丧的墓碑问题。

使用SSTabledump,我们再仔细看看。插入一条上面说的集合数据,不做任何删除,你会看到里面已经有一个墓碑了:

“cells” : [

{ “name” : “crates”, “deletion_info” : { “marked_deleted” : “2016-06-16T18:52:41.900450Z”, “local_delete_time” : “2016-06-16T18:52:41Z” } },

{ “name” : “crates”, “path” : [ “1” ], “value” : “” },

{ “name” : “crates”, “path” : [ “2” ], “value” : “” },

{ “name” : “crates”, “path” : [ “3” ], “value” : “” },

{ “name” : “crates”, “path” : [ “4” ], “value” : “” },

{ “name” : “crates”, “path” : [ “5” ], “value” : “” }

]

再邮件列表里,我发现James Ravn同学已经指出了这个问题,使用了list类型做例子,其实对于所有的集合类型都是这样的。所以我不打算展开更多细节,我这里只想指出这一点,因为这个特性比较奇怪。参考 http://www.jsravn.com/2015/05/13/cassandra-tombstones-collections.html#lists


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Definitive Guide to MongoDB

The Definitive Guide to MongoDB

Peter Membrey、Wouter Thielen / Apress / 2010-08-26 / USD 44.99

MongoDB, a cross-platform NoSQL database, is the fastest-growing new database in the world. MongoDB provides a rich document orientated structure with dynamic queries that you’ll recognize from RDMBS ......一起来看看 《The Definitive Guide to MongoDB》 这本书的介绍吧!

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具