开发者

UUID Cassandra

I am new to Cassandra. I am trying to insert开发者_如何转开发 some values to the columnfamily. The definition of columnfamily in the config file is as follows.

<ColumnFamily Name="CommandQueue"
                    ColumnType="Super"
                    CompareWith="TimeUUIDType"
                    CompareSubcolumnsWith="UTF8Type"/>

When ever I try to insert values to I always get "InvalidRequestException(why: UUIDs must be exactly 16 bytes)".

I am using batch_mutate() to insert column.

How can I insert values to the column family.


"We have an API for that" :-)

https://github.com/rantav/hector/blob/master/core/src/main/java/me/prettyprint/cassandra/utils/TimeUUIDUtils.java

This class makes it easy to build type1 UUIDs and extract the timestamps as needed. See the related test case for examples.

Hector is MIT licensed, so if you are set on doing your own thing, feel free to use whatever helps.


Below is a code snippet (from Nick Berardi's Coder Journal)

public static Guid GenerateTimeBasedGuid(DateTime dateTime)
    {
        long ticks = dateTime.Ticks - GregorianCalendarStart.Ticks;

        byte[] guid = new byte[ByteArraySize];
        byte[] clockSequenceBytes = BitConverter.GetBytes(Convert.ToInt16(Environment.TickCount
            % Int16.MaxValue));
        byte[] timestamp = BitConverter.GetBytes(ticks);

        // copy node
        Array.Copy(Node, 0, guid, NodeByte, Node.Length);

        // copy clock sequence
        Array.Copy(clockSequenceBytes, 0, guid, GuidClockSequenceByte,clockSequenceBytes.Length);

        // copy timestamp
        Array.Copy(timestamp, 0, guid, 0, timestamp.Length);

        // set the variant
        guid[VariantByte] &= (byte)VariantByteMask;
        guid[VariantByte] |= (byte)VariantByteShift;

        // set the version
        guid[VersionByte] &= (byte)VersionByteMask;
        guid[VersionByte] |= (byte)((int)GuidVersion.TimeBased << VersionByteShift);

        return new Guid(guid);
    }


I am just continuing where "Schildmejir" has stopped. This how you can actually use the generated GUID in inserting values to columnfamilies.

Mutation foobar = new Mutation()
{
     Column_or_supercolumn = new ColumnOrSuperColumn() 
       { Super_column = new SuperColumn() 
         { Name = GuidGenerator.GenerateTimeBasedGuid(DateTime.Now).ToByteArray(), 
               Columns = listOfSomeColumns
          } 
        }
};

List<Column> foobarlist = new List<Column>();
listOfChannelIds.Add(new Column() { Name = utf8Encoding.GetBytes("somename"), Value = utf8Encoding.GetBytes(somestring), Timestamp = timeStamp });

You can use the generated GUID either in SupercolumnName or columnName depending on the requirement.


Cassandra expects UUIDs to conform to RFC 4122, so you'll need to either generate compliant values yourself or use an existing library for the language of your choice (most languages have free UUID generation libraries readily available).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