3

嗨,我想用复合键插入这种列族行:

CREATE TABLE my_items (
user_id uuid,
item_id uuid,
description varchar,
 PRIMARY KEY (user_id, item_id));

所以我试试这个:

StringSerializer stringSerializer = StringSerializer.get();
    UUIDSerializer uuidSerializer = UUIDSerializer.get();
    CompositeSerializer compositeSerializer = CompositeSerializer.get();

    HColumn<String, UUID> hColumnObj_userID = HFactory.createColumn("user_id", userID, stringSerializer, uuidSerializer);
    HColumn<String, UUID> hColumnObj_itemID= HFactory.createColumn("item_id", itemID, stringSerializer, uuidSerializer);

    Mutator<Composite> mutator = HFactory.createMutator(
            repository.getKeyspace(),
            compositeSerializer);
    Composite colKey = new Composite();
    colKey.addComponent(userID, uuidSerializer);
    colKey.addComponent(itemID, uuidSerializer);

    mutator.addInsertion(colKey,
            "my_items", hColumnObj_userID);
    mutator.addInsertion(colKey,
            "my_items", hColumnObj_itemID);

    mutator.execute();

上面的代码有什么问题?我不断收到此错误:“InvalidRequestException(为什么:UUIDs 必须正好是 16 个字节)”。以及如何将数据插入到我上面描述的列族中。

干杯

4

1 回答 1

7

看起来 Hector 期待一个包含 UUID 和字符串的 Composite,但只找到了一个字符串。

在编写 Hector 代码之前,您必须将 create DDL 转换为 CQL 使用的实际存储模式。在这种情况下,即使您有两个主键,也只有第一个 user_id 用作行键。情况总是如此。任何其他主键(在本例中为 item_id)用于为除第一个主键之外的每一列形成复合列名称。这意味着当您的 my_items 列族使用 Hector 时,您必须编写两列,一列用于 item_ID,一列用于描述。

item_id 值的列名是由主键 2...n 的值(在本例中为 item_id)和值的常量字符串名称(“item_id”)组成的组合。

描述值的列名也是 item_id 值和值的名称(“描述”)的组合。

如果您编写了 3 个 CQL 表行,每个行具有相同的 user_id 但具有不同的 item_id 值,那么您最终会得到一个列族行,其行键是公共 user_id 值并且有 6 列、一个 item_id 列和一个描述3 个 CQL 表行中的每一行的列。

代码应如下所示:

import java.util.UUID;

import me.prettyprint.cassandra.serializers.CompositeSerializer;
import me.prettyprint.cassandra.serializers.IntegerSerializer;
import me.prettyprint.cassandra.serializers.StringSerializer;
import me.prettyprint.cassandra.serializers.UUIDSerializer;
import me.prettyprint.hector.api.Keyspace;
import me.prettyprint.hector.api.beans.Composite;
import me.prettyprint.hector.api.beans.HColumn;
import me.prettyprint.hector.api.beans.AbstractComposite.ComponentEquality;
import me.prettyprint.hector.api.factory.HFactory;
import me.prettyprint.hector.api.mutation.Mutator;

    // put this here to make it compile cleanly

    Keyspace keyspace = null;
    UUID userID = null;
    UUID itemID = null;
    String description = null;

            // Row key is user_id of type UUID

    Mutator<UUID> mutator = HFactory.createMutator(
            keyspace,
            UUIDSerializer.get());

        // write column for itemID.  
        // Column name is composite of itemID value and constant "item_id"
        // Row key is value of userID

    Composite itemIdColumnName = new Composite();
    itemIdColumnName.addComponent(itemID    , UUIDSerializer.get());
    itemIdColumnName.addComponent("item_id" , StringSerializer.get());
        // HFactory.createColumn takes args: column name, column value, serializer for column name, serializer for column value
    HColumn<Composite, UUID> hColumnObj_itemID = HFactory.createColumn(itemIdColumnName, userID, new CompositeSerializer(), UUIDSerializer.get());
    mutator.addInsertion(userID, "my_items", hColumnObj_itemID);

    // write column for description.  
    // Column name is composite of itemID value and constant "description"
    // Row key is value of userID

    Composite descriptionColumnName = new Composite();
    itemIdColumnName.addComponent(itemID    , UUIDSerializer.get());
    itemIdColumnName.addComponent("description" , StringSerializer.get());
    HColumn<Composite, String> hColumnObj_description = HFactory.createColumn(descriptionColumnName, description , new CompositeSerializer(), StringSerializer.get());
    mutator.addInsertion(userID, "my_items", hColumnObj_description);

    mutator.execute();
于 2012-08-08T17:04:38.460 回答