从Cassandra获取最后插入的数据

Hit*_*esh 3 cql cassandra datastax-enterprise datastax cassandra-2.0

我们的表中有一个主键UUID.要获得最后一条记录,我们按desc uuid字段排序.但是,因为此字段不是整数,所以我们始终不会获得最后一条记录.有没有办法获得最后的记录?请注意,我们不想制作群集列.

这是我的桌子

ArtistId (partition key)  | SongId (primary key, uuid) |  Song
--------------------------------------------
1                         | 9a9fa429-c349-4137         |  abc
1                         | 9a9fa429-b349-6137         |  cde
1                         | 9a9ga429-a349-6132         |  fgh
2                         | 249ga429-a909-6542         |  ijk
2                         | 249kg429-a239-3652         |  lmn
1                         | i55oa429-a909-3462         |  opq
1                         | e4235k59-4ik9-6542         |  rst
Run Code Online (Sandbox Code Playgroud)

shu*_*tty 5

TL&DR:TimeUUID cql3类型来救援!

如果您有这样的数据库架构:

create table music (
  artist_id int,
  song_id timeuuid,
  song text,
  primary key (artist_id, song_id)
) with clustering order by (song_id desc);
Run Code Online (Sandbox Code Playgroud)

请注意原始架构的这些更改:

  • uuid 变成 timeuuid
  • 聚类键反向排序

该表包含一些数据:

 artist_id | dateOf(song_id)          | song
-----------+--------------------------+------
         1 | 2015-05-29 13:25:15+0300 |  baz
         1 | 2015-05-29 13:25:11+0300 |  bar
         1 | 2015-05-29 13:25:06+0300 |  foo
         2 | 2015-05-29 13:25:19+0300 |  qux
Run Code Online (Sandbox Code Playgroud)

要获取固定artist_id的最新song_id,您可以运行以下查询:

select artist_id,dateOf(song_id),song from music where artist_id=1 limit 1;
Run Code Online (Sandbox Code Playgroud)