Reference address http://wiki.apache.org/cassandra/FAQ#working_with_timeuuid_in_java
Download a pack http://johannburkard.de/software/uuid/
Sample Code:
Code Import java. util. List;
Import org. Apache. Thrift. Transport. tTransport;
Import org. Apache. Thrift. Transport. tsocket;
Import org. Apache. Thrift. Protocol. tprotocol;
Import org. Apache. Thrift. Protocol. tbinaryprotocol;
Import org. Apache. Thrift. texception;
Import org. Apache. Cassandra. Thrift. Cassandra;
Import org. Apache. Cassandra. Thrift. column;
Import org. Apache. Cassandra. Thrift. columnorsupercolumn;
Import org. Apache. Cassandra. Thrift. columnparent;
Import org. Apache. Cassandra. Thrift. columnpath;
Import org. Apache. Cassandra. Thrift. consistencylevel;
Import org. Apache. Cassandra. Thrift. invalidrequestexception;
Import org. Apache. Cassandra. Thrift. slicepredicate;
Import org. Apache. Cassandra. Thrift. slicerange;
Import org. Apache. Cassandra. Thrift. timedoutexception;
Import org. Apache. Cassandra. Thrift. unavailableexception;
Public class test {
Public static void main (string [] ARGs) throws exception, invalidrequestexception, unavailableexception, timedoutexception, texception {
Byte [] UUID = asbytearray (gettimeuuid ());
TTransport TR = new tsocket ("localhost", 9160 );
Tprotocol proto = new tbinaryprotocol (TR );
Cassandra. Client client = new Cassandra. Client (PROTO );
Tr. open ();
String key_user_id = "123 ";
Long timestamp = system. currenttimemillis ();
Columnpath CP = new columnpath ();
CP. setcolumn (UUID );
CP. setcolumn_family ("standardbyuuid1 ");
Client. insert ("keyspace1 ",
Key_user_id,
CP,
"Some thing here". getbytes ("UTF-8 "),
Timestamp,
Consistencylevel. One );
Slicerange sr = new slicerange (New byte [0], new byte [0], false, 10 );
Slicepredicate predicate = new slicepredicate ();
Predicate. setslice_range (SR );
Columnparent parent = new columnparent ();
Parent. setcolumn_family ("standardbyuuid1 ");
List <columnorsupercolumn> Results = client. get_slice ("keyspace1", key_user_id, parent, predicate, consistencylevel. One );
For (columnorsupercolumn result: Results)
{
Column column = result. column;
System. Out. println (touuid (column. Name). tostring () + "->" + new string (column. value, "UTF-8 "));
}
Tr. Close ();
System. Out. println ("done .");
}
Public static java. util. UUID gettimeuuid ()
{
Return java. util. UUID. fromstring (new COM. eaio. UUID. UUID (). tostring ());
}
Public static byte [] asbytearray (Java. util. 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;
}
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 ());
}
}
Use Cassandra-CLI to query the results:
Output result:
9a3212f0-7584-11df-9632-001fe210d7db-> Chris goffinet
Fa6685c0-7584-11df-8856-001fe210d7db-> some thing here
Done.