Cassandra Java 使用TimeUUIDType

來源:互聯網
上載者:User

參考地址 http://wiki.apache.org/cassandra/FAQ#working_with_timeuuid_in_java

下載一個包 http://johannburkard.de/software/uuid/

 

程式碼範例:

代碼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 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());
    }
}

 

 

 使用cassandra-cli查詢結果:

 

輸出結果:

9a3212f0-7584-11df-9632-001fe210d7db -> Chris Goffinet

fa6685c0-7584-11df-8856-001fe210d7db -> Some thing here 

done. 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.