使用cqlsh将CSV导入Cassandra时的列顺序

时间:2015-10-02 20:32:01

标签: cassandra cql cqlsh

我是Cassandra的新手。 CQL似乎忽略了CREATE TABLE语句中的列顺序,并且首先按主键排序列,然后按字典顺序排列列。我理解它们是如何在内部存储的,但是从传统的数据库角度来看,无视列顺序并将实现细节泄露给用户是非常令人惊讶的。这是在任何地方记录的吗?

%% If the first item in the list is greater than 2, 
%% return a new list consisting of the first item prepended 
%% to the result of recursively processing the rest of the list
f2([X|Xs]) when X > 2 ->
  [X|f2(XS)];

%% In all other cases where the list is not empty, save off 
%% the first element, then create a new list whose first 
%% element is the first element returned by the recursive call, 
%% and whose second element is the sum of that element and the 
%% first element saved above, with the rest of the recursive 
%% result appended
f2([X|Xs]) ->
  [Y|Ys] = f2(Xs),
  [Y,X+Y|Ys];

%% return 1 if passed an empty list
f2([]) -> [1].

这使您很难按照您认为正在使用的顺序导入包含列的CSV文件。

1a. `f2([3,1,2])` matches the first clause, setting `X=3`, `Xs=[1,2]`

     2a. `f2([1,2])` matches the second clause, setting `X=1`, `Xs=[2]`  

         3a. `f2([2])` matches the second clause, setting `X=2`, `Xs=[]`

             4. `f2([])` matches the second clause, returning [1]

         3b. `[Y|Ys] = [1]` sets `Y=1`, `Ys=[]`

         3c. returns `[1, 1 + 2 | []]` i.e. `[1,3]`

     2b. `[Y|Ys] = [1,3]` sets `Y=1`, `Ys=[3]`

     2c. returns `[1, 1 + 1 | [3]]` i.e. `[1,2,3]`

 1b.  returns `[3|[1,3,2]]` i.e. `[3,1,2,3]`

解决方案似乎是指定COPY语句中的列(或重新排序CSV数据)。

[cqlsh 4.1.1 | Cassandra 2.1.8 | CQL spec 3.1.1 | Thrift protocol 19.39.0]

cqlsh:test> create table test (c int primary key, b text, a int);
cqlsh:test> describe table test;

CREATE TABLE test (
  c int,
  a int,
  b text,
  PRIMARY KEY (c)
)

2 个答案:

答案 0 :(得分:0)

您应指定要与之交易的列。永远不要假设使用Cassandra的列顺序,即使你改变你的csv文件以匹配它更安全的顺序,即使在具有许多列的表上指定确切的列。

Cassandra使用列顺序和特定存储位置来更快地访问数据。

答案 1 :(得分:0)

Cassandra按以下方式命令其列:

  1. 分区键
  2. 群集密钥
  3. 按字母顺序排列的剩余列。
  4. 例如说我创建了下表:

    CREATE TABLE products (
    product_id text,
    account_id text,
    avg_rating float,
    brand text,
    brand_name text
    PRIMARY KEY (product_id, account_id)
    ) WITH CLUSTERING ORDER BY (account_id ASC);
    

    第1列= product_id(因为它是分区键)
    第二列= account_id(因为它是一个聚类键)
    其余列按字母顺序排列。

相关问题