Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Cassandra Wiki" for 
change notification.

The "FAQ" page has been changed by JesseMcConnell.
http://wiki.apache.org/cassandra/FAQ?action=diff&rev1=29&rev2=30

--------------------------------------------------

   * [[#node_clients_connect_to|Does it matter which node a Thrift client 
connects to?]]
   * [[#what_kind_of_hardware_should_i_use|What kind of hardware should I run 
Cassandra on?]]
   * [[#architecture|What are SSTables and Memtables?]]
+  * [[#working_with_timeuuid_in_java|Why is it so hard to work with 
TimeUUIDType in Java?]]
  
  <<Anchor(cant_listen_on_ip_any)>>
  == Why can't I make Cassandra listen on 0.0.0.0 (all my addresses)? ==
@@ -70, +71 @@

  is a file of key/value string pairs, sorted by keys.
  There are important Memtable parameters described in 
[[MemtableThresholds|MemtableThresholds]].
  
+ 
+ <<Anchor(working_with_timeuuid_in_java)>>
+ == Why is it so hard to work with TimeUUIDType in Java? ==
+ 
+ TimeUUID's are problematic in java but here is one way to work with them and 
Cassandra.
+ 
+ Use the UUID generator from: http://johannburkard.de/software/uuid/
+ 
+ Below are three methods that are quite useful in working with the uuids as 
they come in and
+ out of Cassandra.
+ 
+ Generate a new UUID to use in a TimeUUIDType sorted column family.
+ 
+ {{{
+       /**
+        * Gets a new time uuid.
+        *
+        * @return the time uuid
+        */
+       public static java.util.UUID getTimeUUID()
+       {
+               return java.util.UUID.fromString(new 
com.eaio.uuid.UUID().toString());
+       }
+ }}}   
+       
+ When you read out of cassandra your getting a byte[] that needs to be 
converted into a TimeUUID
+ and since the java.util.UUID doesn't seem to have a simple way of doing this, 
pass it through
+ the eaio uuid dealio again.
+ 
+ {{{
+       /**
+        * Returns an instance of uuid.
+        * 
+        * @param uuid the uuid
+        * @return the java.util. uuid
+        */
+       public static java.util.UUID toUUID( byte[] uuid )
+       {
+         long msb = 0;
+         long lsb = 0;
+         assert uuid.length == 16;
+         for (int i=0; i<8; i++)
+             msb = (msb << 8) | (uuid[i] & 0xff);
+         for (int i=8; i<16; i++)
+             lsb = (lsb << 8) | (uuid[i] & 0xff);
+         long mostSigBits = msb;
+         long leastSigBits = lsb;
+       
+         com.eaio.uuid.UUID u = new com.eaio.uuid.UUID(msb,lsb);
+         return java.util.UUID.fromString(u.toString());
+       }
+ }}}
+ 
+ When you want to actually place the UUID into the Column then you'll want to 
convert it like this.
+ This method is often used in conjuntion with the getTimeUUID() mentioned 
above.
+ 
+ {{{
+       /**
+        * As byte array.
+        * 
+        * @param uuid the uuid
+        * 
+        * @return the byte[]
+        */
+       public static byte[] asByteArray(java.util.UUID uuid) 
+       {
+           long msb = uuid.getMostSignificantBits();
+           long lsb = uuid.getLeastSignificantBits();
+           byte[] buffer = new byte[16];
+ 
+           for (int i = 0; i < 8; i++) {
+                   buffer[i] = (byte) (msb >>> 8 * (7 - i));
+           }
+           for (int i = 8; i < 16; i++) {
+                   buffer[i] = (byte) (lsb >>> 8 * (7 - i));
+           }
+ 
+           return buffer;
+       }
+ }}}
+ 

Reply via email to