Converting Guid to Cassandra UUID

1.5k Views Asked by At

I want to convert a Guid to a UUID or a string version of the same so that the following CQL query will work.

                        if (cassandraDb.ExecuteQuery(string.Format("SELECT OrderGroupId FROM Order WHERE OrderGroupId={0}", orderGroupId)).Count() <= 0)                            {

The variable 'orderGroupId' is a Guid. Obviously this is using FluentCassandra in a C#/.NET environmnet. Any hints?

Thank you.

2

There are 2 best solutions below

2
Oren On

To convert System.Guid to FluentCassandra.Types.UUIDType you just need to assign one into the other.

Same goes for FluentCassandra.Types.TimeUUIDType.

0
Allison A On

Execute the CQL query directly as a string by using the .ToString() property.

string.Format("SELECT OrderGroupId FROM Order WHERE OrderGroupId={0}", orderGroupId.ToString())

Should work fine. If it does not... you may want to try this (which I found on a comment thread on one of the C# clients):

    protected static string ToCqlString(Guid guid) {
        var bytes = guid.ToByteArray();
        StringBuilder sb = new StringBuilder(bytes.Length * 2);
        for (int i = 0; i < 16; i++) {
            if (i == 4 || i == 6 || i == 8 || i == 10) {
                sb.Append("-");
            }
            var b = bytes[i];
            sb.AppendFormat("{0:x2}", b);

        }
        return sb.ToString();
    }

Cheers, Allison