日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
漫談Cassandra客戶端的使用

數(shù)據(jù)庫頻道之前也曾有過《NoSOL:關(guān)系型數(shù)據(jù)庫終結(jié)者?》專題,希望大家能更深入的了解NoSQL。

站在用戶的角度思考問題,與客戶深入溝通,找到白塔網(wǎng)站設(shè)計(jì)與白塔網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:做網(wǎng)站、成都網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名與空間、網(wǎng)絡(luò)空間、企業(yè)郵箱。業(yè)務(wù)覆蓋白塔地區(qū)。

最近試用了一段時(shí)間Cassandra,將Oracle中的數(shù)據(jù)導(dǎo)入進(jìn)來,遇到了問題然后解決問題,收獲挺大。在這個(gè)過程中,除了設(shè)計(jì)一個(gè)合理的數(shù)據(jù)模型,再就是使用Cassandra API進(jìn)行交互了。

Cassandra在設(shè)計(jì)的時(shí)候,就是支持Thrift的,這意味著我們可以使用多種語言開發(fā)。

對(duì)于Cassandra的開發(fā)本身而言,這是使用Thrift的好處:支持多語言。壞處也是顯而易見的:Thrift API功能過于簡單,不具備在生產(chǎn)環(huán)境使用的條件。

在Cassandra Wiki頁面上,也有基于Thrift API開發(fā)的更加高級(jí)的API,各個(gè)語言都有,具體信息可以參考:http://wiki.apache.org/cassandra/ClientExamples。

這次只談?wù)勏旅鎯深怞ava的客戶端:

1 Thrift Java API

2 hector

Thrift Java API

這個(gè)是Cassandra自帶的最簡單的一類API,這個(gè)文件在apache-cassandra-0.5.1.jar中包含了。可以直接使用。我們也可以自己安裝一個(gè)Thrift,然后通過cassandra.thrift文件自動(dòng)生成。

如果你要使用Cassandra,那么我們必須要了解Thrift API,畢竟所有的其他更加高級(jí)的API都是基于這個(gè)來包裝的。

提供的功能

插入數(shù)據(jù)

插入數(shù)據(jù)需要指定keyspace,ColumnFamily, Column,Key,Value,timestamp和數(shù)據(jù)同步級(jí)別。(如何需要了Cassandra的解數(shù)據(jù)模型,可以參考《大話Cassandra數(shù)據(jù)模型》)

 
 
 
  1. /** * Insert a Column consisting of (column_path.column, value, timestamp)   
  2. at the given column_path.column_family and optional   
  3. * column_path.super_column. Note that column_path.column is here required,   
  4. since a SuperColumn cannot directly contain binary   
  5. * values -- it can only contain sub-Columns.   
  6. *    
  7. * @param keyspace   
  8. * @param key   
  9. * @param column_path   
  10. * @param value   
  11. * @param timestamp   
  12. * @param consistency_level   
  13. */public void insert(String keyspace, String key, ColumnPath column_path, byte[] value, long timestamp, int consistency_level) throws InvalidRequestException, UnavailableException, TimedOutException, TException;   
  14. /** * Insert Columns or SuperColumns across different Column Families for the same row key. batch_mutation is a   
  15. * map> -- a map which pairs column family names with the relevant ColumnOrSuperColumn   
  16. * objects to insert.   
  17. *    
  18. * @param keyspace   
  19. * @param key   
  20. * @param cfmap   
  21. * @param consistency_level   
  22. */public void batch_insert(String keyspace, String key, Map> cfmap, int consistency_level) throws InvalidRequestException, UnavailableException 

讀取數(shù)據(jù)

獲取一個(gè)查詢條件精確的值。

   
 
 
  1. /** * Get the Column or SuperColumn at the given column_path. If no value is present, NotFoundException is thrown. (This is   
  2. * the only method that can throw an exception under non-failure conditions.)   
  3. *  * @param keyspace   
  4. * @param key   
  5. * @param column_path   
  6. * @param consistency_level   
  7. */public ColumnOrSuperColumn get(String keyspace, String key, ColumnPath column_path,   
  8. int consistency_level) throws InvalidRequestException, NotFoundException, UnavailableException, TimedOutException, TException;   
  9. /** * Perform a get for column_path in parallel on the given list keys. The return value maps keys to the   
  10. * ColumnOrSuperColumn found. If no value corresponding to a key is present, the key will still be in the map, but both   
  11. * the column and super_column references of the ColumnOrSuperColumn object it maps to will be null.   
  12. *  * @param keyspace   
  13. * @param keys * @param column_path   
  14. * @param consistency_level   
  15. */public Map multiget(String keyspace, List keys,   
  16. ColumnPath column_path, int consistency_level) throws InvalidRequestException 

