简体   繁体   English

使用CQL3在Cassandra中插入任意列

[英]Inserting arbitrary columns in Cassandra using CQL3

Prior to CQL3 one could insert arbitrary columns such as columns that are named by a date: 在CQL3之前,可以插入任意列,例如由日期命名的列:

cqlsh:test>CREATE TABLE seen_ships (day text PRIMARY KEY)
                WITH comparator=timestamp AND default_validation=text;
cqlsh:test>INSERT INTO seen_ships (day, '2013-02-02 00:08:22')
                VALUES ('Tuesday', 'Sunrise');

Per this post It seems that things are different in CQL3. 根据这篇文章 ,CQL3似乎有所不同。 Is it still somehow possible to insert arbitrary columns? 是否仍然可以插入任意列? Here's my failed attempt: 这是我失败的尝试:

cqlsh:test>CREATE TABLE seen_ships (
    day text,
    time_seen timestamp,
    shipname text,
    PRIMARY KEY (day, time_seen)
);

cqlsh:test>INSERT INTO seen_ships (day, 'foo') VALUES ('Tuesday', 'bar');

Here I get Bad Request: line 1:29 no viable alternative at input 'foo' 在这里我收到Bad Request: line 1:29 no viable alternative at input 'foo'

So I try a slightly different table because maybe this is a limitation of compound keys: 所以我尝试了一个稍微不同的表,因为这可能是复合键的限制:

cqlsh:test>CREATE TABLE seen_ships ( day text PRIMARY KEY );
cqlsh:test>INSERT INTO seen_ships (day, 'foo') VALUES ('Tuesday', 'bar');

Again with the Bad Request: line 1:29 no viable alternative at input 'foo' 再次使用Bad Request: line 1:29 no viable alternative at input 'foo'

What am I missing here? 我在这里错过了什么?

There's a good blog post over on the Datastax blog about this: http://www.datastax.com/dev/blog/does-cql-support-dynamic-columns-wide-rows 在Datastax博客上有一篇很好的博客文章: http ://www.datastax.com/dev/blog/does-cql-support-dynamic-columns-wide-rows

The answer is that yes, CQL3 supports dynamic colums, just not the way it worked in earlier versions of CQL. 答案是肯定的,CQL3支持动态列,而不是它在早期版本的CQL中的工作方式。 I don't really understand your example, you mix datestamps with strings in a way I don't see how it worked in CQL2 either. 我真的不明白你的例子,你将日期戳与字符串混合在一起我不知道它在CQL2中是如何工作的。 If I understand you correctly you want to make a timeline of ship sightings, where the partition key (row key) is the day and each sighting is a time/name pair. 如果我理解正确你想要制作船舶目击的时间表,那么分区键(行键)就是当天,每个目击都是一个时间/名称对。 Here's a suggestion: 这是一个建议:

CREATE TABLE ship_sightings (
  day TEXT,
  time TIMESTAMP,
  ship TEXT,
  PRIMARY KEY (day, time)
)

And you insert entries with 并插入条目

INSERT INTO ship_sightings (day, time, ship) VALUES ('Tuesday', NOW(), 'Titanic')

however, you should probably use a TIMEUUID instead of TIMESTAMP (and the primary key could be a DATE ), since otherwise you might add two sightings with the same timestamp and only one will survive. 但是,您应该使用TIMEUUID而不是TIMESTAMP (并且主键可能是DATE ),否则您可能会添加两个具有相同时间戳的目击,并且只有一个会存活。

This was an example of wide rows, but then there's the issue of dynamic columns, which isn't exactly the same thing. 这是一个宽行的例子,但是有动态列的问题,这不是完全相同的事情。 Here's an example of that in CQL3: 以下是CQL3中的示例:

CREATE TABLE ship_sightings_with_properties (
  day TEXT,
  time TIMEUUID,
  ship TEXT,
  property TEXT,
  value TEXT,
  PRIMARY KEY (day, time, ship, property)
)

which you can insert into like this: 您可以像这样插入:

INSERT INTO ship_sightings_with_properties (day, time, ship, property, value)
VALUES ('Sunday', NOW(), 'Titanic', 'Color', 'Black')
# you need to repeat the INSERT INTO for each statement, multiple VALUES isn't
# supported, but I've not included them here to make this example shorter
VALUES ('Sunday', NOW(), 'Titanic', 'Captain', 'Edward John Smith')
VALUES ('Sunday', NOW(), 'Titanic', 'Status', 'Steaming on')
VALUES ('Monday', NOW(), 'Carapathia', 'Status', 'Saving the passengers off the Titanic')

The downside with this kind of dynamic columns is that the property names will be stored multiple times (so if you have a thousand sightings in a row and each has a property called "Captain", that string is saved a thousand times). 这种动态列的缺点是属性名称将被存储多次(因此,如果你连续观察了一千个并且每个都有一个名为“Captain”的属性,那么该字符串将被保存一千次)。 On-disk compression takes away most of that overhead, and most of the time it's nothing to worry about. 磁盘上的压缩消除了大部分开销,而且大部分时间都没有什么可担心的。

Finally a note about collections in CQL3. 最后关于CQL3中的集合的注释。 They're a useful feature, but they are not a way to implement wide rows or dynamic columns. 它们是一个有用的功能,但它们不是实现宽行或动态列的方法。 First of all they have a limit of 65536 items, but Cassandra can't enforce this limit, so if you add too many elements you might not be able to read them back later. 首先,他们有65536项限制,但Cassandra无法强制执行此限制,因此如果添加太多元素,您可能无法在以后阅读它们。 Collections are mostly for small multi-values fields -- the canonical example is an address book where each row is an entry and where entries only have a single name, but multiple phone numbers, email addresses, etc. 集合主要用于小型多值字段 - 规范示例是地址簿,其中每行是条目,条目只有一个名称,但有多个电话号码,电子邮件地址等。

It is not truly dynamic column, but most times you can get away with collections. 它不是真正的动态列,但大多数时候你可以逃避收藏。 Using Map column you might store some dynamic data 使用Map列可以存储一些动态数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM