简体   繁体   English

Cassandra并使用cql来插入列

[英]Cassandra and using cql to INSERT a column

Trying to understand some fundamentals in Cassandra, I was under the impression that one of the advantages a developer can take in designing a data model is by dynamically adding columns to a row identified by a key. 为了理解Cassandra中的一些基础知识,我的印象是开发人员在设计数据模型时可以采用的一个优点是动态地将列添加到由键标识的行中。 That means I can model my data so that if it makes sense, a key can be something such as a user_id from a relational database, and I can for example, create arbitrary amounts of columns that relate to that user. 这意味着我可以对我的数据进行建模,以便在有意义的情况下,密钥可以是诸如关系数据库中的user_id之类的东西,例如,我可以创建与该用户相关的任意数量的列。

What I'm not understanding is why there is so much emphasis to predefined columns in CLQ examples, particularly in the CREATE TABLE/COLUMNFAMILY examples: 我不理解的是为什么CLQ示例中的预定义列非常强调,特别是在CREATE TABLE / COLUMNFAMILY示例中:

CREATE TABLE emp (
  empID int,
  deptID int,
  first_name varchar,
  last_name varchar,
  PRIMARY KEY (empID, deptID)
);

Wouldn't this type of model make more sense to just stuff into a relational database? 将这种类型的模型放入关系数据库中会不会更有意义? What if I don't know my column name until runtime and need to dynamically create it? 如果我在运行时之前不知道我的列名并且需要动态创建它,该怎么办? Do I have to use ALTER TABLE to add a new column to the row using CLQ? 我是否必须使用ALTER TABLE使用CLQ向行添加新列? The particular app use-case I have in mind I would just need a key identifier and arbitrary column names where the column name might include a timestamp+variable_identifier. 我想到的特定应用程序用例我只需要一个键标识符和任意列名,其中列名可能包含时间戳+ variable_identifier。

Is Cassandra the right tool for that? Cassandra是正确的工具吗? Are the predefined columns in documentation nothing more than an example? 文档中的预定义列只是一个示例吗? How does one add a dynamic column name with an existing column family/table? 如何使用现有列族/表添加动态列名?

Do I have to use ALTER TABLE to add a new column to the row using CLQ? 我是否必须使用ALTER TABLE使用CLQ向行添加新列?

Yes, the schema must be defined before you can insert into 'new columns'. 是的,必须先定义架构,然后才能插入“新列”。 However you could define 1 column that is a collection of data. 但是,您可以定义1列作为数据集合。 Look at the 'tag' example in datastax's 'thrift to cql upgrade' blog under mixing dynamic and static columns . 混合动态和静态列下,查看datastax的'thrift to cql upgrade'博客中的'tag'示例。

How does one add a dynamic column name with an existing column family/table? 如何使用现有列族/表添加动态列名?

In CQL you have to first alter the structure of the table (column family) using the ALTER keyword. 在CQL中,您必须首先使用ALTER关键字更改表(列族)的结构。 My guess is that this is to ensure that column families contain the specified columns eliminating the chance of a column being added by mistake (better data quality). 我的猜测是,这是为了确保列族包含指定的列,从而消除错误添加列的可能性(更好的数据质量)。

Is Cassandra the right tool for that? Cassandra是正确的工具吗?

I think it is, but if you need to add columns on-the-fly without specifying schema altering statements then you should probably look into thrift based APIs which can do that, but just a friendly warning, datastax advise that new applications use CQL. 我认为是这样,但是如果您需要在不指定模式更改语句的情况下即时添加列,那么您应该考虑基于thrift的API,这可以做到这一点,但只是一个友好的警告,datastax建议新应用程序使用CQL。

My answer from the mailing list: 我在邮件列表中的回答是:

Schemalessness is not a fundemental concept to Cassandra, at all. 对于Cassandra来说,Schemalessness根本不是一个有趣的概念。 You're probably suffering from too much exposure to document databases. 您可能会遭受过多的文档数据库暴露。 Experience has shown that having schema to say "email column is text, and birth date column is a timestamp" is very useful as projects and teams grow. 经验表明,随着项目和团队的成长,使用模式说“电子邮件列是文本,出生日期列是时间戳”非常有用。

There's nothing wrong with the relational model per se (subject to the usual explanation about needing to denormalize to scale). 关系模型本身没有任何问题(根据需要对规范进行非规范化的通常解释)。 Cassandra is about making applications scale, not throwing the SQL baby out with the bathwater for the sake of being different. Cassandra是关于使应用程序扩展,而不是为了与众不同而将SQL婴儿扔出洗澡水。

That said, if you really don't know what kinds of attributes might apply (generally because they are user-generated) you can use a Map. 也就是说,如果你真的不知道可能适用哪种属性(通常是因为它们是用户生成的),你可以使用Map。

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

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