Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何像SQL一样在cassandra中设置自动递增ID(整数)?_Cassandra - Fatal编程技术网

如何像SQL一样在cassandra中设置自动递增ID(整数)?

如何像SQL一样在cassandra中设置自动递增ID(整数)?,cassandra,Cassandra,我有数百万张唱片,现在我得去拿最后一张最新的唱片。我想用record_id保存每条记录,并且我想在插入新记录时自动增加此record_id 例如:假设我有1000条记录,首先我需要从901到1000的最新100条记录。现在在第二个请求中,我想要下一个100最新记录,从801到900。我已经通过这么多的链接,但没有找到任何相关的。任何机构都能给出适当的解决方案吗 假设emp表包含: 名称文本, 记录\u id int, 地址文本 假设name是主键,record_id是集群键。但现在请不要讨论主键

我有数百万张唱片,现在我得去拿最后一张最新的唱片。我想用record_id保存每条记录,并且我想在插入新记录时自动增加此record_id

例如:假设我有1000条记录,首先我需要从901到1000的最新100条记录。现在在第二个请求中,我想要下一个100最新记录,从801到900。我已经通过这么多的链接,但没有找到任何相关的。任何机构都能给出适当的解决方案吗

假设emp表包含:
名称文本,
记录\u id int,
地址文本


假设name是主键,record_id是集群键。但现在请不要讨论主键的概念,因为我的要求是将id创建为集群密钥(我使用的是cassandra 2.2.3和cql 3.3.1)

Cassandra中不支持自动增量ID。但是您可以使用一个替代的集群键来确保记录从最早的到最近的顺序。

Cassandra中不支持自动增量ID。但是您可以使用一个替代的集群键来确保记录从最早的到最近的顺序。

自动增量ID在Cassandra或任何其他分布式数据库中都不起作用

为什么??假设有三个节点。两个节点同时获得对同一表的写入请求。其中一个检查表中的max ID,并得到(示例)响应2544。在可以写入新行之前,另一个节点执行相同的过程,并获得2544。现在插入两行2545,在Cassandra中,最后一次写入“获胜”,因此将丢失第一次写入

因此,这也是为什么读写方法在Cassandra中被认为是反模式的原因。正如Stefan所建议的,TimeUUID提供了一种解决此问题的方法

在Cassandra中,您需要设计符合查询模式的表。我听到的是,你想检索最后100名更新的员工。我将创建一个特定的表来提供:

CREATE TABLE employee_updates (
  datebucket text,
  record_id timeuuid,
  name text,
  address text,
  PRIMARY KEY (datebucket,record_id))
WITH CLUSTERING ORDER BY (record_id DESC);
现在,当您查询此表中的最后100条记录时:

SELECT * FROM employee_udpates WHERE datebucket='20160309' LIMIT 100;
您可以获取该特定日期的最新100条记录

注意:如果“一天”对于您的解决方案来说过于精细(每天只有少数员工记录得到更新),那么请随意将其扩展到更适用的范围

更新:

如果我想要上一张最新的100张唱片,从801张到900张呢

这个解决方案实际上有一种方法可以“翻页”结果

让我们在表中插入一些行:

> INSERT INTO employee_updates (datebucket, record_id , address , name ) VALUES ('20160309',now(),'123 main st.','Bob Kerman');
> INSERT INTO employee_updates (datebucket, record_id , address , name ) VALUES ('20160309',now(),'456 Gene ave.','Bill Kerman');
> INSERT INTO employee_updates (datebucket, record_id , address , name ) VALUES ('20160309',now(),'34534 Water st.','Jebediah Kerman');
> INSERT INTO employee_updates (datebucket, record_id , address , name ) VALUES ('20160309',now(),'843 Rocket dr.','Valentina Kerman');
> INSERT INTO employee_updates (datebucket, record_id , address , name ) VALUES ('20160309',now(),'33476 Booster way','Isabella Kerman');
> INSERT INTO employee_updates (datebucket, record_id , address , name ) VALUES ('20160309',now(),'43 Solid Rocket pl.','Helcine Kerman');
现在,让我选择今天最新的三大主题:

> SELECT datebucket, record_id, dateof(record_id), name 
  FROm employee_updates WHERE datebucket='20160309' LIMIT 3;

 datebucket | record_id                            | system.dateof(record_id) | name
------------+--------------------------------------+--------------------------+------------------
   20160309 | 511f9150-e5db-11e5-a4ba-a52893cc9f36 | 2016-03-09 09:43:02+0000 |   Helcine Kerman
   20160309 | 2f9f3670-e5db-11e5-a4ba-a52893cc9f36 | 2016-03-09 09:42:06+0000 |  Isabella Kerman
   20160309 | 23b0dc60-e5db-11e5-a4ba-a52893cc9f36 | 2016-03-09 09:41:46+0000 | Valentina Kerman

(3 rows)
由于我已将此表按降序聚集在
record\u id
上,因此只需查询比我上次读取的
record\u id
小的
即可获得接下来的3条记录。在这种情况下,应该是
23b0dc60-e5db-11e5-a4ba-a52893cc9f36

> SELECT datebucket, record_id, dateof(record_id), name 
  FROm employee_updates WHERE datebucket='20160309' 
  AND record_id < 23b0dc60-e5db-11e5-a4ba-a52893cc9f36 LIMIT 3;

 datebucket | record_id                            | system.dateof(record_id) | name
------------+--------------------------------------+--------------------------+-----------------
   20160309 | 16400100-e5db-11e5-a4ba-a52893cc9f36 | 2016-03-09 09:41:23+0000 | Jebediah Kerman
   20160309 | 0b239cf0-e5db-11e5-a4ba-a52893cc9f36 | 2016-03-09 09:41:05+0000 |     Bill Kerman
   20160309 | 00d648b0-e5db-11e5-a4ba-a52893cc9f36 | 2016-03-09 09:40:47+0000 |      Bob Kerman

(3 rows)
>选择datebucket、记录id、日期(记录id)、名称
来自员工_更新,其中datebucket='20160309'
并记录_id<23b0dc60-e5db-11e5-a4ba-a52893cc9f36限值3;
datebucket |记录| id | system.dateof(记录| id)|名称
------------+--------------------------------------+--------------------------+-----------------
20160309 | 16400100-e5db-11e5-a4ba-a52893cc9f36 | 2016-03-09 09 09:41:23+0000 | Jebediah Kerman
20160309 | 0b239cf0-e5db-11e5-a4ba-a52893cc9f36 | 2016-03-09 09:41:05+0000 |比尔·科曼
20160309 | 00d648b0-e5db-11e5-a4ba-a52893cc9f36 | 2016-03-09 09:40:47+0000 |鲍勃·科曼
(3排)

自动增量ID在Cassandra或任何其他分布式数据库中都不起作用

为什么??假设有三个节点。两个节点同时获得对同一表的写入请求。其中一个检查表中的max ID,并得到(示例)响应2544。在可以写入新行之前,另一个节点执行相同的过程,并获得2544。现在插入两行2545,在Cassandra中,最后一次写入“获胜”,因此将丢失第一次写入

因此,这也是为什么读写方法在Cassandra中被认为是反模式的原因。正如Stefan所建议的,TimeUUID提供了一种解决此问题的方法

在Cassandra中,您需要设计符合查询模式的表。我听到的是,你想检索最后100名更新的员工。我将创建一个特定的表来提供:

CREATE TABLE employee_updates (
  datebucket text,
  record_id timeuuid,
  name text,
  address text,
  PRIMARY KEY (datebucket,record_id))
WITH CLUSTERING ORDER BY (record_id DESC);
现在,当您查询此表中的最后100条记录时:

SELECT * FROM employee_udpates WHERE datebucket='20160309' LIMIT 100;
您可以获取该特定日期的最新100条记录

