关于cassandra0.7.5的操作

时间:2021-10-02 04:50:28
 


import java.nio.ByteBuffer;

import org.apache.cassandra.thrift.Cassandra;
import org.apache.cassandra.thrift.Column;   
import org.apache.cassandra.thrift.ColumnPath;   
import org.apache.cassandra.thrift.ConsistencyLevel;   
import org.apache.cassandra.thrift.InvalidRequestException;   
import org.apache.cassandra.thrift.NotFoundException;   
import org.apache.cassandra.thrift.TimedOutException;   
import org.apache.cassandra.thrift.UnavailableException;   
import org.apache.thrift.TException;   
import org.apache.thrift.protocol.TBinaryProtocol;   
import org.apache.thrift.transport.TSocket;   
import org.apache.thrift.transport.TTransport;   
import org.apache.thrift.transport.TTransportException;   
  
  
 public class SampleOne {     
     static Cassandra.Client cassandraClient;     
     static TTransport socket;   
      
      
     private static void init() throws TTransportException {     
//         String server = "192.168.1.129";     
       String server = "localhost";   
         int port = 9160;     
      
         /* 首先指定cassandra server的地址 */     
         socket = new TSocket(server, port);     
         System.out.println(" connected to " + server + ":" + port + ".");     
      
      
         /* 指定通信协议为二进制流协议 */     
         TBinaryProtocol binaryProtocol = new TBinaryProtocol(socket, false, false);     
         cassandraClient = new Cassandra.Client(binaryProtocol);     
      
      
         /* 建立通信连接 */     
         socket.open();     
     }     
      
      
     public static void main(String[] args) throws Exception {     
         /* 初始化连接 */     
         init();     
      
      
         /* 选择需要操作的Keyspaces, 可以理解成数据库的表 */     
         String keyspace= "test";     
         String row = "hh";     
      
         /* 创建一个Table Name */     
         String tableName = "table1";   
            
         /* 插入一条记录 */  
//         insertOrUpdate(keyspace,tableName,row,"name","happy birthday!",System.currentTimeMillis());   
         /* 删除一条记录 */  
         //delete(keyspace,tableName,row,"name",System.currentTimeMillis());   
         /* 获取一条记录 (由于插入和删除是同一条记录,有可能会检索不到哦!请大家主意!*/  
         Column column = getByColumn(keyspace,tableName,row,"name", System.currentTimeMillis());   
         System.out.println("read row " + row);     
         System.out.println("column name " + ":" +column.name);     
         System.out.println("column value" + ":" + column.value);     
         System.out.println("column timestamp" + ":" + (column.timestamp));     
            
         close();   
     }   
        
     /**  
      * 获取数据  
      */  
     public static Column getByColumn(String tableSpace,String tableName, String rowParam,String ColumnName,long timeStamp)    
    throws TException, TimedOutException, InvalidRequestException, UnavailableException, NotFoundException{   
      /* 选择需要操作的Keyspaces, 存放数据表所在的空间位置 */     
      String keyspace= tableSpace;    
      /* 数据所在的行标 */  
      String row = rowParam;     
  
      /* 创建一个column path */     
      ColumnPath col = new ColumnPath(tableName);     
      col.setColumn(ColumnName.getBytes());    
         
      /* 执行查询操作,指定keysapce, row, col, timestamp   
       * timestamp是用来做数据一致性保证的, 而consistency_level是用来控制数据分布的策略,前者的理论依据是bigtable, 后者的理论依据是dynamo   
       */      
      return          cassandraClient.get(ByteBuffer.wrap(col.getColumn()),col, ConsistencyLevel.ONE).getColumn();
     }   
        
        
     /**  
      * 关闭当前的远程访问连接  
      */  
     public static void close() {   
         socket.close();   
    }   
 }    


出现异常

Exception in thread "Main Thread" org.apache.thrift.transport.TTransportException
at org.apache.thrift.transport.TIOStreamTransport.read(TIOStreamTransport.java:132)
at org.apache.thrift.transport.TTransport.readAll(TTransport.java:84)
at org.apache.thrift.protocol.TBinaryProtocol.readAll(TBinaryProtocol.java:378)
at org.apache.thrift.protocol.TBinaryProtocol.readI32(TBinaryProtocol.java:297)
at org.apache.thrift.protocol.TBinaryProtocol.readMessageBegin(TBinaryProtocol.java:204)
at org.apache.cassandra.thrift.Cassandra$Client.recv_get(Cassandra.java:479)
at org.apache.cassandra.thrift.Cassandra$Client.get(Cassandra.java:462)
at SampleOne.getByColumn(SampleOne.java:129)
at SampleOne.main(SampleOne.java:63)

3 个解决方案

#1


引用楼主 zhouyusunquan 的回复:
import java.nio.ByteBuffer;

import org.apache.cassandra.thrift.Cassandra;
import org.apache.cassandra.thrift.Column;  
import org.apache.cassandra.thrift.ColumnPath;  
import org.apache.cassandr……

数据是
[default@test] list table1;
Using default limit of 100
-------------------
RowKey: hwp
=> (column=age, value=11, timestamp=1304582363906000)
=> (column=name, value=huangweipeng, timestamp=1304582538003000)
-------------------
RowKey: hh
=> (column=age, value=25, timestamp=1304582143086000)
=> (column=name, value=huanghua, timestamp=1304582719977000)
-------------------
RowKey: zq
=> (column=age, value=25, timestamp=1304582088595000)
=> (column=name, value=zhouquan, timestamp=1304582750678000)

3 Rows Returned.

#2


这个不会啊,呵呵

#3


楼主,请问你的问题解决了吗,我现在也遇到这个问题,求解决~

#1


引用楼主 zhouyusunquan 的回复:
import java.nio.ByteBuffer;

import org.apache.cassandra.thrift.Cassandra;
import org.apache.cassandra.thrift.Column;  
import org.apache.cassandra.thrift.ColumnPath;  
import org.apache.cassandr……

数据是
[default@test] list table1;
Using default limit of 100
-------------------
RowKey: hwp
=> (column=age, value=11, timestamp=1304582363906000)
=> (column=name, value=huangweipeng, timestamp=1304582538003000)
-------------------
RowKey: hh
=> (column=age, value=25, timestamp=1304582143086000)
=> (column=name, value=huanghua, timestamp=1304582719977000)
-------------------
RowKey: zq
=> (column=age, value=25, timestamp=1304582088595000)
=> (column=name, value=zhouquan, timestamp=1304582750678000)

3 Rows Returned.

#2


这个不会啊,呵呵

#3


楼主,请问你的问题解决了吗,我现在也遇到这个问题,求解决~