獲取某一個(gè)keyspace,Key,ColumnFamily,SuperColumn(如果有的話需要指定)下面的相關(guān)數(shù)據(jù):只查詢Column的name符合條件的相關(guān)數(shù)據(jù)(SlicePredicate)。

   
 
 
  1. /** * Get the group of columns contained by column_parent (either a ColumnFamily name or a ColumnFamily/SuperColumn name   
  2. * pair) specified by the given SlicePredicate. If no matching values are found, an empty list is returned.   
  3. *  * @param keyspace   
  4. * @param key   
  5. * @param column_parent   
  6. * @param predicate   
  7. * @param consistency_level   
  8. */public List get_slice(String keyspace, String key, ColumnParent column_parent, SlicePredicate predicate,   
  9. int consistency_level) throws InvalidRequestException, UnavailableException, TimedOutException, TException; /*  
  10. * * Performs a get_slice for column_parent and predicate for the given keys in parallel.   
  11. *    
  12. * @param keyspace   
  13. * @param keys   
  14. * @param column_parent   
  15. * @param predicate   
  16. * @param consistency_level   
  17. */public Map> multiget_slice(String keyspace, List keys, ColumnParent column_parent,   
  18. SlicePredicate predicate, int consistency_level) throws InvalidRequestException, UnavailableException, TimedOutException, TException; 

