小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

簡單使用hbase

 石頭狗 2009-03-15

簡單使用hbase

關(guān)鍵字: hbase

我們來簡單了解如何操作hbase,hbase與我們常用的數(shù)據(jù)庫最大的差別就是列存儲和無數(shù)據(jù)類型,所有數(shù)據(jù)都以string類型存儲,再有如果hbase table里有5個字段,但實際只有4個字段有值,那么為null的那個字段是不占用空間的,這點比較好,可以跟我們常用的數(shù)據(jù)庫比較下
首先還是創(chuàng)建一個表,暫不使用mapreduce:

Java代碼 復(fù)制代碼
  1. /**  
  2.  * 定義幾個常量  
  3.  */  
  4. public static HBaseConfiguration conf = new HBaseConfiguration();   
  5. static HTable table = null;   
  6. /**  
  7.  * 創(chuàng)建hbase table  
  8.  * @param table  
  9.  * @throws IOException  
  10.  */  
  11. public static void creatTable(String tablename) throws IOException {   
  12.     HBaseAdmin admin = new HBaseAdmin(conf);   
  13.     if (!admin.tableExists(new Text(tablename))) {   
  14.         HTableDescriptor tableDesc = new HTableDescriptor(tablename);   
  15.         tableDesc.addFamily(new HColumnDescriptor("ip:"));   
  16.         tableDesc.addFamily(new HColumnDescriptor("time:"));   
  17.         tableDesc.addFamily(new HColumnDescriptor("type:"));   
  18.         tableDesc.addFamily(new HColumnDescriptor("cookie:"));   
  19.         //注意這個C列,下面我會簡單以此列來說明列存儲   
  20.         tableDesc.addFamily(new HColumnDescriptor("c:"));   
  21.         admin.createTable(tableDesc);   
  22.         System.out.println("table create ok!!!");   
  23.     } else {   
  24.         System.out.println("table Already exists");   
  25.     }   
  26. }  

 將hadoop/hbase都啟動再運行,在hql中使用"desc tablename"就可以看出這個表有5個字段,下面我們再來往這個表里錄入點數(shù)據(jù),上面說過值為空的字段是不占用空間的,這里還要注意點,經(jīng)過我的測試,如果發(fā)現(xiàn)這個字段無值,就不要往hbase里面寫null值,當(dāng)然你要真往這個字段存null值是不會有任何問題的,但你查詢這個有null值的字段時,會有點毛病,當(dāng)然這個我也不知道怎么描述,有興趣的可以試試看,所以我下面有判斷,再者,hbase table里面的每一行數(shù)據(jù)集都必須有一個唯一row關(guān)鍵字,這個row你可以隨便定義,方便準(zhǔn)確找到你需要的數(shù)據(jù)

Java代碼 復(fù)制代碼
  1. /**  
  2.  * 錄入數(shù)據(jù)  
  3.  * @throws Exception  
  4.  */  
  5. public static void insertData() throws Exception{   
  6.     //讀取日志文件   
  7.     BufferedReader reader = new BufferedReader(new FileReader("log file name"));   
  8.     if(table==null)   
  9.         table = new HTable(conf, new Text(tablename));   
  10.     String line;   
  11.     while((line = reader.readLine()) != null){   
  12.         //這里我就不說了,先前有說明   
  13.         LogAccess log = new LogAccess(line);   
  14.         //這里我使用time+cookie為row關(guān)鍵字,確保不重復(fù),如果cookie記錄有重復(fù),將區(qū)別對待,這里暫不多做說明   
  15.         String row = createRow(log.getTime(),log.getCookie());   
  16.            
  17.         long lockid = table.startUpdate(new Text(row));   
  18.         if(!log.getIp().equals("") && log.getIp()!=null)   
  19.             table.put(lockid, new Text("ip:"), log.getIp().getBytes());   
  20.         if(!log.getTime().equals("") && log.getTime()!=null)   
  21.             table.put(lockid, new Text("time:"), log.getTime().getBytes());   
  22.         if(!log.getType().equals("") && log.getType()!=null)   
  23.             table.put(lockid, new Text("type:"), log.getType().getBytes());   
  24.         if(!log.getCookie().equals("") && log.getCookie()!=null)   
  25.             table.put(lockid, new Text("cookie:"), log.getCookie().getBytes());   
  26.         //這里要注意,我是往c列中寫入了5個字段,你可以想象,我在c列中存入了一個map   
  27.         if(!log.getRegmark().equals("") && log.getRegmark()!=null)   
  28.             table.put(lockid, new Text("c:_regmark"), log.getRegmark().getBytes());   
  29.         if(!log.getRegmark2().equals("") && log.getRegmark2()!=null)   
  30.             table.put(lockid, new Text("c:_regmark2"), log.getRegmark2().getBytes());   
  31.         if(!log.getSendshow().equals("") && log.getSendshow()!=null)   
  32.             table.put(lockid, new Text("c:_sendshow"), log.getSendshow().getBytes());   
  33.         if(!log.getCurrenturl().equals("") && log.getCurrenturl()!=null)   
  34.             table.put(lockid, new Text("c:_currenturl"), log.getCurrenturl().getBytes());   
  35.         if(!log.getAgent().equals("") && log.getAgent()!=null)   
  36.             table.put(lockid, new Text("c:_agent"), log.getAgent().getBytes());   
  37.         //存入數(shù)據(jù)   
  38.         table.commit(lockid);   
  39.     }   
  40. }  

 

O了,測試下吧

評論
blank 2008-10-16   回復(fù)
http://solomons./blog/205736
上面這篇文章詳細(xì)講解了hadoop/hbase的安裝以及啟動方式,有興趣看看吧
xianglei 2008-10-15   回復(fù)
hbase怎么啟動?

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多