7

我在 CQL3 中有一张这样的表

create table product_info
(
 key text,
 value text,
 Primary key (key)
);

它是一个垂直的桌子。因为我可以使用 (key , value ) 对插入新行。

样本数据将是:

产品信息

  key                |     value       
  -------------------------------------------
  product_name       |   sample_product   
  quantity           |   2
  manufacture        |   sample_manufacturer   
  ....                   ....

但我需要的是一个水平表,我可以在不改变表的情况下动态添加列。

产品信息

    product_name     |   quantity   |  manufacture           |  ....
   ------------------------------------------------------------------------------    
    sample_product   |    2         |  sample_manufacturer   |  ....

我需要像上表这样的结构,需要不断添加列。

CQL3 提供了动态添加列的选项,但在此之前我们需要更改表。

我需要知道是否有任何其他方法可以做到这一点。

我发现通过使用 thrift api 是可能的,但是由于不支持 thrift,所以不能使用它。

是否有任何其他 API,如 hector 或其他支持此功能的 API?

我确实经历了类似的堆栈溢出帖子,但我没有得到更好的解决方案。

4

2 回答 2

5
CREATE TABLE product_info(
    product_name text,
    key text,
    value text,
    PRIMARY KEY (product_name, key)
);

现在您可以插入多达 2B 个 k/v 对,因为键现在是聚类列。

INSERT INTO product_info (product_name, key, value) 
    VALUES ('iPhone 6', 'quantity', '2');

INSERT INTO product_info (product_name, key, value) 
    VALUES ('iPhone 6', 'manufacturer', 'Apple');

INSERT INTO product_info (product_name, key, value) 
    VALUES ('iPhone 6', 'quantity', '2');

INSERT INTO product_info (product_name, key, value) 
    VALUES ('iPhone 6', 'another column name', 'another column value');

但是,您没有指定查询访问模式,因此对于您的应用程序,此数据模型可能完全错误(或正常)。

于 2014-12-23T02:43:30.890 回答
1

您是否考虑过使用地图?

create table products(
 id text primary key,
 something text,
 attributes map<text, text>
);

http://www.datastax.com/documentation/cql/3.1/cql/cql_using/use_collections_c.html结尾

于 2014-12-23T11:38:32.133 回答