查詢Key的取值范圍(使用這個(gè)功能需要使用order-preserving partitioner)。

 
 
 
  1. /** * @deprecated; use get_range_slice instead   
  2. *    
  3. * @param keyspace   
  4. * @param column_family   
  5. * @param start   
  6. * @param finish   
  7. * @param count   
  8. * @param consistency_level   
  9. */public List get_key_range(String keyspace, String column_family,   
  10. String start, String finish, int count, int consistency_level) 
  11. throws InvalidRequestException, UnavailableException, TimedOutException, TException; /*  
  12. *   
  13. * returns a subset of columns for a range of keys.   
  14. *    
  15. * @param keyspace   
  16. * @param column_parent   
  17. * @param predicate   
  18. * @param start_key   
  19. * @param finish_key   
  20. * @param row_count   
  21. * @param consistency_level   
  22. */public List get_range_slice(String keyspace, ColumnParent column_parent,   
  23. SlicePredicate predicate, String start_key, String finish_key, int row_count 

查詢系統(tǒng)的信息。

   
 
 
  1. /**   
  2. * get property whose value is of type string.   
  3. *    
  4. * @param property   
  5. */public String get_string_property(String property) throws TException; /*  
  6. *   
  7. * get property whose value is list of strings.   
  8. *    
  9. * @param property */public List get_string_list_property(String property) throws TException; /*  
  10. *   
  11. * describe specified keyspace   
  12. *    
  13. * @param keyspace   
  14. */public Map> describe_keyspace(String keyspace)   
  15. throws NotFoundException, TException; 

通過這些操作,我們可以了解到系統(tǒng)的信息。

其中一個(gè)比較有意思的查詢信息是:token map,通過這個(gè)我們可以知道哪些Cassandra Service是可以提供服務(wù)的。

刪除數(shù)據(jù)

 
 
 
  1. /**   
  2. * Remove data from the row specified by key at the granularity specified by column_path,   
  3. and the given timestamp. Note   
  4. * that all the values in column_path besides column_path.column_family are truly optional: you can remove the entire   
  5. * row by just specifying the ColumnFamily, or you can remove a SuperColumn   
  6. or a single Column by specifying those levels too.   
  7. *    
  8. * @param keyspace   
  9. * @param key   
  10. * @param column_path   
  11. * @param timestamp   
  12. * @param consistency_level   
  13. */public void remove(String keyspace, String key, ColumnPath column_path,   
  14. long timestamp, int consistency_level) throws InvalidRequestException, UnavailableException 

這里需要注意的是,由于一致性的問題。這里的刪除操作不會(huì)立即刪除所有機(jī)器上的該數(shù)據(jù),但是最終會(huì)一致。

程序范例

 
 
 
  1. import java.util.List;  
  2. import java.io.UnsupportedEncodingException;   
  3. import org.apache.thrift.transport.TTransport;  
  4. import org.apache.thrift.transport.TSocket;  
  5. import org.apache.thrift.protocol.TProtocol;  
  6. import org.apache.thrift.protocol.TBinaryProtocol;  
  7. import org.apache.thrift.TException;  
  8. import org.apache.cassandra.service.*;   
  9. public class CClient{      
  10. public static void main(String[] args)      
  11. throws TException, InvalidRequestException,   
  12. UnavailableException, UnsupportedEncodingException, NotFoundException      
  13. {          
  14. TTransport tr = new TSocket("localhost", 9160);   
  15.  TProtocol proto = new TBinaryProtocol(tr);          
  16. Cassandra.Client client = new Cassandra.Client(proto);          
  17. tr.open();           
  18. String key_user_id = "逖靖寒的世界";          
  19.  // insert data          
  20. long timestamp = System.currentTimeMillis();       
  21.  client.insert("Keyspace1", key_user_id,                       
  22.  new ColumnPath("Standard1", null, "網(wǎng)址".getBytes("UTF-8")),                       
  23.  "http://gpcuster.cnblogs.com".getBytes("UTF-8"), timestamp,ConsistencyLevel.ONE);         
  24.  client.insert("Keyspace1", key_user_id,                        
  25. new ColumnPath("Standard1", null, "作者".getBytes("UTF-8")),                        
  26. "逖靖寒".getBytes("UTF-8"), timestamp, ConsistencyLevel.ONE);          
  27.  // read single column          
  28. ColumnPath path = new ColumnPath("Standard1", null, "name".getBytes("UTF-8"));     
  29.  System.out.println(client.get("Keyspace1", key_user_id, path, ConsistencyLevel.ONE));           
  30. // read entire row         
  31.  SlicePredicate predicate = new SlicePredicate(null, new SliceRange(new byte[0], new byte[0], false, 10));   
  32. ColumnParent parent = new ColumnParent("Standard1", null);         
  33.  List results = client.get_slice("Keyspace1", key_user_id, parent, predicate, ConsistencyLevel.ONE);         
  34.  for (ColumnOrSuperColumn result : results)        {              
  35. Column column = result.column;              
  36. System.out.println(new String(column.name, "UTF-8") + " -> " + new String(column.value, "UTF-8"));         
  37.  }          
  38.  tr.close();     
  39.  }} 

優(yōu)點(diǎn)與缺點(diǎn)

優(yōu)點(diǎn):簡單高效

缺點(diǎn):功能簡單,無法提供連接池,錯(cuò)誤處理等功能,不適合直接在生產(chǎn)環(huán)境使用。

Hector

Hector是基于Thrift Java API包裝的一個(gè)Java客戶端,提供一個(gè)更加高級(jí)的一個(gè)抽象。

程序范例

 
 
 
  1. package me.prettyprint.cassandra.service;   
  2. import static me.prettyprint.cassandra.utils.StringUtils.bytes;  
  3. import static me.prettyprint.cassandra.utils.StringUtils.string;   
  4. import org.apache.cassandra.service.Column;  
  5. import org.apache.cassandra.service.ColumnPath;   
  6. public class ExampleClient {     
  7. public static void main(String[] args) throws IllegalStateException, PoolExhaustedException,        
  8. Exception {      
  9. CassandraClientPool pool = CassandraClientPoolFactory.INSTANCE.get();    CassandraClient client = pool.borrowClient("localhost", 9160);      
  10. // A load balanced version would look like this:      
  11. // CassandraClient client = pool.borrowClient(new String[] {"cas1:9160", "cas2:9160", "cas3:9160"});       
  12. try {        
  13. Keyspace keyspace = client.getKeyspace("Keyspace1");        
  14. ColumnPath columnPath = new ColumnPath("Standard1", null, bytes("網(wǎng)址"));         
  15. // insert        
  16. keyspace.insert("逖靖寒的世界", columnPath, bytes("http://gpcuster.cnblogs.com"));         
  17. // read        
  18. Column col = keyspace.getColumn("逖靖寒的世界", columnPath);       System.out.println("Read from cassandra: " + string(col.getValue()));       
  19. } finally {       
  20.  // return client to pool. do it in a finally block to make sure it's executed        
  21. pool.releaseClient(client);     
  22.  }  }} 

優(yōu)點(diǎn)

1 提供連接池。

2 提供錯(cuò)誤處理:當(dāng)操作失敗的時(shí)候,Hector會(huì)根據(jù)系統(tǒng)信息(token map)自動(dòng)連接另一個(gè)Cassandra Service。

3 編程接口容易使用。

4 支持JMX。

缺點(diǎn)

1 不支持多線程的環(huán)境。

2 keyspace封裝過多(數(shù)據(jù)校驗(yàn)和數(shù)據(jù)重新封裝),如果進(jìn)行大量的數(shù)據(jù)操作,這里的消耗需要考慮。

3 錯(cuò)誤處理不夠人性化:如果所有的Cassandra Service都非常繁忙,那么經(jīng)過多次操作失敗后,最終的結(jié)果失敗。

總結(jié)

Hector已經(jīng)是一個(gè)基本足夠使用的Java客戶端了,但是還是缺乏一些相關(guān)的功能,比如:

1 線程安全。

2 支持自動(dòng)的多線程查詢和插入,提高操作效率。

3 人性化的錯(cuò)誤處理機(jī)制。

4 避免過多的封裝。


網(wǎng)頁題目:漫談Cassandra客戶端的使用
網(wǎng)站鏈接:http://m.5511xx.com/article/cdoddji.html