tags:

views:

105

answers:

3
+1  Q: 

UUID Cassandra.

Hi all,

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.

+1  A: 

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);
    }
Schildmeijer
A: 

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).

Nick Bastin
A: 

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.

Sandeep