注意:如果“一天”对于您的解决方案来说过于精细(每天只有少数员工记录得到更新),那么请随意将其扩展到更适用的范围

更新:

如果我想要上一张最新的100张唱片,从801张到900张呢

这个解决方案实际上有一种方法可以“翻页”结果

让我们在表中插入一些行:

> INSERT INTO employee_updates (datebucket, record_id , address , name ) VALUES ('20160309',now(),'123 main st.','Bob Kerman');
> INSERT INTO employee_updates (datebucket, record_id , address , name ) VALUES ('20160309',now(),'456 Gene ave.','Bill Kerman');
> INSERT INTO employee_updates (datebucket, record_id , address , name ) VALUES ('20160309',now(),'34534 Water st.','Jebediah Kerman');
> INSERT INTO employee_updates (datebucket, record_id , address , name ) VALUES ('20160309',now(),'843 Rocket dr.','Valentina Kerman');
> INSERT INTO employee_updates (datebucket, record_id , address , name ) VALUES ('20160309',now(),'33476 Booster way','Isabella Kerman');
> INSERT INTO employee_updates (datebucket, record_id , address , name ) VALUES ('20160309',now(),'43 Solid Rocket pl.','Helcine Kerman');
现在,让我选择今天最新的三大主题:

> SELECT datebucket, record_id, dateof(record_id), name 
  FROm employee_updates WHERE datebucket='20160309' LIMIT 3;

 datebucket | record_id                            | system.dateof(record_id) | name
------------+--------------------------------------+--------------------------+------------------
   20160309 | 511f9150-e5db-11e5-a4ba-a52893cc9f36 | 2016-03-09 09:43:02+0000 |   Helcine Kerman
   20160309 | 2f9f3670-e5db-11e5-a4ba-a52893cc9f36 | 2016-03-09 09:42:06+0000 |  Isabella Kerman
   20160309 | 23b0dc60-e5db-11e5-a4ba-a52893cc9f36 | 2016-03-09 09:41:46+0000 | Valentina Kerman

(3 rows)
由于我已将此表按降序聚集在
record\u id
上,因此只需查询比我上次读取的
record\u id
小的
即可获得接下来的3条记录。在这种情况下,应该是
23b0dc60-e5db-11e5-a4ba-a52893cc9f36

> SELECT datebucket, record_id, dateof(record_id), name 
  FROm employee_updates WHERE datebucket='20160309' 
  AND record_id < 23b0dc60-e5db-11e5-a4ba-a52893cc9f36 LIMIT 3;

 datebucket | record_id                            | system.dateof(record_id) | name
------------+--------------------------------------+--------------------------+-----------------
   20160309 | 16400100-e5db-11e5-a4ba-a52893cc9f36 | 2016-03-09 09:41:23+0000 | Jebediah Kerman
   20160309 | 0b239cf0-e5db-11e5-a4ba-a52893cc9f36 | 2016-03-09 09:41:05+0000 |     Bill Kerman
   20160309 | 00d648b0-e5db-11e5-a4ba-a52893cc9f36 | 2016-03-09 09:40:47+0000 |      Bob Kerman

(3 rows)
>选择datebucket、记录id、日期(记录id)、名称
来自员工_更新,其中datebucket='20160309'
并记录_id<23b0dc60-e5db-11e5-a4ba-a52893cc9f36限值3;
datebucket |记录| id | system.dateof(记录| id)|名称
------------+--------------------------------------+--------------------------+-----------------
20160309 | 16400100-e5db-11e5-a4ba-a52893cc9f36 | 2016-03-09 09 09:41:23+0000 | Jebediah Kerman
20160309 | 0b239cf0-e5db-11e5-a4ba-a52893cc9f36 | 2016-03-09 09:41:05+0000 |比尔·科曼
20160309 | 00d648b0-e5db-11e5-a4ba-a52893cc9f36 | 2016-03-09 09:40:47+000