cassandra删除数据问题

来源:互联网 发布:观看农村淘宝的感想 编辑:程序博客网 时间:2024/06/16 21:11

cassandra删除数据和Oracle有点不一样
常规的写法

cassandra@cqlsh> delete from szmb.satellite where protime <'2017-8-10 15:02:35';InvalidRequest: code=2200 [Invalid query] message="Some partition key parts are missing: id"

使用这种方式却可以

delete from szmb.satellite where  id  in(a0befbad-8eec-479b-a7f3-bd7360452323, 6c285d62-864a-4570-892d-d2c0a9275bd3);

Some partition key parts are missing: id

缺少一些分区键:id

网上找的基本上都是主键查询的删除
类似:DELETE FROM emp WHERE emp_id=3;

如何根据时间删除数据呢?这个问题困扰很久了。

下载到一本【Cassandra权威指南(中文版) 高清.PDF】在删除一章节中看到 cassandra压根没有delete操作 只有remove
不过例子里面没有写怎么删除表数据。只有一行
client.remove(key,colPath,clock,ConsistencyLevel.All);
这些字段都不明确。怎么删除表里面过期的数据还是没有解决!
官网api
在这里也找不到线索。可能英语不好没发现,按道理是应该有的。
官网摘抄

DELETEDeleting rows or parts of rows uses the DELETE statement:delete_statement ::=  DELETE [ simple_selection ( ',' simple_selection ) ]                      FROM table_name                      [ USING update_parameter ( AND update_parameter )* ]                      WHERE where_clause                      [ IF ( EXISTS | condition ( AND condition )*) ]For instance:DELETE FROM NerdMovies USING TIMESTAMP 1240003134 WHERE movie = 'Serenity';DELETE phone FROM Users WHERE userid IN (C73DE1D3-AF08-40F3-B124-3FF3E5109F22, B70DE1D0-9908-4AE3-BE34-5573E5B09F14);The DELETE statement deletes columns and rows. If column names are provided directly after the DELETE keyword, only those columns are deleted from the row indicated by the WHERE clause. Otherwise, whole rows are removed.The WHERE clause specifies which rows are to be deleted. Multiple rows may be deleted with one statement by using an IN operator. A range of rows may be deleted using an inequality operator (such as >=).DELETE supports the TIMESTAMP option with the same semantics as in updates.In a DELETE statement, all deletions within the same partition key are applied atomically and in isolation.A DELETE operation can be conditional through the use of an IF clause, similar to UPDATE and INSERT statements. However, as with INSERT and UPDATE statements, this will incur a non-negligible performance cost (internally, Paxos will be used) and so should be used sparingly.

DELETE语句中,在相同的分区键删除应用原子性和隔离。
删除操作可以通过使用if子句来实现,类似于UPDATE语句和INSERT语句。然而,与INSERT和UPDATE语句,这将产生一个不可忽略的性能成本(内部,Paxos将用)等应谨慎使用。
终于在
这里写图片描述
找到一个说明。
尴尬了 我还是不知道怎么写这个删除语句

assandra@cqlsh> DELETE FROM szmb.satellite USING TIMESTAMP 1502310523 WHERE prodate = '20170810';InvalidRequest: code=2200 [Invalid query] message="Some partition key parts are missing: id"cassandra@cqlsh> DELETE FROM szmb.satellite USING TIMESTAMP 1502310523 WHERE id!=null';   ... ;cassandra@cqlsh> DELETE FROM szmb.satellite USING TIMESTAMP 1502310523 WHERE id!=null;InvalidRequest: code=2200 [Invalid query] message="Unsupported "!=" relation: id != NULL"cassandra@cqlsh> DELETE FROM szmb.satellite USING TIMESTAMP 1502310523 WHERE id not null;SyntaxException: <ErrorMessage code=2000 [Syntax error in CQL query] message="line 1:63 no viable alternative at input 'not' (...satellite USING TIMESTAMP 1502310523 WHERE [id] not...)">cassandra@cqlsh>

感觉这个异常才是关键

InvalidRequest: code=2200 [Invalid query] message="Some partition key parts are missing: id"

cassandra怎么判断不等于空 或者判断不等于某值
感觉要用这个ID做删除的条件才行。

