【问题标题】:Cassandra Composite Column FamilyCassandra 复合柱系列
【发布时间】:2013-07-09 05:56:52
【问题描述】:

我想创建一个简单的 sql 世界需求

CREATE TABLE event_tracking (
  key text,
  trackingid timeuuid,
  entityId bigint,
  entityType text
  userid bigint
  PRIMARY KEY (key, trackingid)
)

我需要一个 cli create 命令,但我无法做到。我需要通过 cli 创建列族,因为猪无法读取通过 cqlsh 创建的列族(duh)

这是我尝试过但没有奏效的方法

 create column family event_tracking
...  WITH comparator='CompositeType(TimeUUIDType)'
...  AND key_validation_class=UTF8Type
...  AND default_validation_class = UTF8Type;

1)我不知道为什么在cqlsh中看到它时会添加值列

CREATE TABLE event_tracking (
  key text,
  trackingid timeuuid,
  value text,
  PRIMARY KEY (key, trackingid)
) WITH COMPACT STORAGE AND
  bloom_filter_fp_chance=0.010000 AND
  caching='KEYS_ONLY' AND
  comment='' AND
  dclocal_read_repair_chance=0.000000 AND
  gc_grace_seconds=864000 AND
  read_repair_chance=0.100000 AND
  replicate_on_write='true' AND
  populate_io_cache_on_flush='false' AND
  compaction={'class': 'SizeTieredCompactionStrategy'} AND
  compression={'sstable_compression': 'SnappyCompressor'};

2) 我正在使用 asynatax 插入行。

OperationResult<CqlResult<Integer, String>> result = keyspace.prepareQuery(CQL3_CF)
    .withCql("INSERT INTO event_tracking (key, column1, value) VALUES ("+System.currentTimeMillis()+","+TimeUUIDUtils.getTimeUUID(System.currentTimeMillis())+",'23232323');").execute();

但是一旦我尝试添加动态列,它就无法识别

OperationResult<CqlResult<Integer, String>> result = keyspace.prepareQuery(CQL3_CF)
.withCql("INSERT INTO event_tracking (key, column1, value, userId, event) VALUES ("+System.currentTimeMillis()+","+TimeUUIDUtils.getTimeUUID(System.currentTimeMillis())+",'23232323', 123455, 'view');").execute(); 

看来我无法通过 cql3 添加动态列

3) 如果我尝试通过 cql3 添加新列

alter table event_tracking add eventid bigint;

它给了我

Bad Request: Cannot add new column to a compact CF

【问题讨论】:

    标签: cassandra cqlsh


    【解决方案1】:

    0) 如果您使用COMPACT STORAGE 创建表,Pig 应该能够看到它,即使您是从 CQL3 创建的。但是您也需要将entityIdentityType 放入主键才能使其工作(紧凑存储基本上意味着主键中的第一列成为行键,以下成为用作列的复合类型键,然后只有一列的空间,这将是值)。

    1) 当您以旧方式创建表时,总会有一个value,它是列的值,在 CQL3 中表示为一个名为value。这就是 CQL3 将底层存储模型映射到表的方式。

    2) 您创建了一个表,其列的类型为CompositeType(TimeUUIDType),因此您只能添加TimeUUIDs 的列。您不能告诉 C* 将字符串保存为 TimeUUID 列键。

    3) 循环回 0 使用这个表:

    CREATE TABLE event_tracking (
      key text,
      trackingid timeuuid,
      entityId bigint,
      entityType text,
      userid bigint,
      PRIMARY KEY (key, trackingid, entityId, entityType)
    ) WITH COMPACT STORAGE
    

    这一假设假设每个userid 只能有一个trackingId/entityId/entityType 组合(你的大小写不一致是怎么回事,顺便说一句?)。情况并非如此,您需要走完整的动态列路线,但是您不能为 entityIdentityType 提供不同的数据类型(但在 CQL3 之前也是如此),请参阅此问题如何做动态列的例子:Inserting arbitrary columns in Cassandra using CQL3

    【讨论】:

    • 谢谢西奥。我的意图是创建列族作为键(System.getCurrentMilliSeconds()),将id跟踪为uuid并将它们作为复合键,然后将entityid、entitytype、userid和event添加为动态列。目的是即使我们同时收到并发请求,我们也可以将事件记录在同一行中。我希望我能说得通。
    • 我可以创建 CREATE TABLE event_tracking ( key text, trackingid timeuuid
    猜你喜欢
    • 1970-01-01
    • 2013-11-28
    • 2016-08-24
    • 2013-06-02
    • 1970-01-01
    • 2013-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多