Hector - 使用复合键插入行

时间:2012-08-06 10:02:52

标签: java cassandra hector

您好我想用复合键插入这种列族行:

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(why:UUIDs必须正好是16个字节)”。如何将数据插入到我上面描述的列族中。

干杯

1 个答案:

答案 0 :(得分:7)

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

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

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

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

如果您编写了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();