cassandra@cqlsh> DELETE FROM szmb.satellite USING TIMESTAMP 1502310523 WHERE id= 2ec8e7b0-d33e-4bfe-8b42-bf4dcfd352c1;OperationTimedOut: errors={}, last_host=192.168.1.2cassandra@cqlsh>cassandra@cqlsh> DELETE FROM szmb.satellite USING TIMESTAMP 1502310523 WHERE id= 2ec8e7b0-d33e-4bfe-8b42-bf4dcfd352c1;cassandra@cqlsh>

这样子删是没问题的。

cassandra@cqlsh>  select count(*) from szmb.satellite;ReadTimeout: code=1200 [Coordinator node timed out waiting for replica nodes' responses] message="Operation timed out - received only 0 responses." info={'received_responses': 0, 'required_responses': 1, 'consistency': 'ONE'}cassandra@cqlsh>  select count(*) from szmb.satellite; count-------  1157(1 rows)Warnings :Aggregation query used without partition keycassandra@cqlsh>

明天来搞吧,艾西

——————————分割线—————————————
2017年8月20日13:18:59记录 深圳今天好热

刚测试了下 参考了这篇
终于找到一个按条件能删除成功的例子

他的字段是text类型的。
我改造表设计了一个时间类型的字段 如下

CREATE TABLE tlp_lab.tombstonet (protime timestamp, filename varchar,fruit text, date text, crates set<int>, PRIMARY KEY (protime,fruit));INSERT INTO tlp_lab.tombstonet(protime,filename,fruit, date, crates) VALUES ('2017-8-21 13:10:34', '201708121122.ref', 'apple', '20170616', {1,2,3,4,5});INSERT INTO tlp_lab.tombstonet (protime,filename,fruit, date, crates) VALUES ('2017-8-22 13:10:34', '201708121122.ref', 'apple', '20170817', {1,2,3});INSERT INTO tlp_lab.tombstonet (protime,filename,fruit, date, crates) VALUES ('2017-8-23 13:10:34', '201708121122.ref', 'pickles', '20170916', {6,7,8}) USING TTL 2592000;DELETE FROM tlp_lab.tombstonet WHERE fruit='apple'   and protime>'2017-8-21 13:12:09';

期间还发现 删除的时候好像必须有全部主键的条件 如果没有则报错
InvalidRequest: code=2200 [Invalid query] message=”Some partition key parts are missing: filename”
会说missing主键名字

增加这个时间类型主键删除的时候报错

cassandra@cqlsh> DELETE FROM tlp_lab.tombstonet WHERE fruit='apple'   and protime>'2017-8-21 13:12:09';InvalidRequest: code=2200 [Invalid query] message="Only EQ and IN relation are supported on the partition key (unless you use the token() function)"

好像需要用函数unless you use the token() function

目前为止 发现只有上面那篇文章中text类型的可以删除。暂时先按这种方式搞了。但是主键设计有问题,删除的时候必须要主键条件,表中时间字段是可能重复的,不能当主键。考虑使用插入时间当主键,也不好设计速度太快都是毫秒微妙级别的。如果知道写不为空判断就好了。可以指定一个uuid的主键条件为不为空。再按时间删除。知识匮乏太辛苦了。这个问题折腾了好久。希望有经验的大神能指导一下。也希望能帮助刚入门遇到很多问题的童鞋。

感觉还是捉摸不透,刚只用了一个主键的时候也报错

cassandra@cqlsh> CREATE TABLE tlp_lab.tombstonet (protime timestamp, filename varchar,fruit text, date text, crates set<int>, PRIMARY KEY (date));cassandra@cqlsh> cassandra@cqlsh> cassandra@cqlsh> INSERT INTO tlp_lab.tombstonet(protime,filename,fruit, date, crates) VALUES ('2017-8-21 13:10:34', '201708121122.ref', 'apple', '20170616', {1,2,3,4,5});cassandra@cqlsh> cassandra@cqlsh> INSERT INTO tlp_lab.tombstonet (protime,filename,fruit, date, crates) VALUES ('2017-8-22 13:10:34', '201708121122.ref', 'apple', '20170817', {1,2,3});cassandra@cqlsh> cassandra@cqlsh> INSERT INTO tlp_lab.tombstonet (protime,filename,fruit, date, crates) VALUES ('2017-8-23 13:10:34', '201708121122.ref', 'pickles', '20170916', {6,7,8}) USING TTL 2592000;cassandra@cqlsh> cassandra@cqlsh> cassandra@cqlsh> DELETE FROM tlp_lab.tombstonet WHERE   date < '20170618';InvalidRequest: code=2200 [Invalid query] message="Only EQ and IN relation are supported on the partition key (unless you use the token() function)"cassandra@cqlsh> 

必须要2个及以上的主键吗?

——————–2017-8-25 12:03:24分割线———————
参考这篇

测试过程

测试过程;新建表字段如下cassandra@cqlsh> CREATE TABLE   tlp_lab.TEST_PAYLOAD   ... (   ...   BUCKET varchar,   ...   TIME_STAMP timestamp,   ...   TYPE text,   ...   PRIMARY KEY (BUCKET, TIME_STAMP)   ... ); 插入cassandra@cqlsh> insert into tlp_lab.TEST_PAYLOAD(bucket,time_stamp,type) values('test','2017-8-25 11:57:28','typetext');cassandra@cqlsh> insert into tlp_lab.TEST_PAYLOAD(bucket,time_stamp,type) values('test','2017-8-25 11:57:28','typetext');主键相同插入失败 cassandra@cqlsh> select count(*)from tlp_lab.TEST_PAYLOAD; count-------     1(1 rows)Warnings :Aggregation query used without partition keycassandra@cqlsh> insert into tlp_lab.TEST_PAYLOAD(bucket,time_stamp,type) values('test2','2017-8-25 11:57:28','typetext');cassandra@cqlsh> select count(*)from tlp_lab.TEST_PAYLOAD; count-------     2(1 rows)Warnings :Aggregation query used without partition keycassandra@cqlsh> insert into tlp_lab.TEST_PAYLOAD(bucket,time_stamp,type) values('test2','2017-8-25 10:57:28','typetext');检查插入cassandra@cqlsh> select count(*)from tlp_lab.TEST_PAYLOAD; count-------     3(1 rows)Warnings :Aggregation query used without partition keycassandra@cqlsh> insert into tlp_lab.TEST_PAYLOAD(bucket,time_stamp,type) values('test2','2017-8-24 10:57:28','typetext');cassandra@cqlsh> insert into tlp_lab.TEST_PAYLOAD(bucket,time_stamp,type) values('test2','2017-8-29 10:57:28','typetext');检查插入 cassandra@cqlsh> select count(*)from tlp_lab.TEST_PAYLOAD; count-------     5(1 rows)Warnings :Aggregation query used without partition keycassandra@cqlsh>开始删除cassandra@cqlsh> delete from tlp_lab.TEST_PAYLOAD where bucket='test' and TIME_STAMP >= '2017-8-25 12:01:09';cassandra@cqlsh> select count(*)from tlp_lab.TEST_PAYLOAD; count-------     5(1 rows)Warnings :Aggregation query used without partition keycassandra@cqlsh> delete from tlp_lab.TEST_PAYLOAD where bucket='test' and TIME_STAMP <= '2017-8-25 12:01:09';检查删除是否成功cassandra@cqlsh> select count(*)from tlp_lab.TEST_PAYLOAD; count-------     4(1 rows)Warnings :Aggregation query used without partition keycassandra@cqlsh>删除 :sandra@cqlsh> delete from tlp_lab.TEST_PAYLOAD where bucket='test' and TIME_STAMP >= '2017-8-25 12:01:09';cassandra@cqlsh> delete from tlp_lab.TEST_PAYLOAD where bucket='test' and TIME_STAMP <= '2017-8-25 12:01:09';cassandra@cqlsh> select count(*)from tlp_lab.TEST_PAYLOAD; count-------     4(1 rows)Warnings :Aggregation query used without partition keycassandra@cqlsh>cassandra@cqlsh> select * from tlp_lab.TEST_PAYLOAD; bucket | time_stamp                      | type--------+---------------------------------+----------  test2 | 2017-08-24 02:57:28.000000+0000 | typetext  test2 | 2017-08-25 02:57:28.000000+0000 | typetext  test2 | 2017-08-25 03:57:28.000000+0000 | typetext  test2 | 2017-08-29 02:57:28.000000+0000 | typetext
原创粉丝点击