惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 叶小钗
Last Week in AI
Last Week in AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
V2EX
J
Java Code Geeks
The GitHub Blog
The GitHub Blog
博客园_首页
U
Unit 42
人人都是产品经理
人人都是产品经理
Engineering at Meta
Engineering at Meta
IT之家
IT之家
G
Google Developers Blog
L
LangChain Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
爱范儿
爱范儿
罗磊的独立博客
博客园 - 三生石上(FineUI控件)
Martin Fowler
Martin Fowler
Jina AI
Jina AI
有赞技术团队
有赞技术团队
Apple Machine Learning Research
Apple Machine Learning Research
Vercel News
Vercel News
小众软件
小众软件
H
Help Net Security

博客园 - Liren

查找目录中同名的文件或者文件夹 获取设置一个字节某一个位的数值 基于JMS的数据交换既数据互操作平台的解决方案 Spring MVC基于注解的Junit测试 Spring MVC 3中关于url-pattern设成"/"后,资源访问问题 博文阅读密码验证 - 博客园 NotificationManager - Liren - 博客园 发送短信 调用系统联系人列表 MorseCodeConverter 自用留存 - Liren - 博客园 Android 给自己的类加个事件 用代码旋转屏幕 - Liren - 博客园 Android SQLiteHelper Cassandra API60 Java 代码示例 转:在 CLI 中練習 Data Model asp.net 实现一个简单CAS Server - Liren Ajax跨域访问代理类,支持GET和POST方法 Python 学习笔记:需要仔细阅读一个函数 Python 学习笔记: 备份工具
Cassandra Java 使用TimeUUIDType
Liren · 2010-06-11 · via 博客园 - Liren

参考地址 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], false10);
        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.