找回密碼 或 安全提問
 註冊
|註冊|登錄

伊莉討論區

搜索
感激所有對伊莉作出奉獻的人尊貴會員無限使用任何功能認識好友、聊天,分享生活趣事
波多野結進擊的巨3d世紀帝國高中
unholy j怪物們的藥師兄妹藥師3262563聖騎士之無職轉生

休閒聊天興趣交流學術文化旅遊交流飲食交流家庭事務PC GAMETV GAME
熱門線上其他線上感情感性寵物交流家族門派動漫交流貼圖分享BL/GL
音樂世界影視娛樂女性頻道潮流資訊BT下載區GB下載區下載分享短片
電腦資訊數碼產品手機交流交易廣場網站事務長篇小說體育運動時事經濟
上班一族博彩娛樂

[繁]我的英雄學院 第

[繁]怪人的沙拉碗03-

捷運淡水線上 老人自

✡ 完美世界・155・20

[繁中]霹靂天機貳 仙

(4月新番)[簡]怪異與
C & C++ 語言C# 語言Visual Basic 語言PHP 語言JAVA 語言
查看: 3570|回復: 1
打印上一主題下一主題

[分享]通過程式對Hbase進行管理()[複製鏈接]

Invi6666 該用戶已被刪除
跳轉到指定樓層
樓主
發表於 2012-11-16 02:32 PM|只看該作者|倒序瀏覽
本帖最後由 Invi6666 於 2012-11-16 02:34 PM 編輯

在這比較少看到Hbase的程式碼,剛好最近寫好了一個Hbase的程式在此分享給大家,希望可以幫到一些人。

雖然是一個蠻普通的程式碼,就純粹透過程式對Hbase新增資料表,新增資料等等的功能
  1. import java.io.IOException;  
  2. import java.util.ArrayList;  
  3. import java.util.List;
  4. import java.util.Scanner;

  5. import org.apache.hadoop.conf.Configuration;  
  6. import org.apache.hadoop.hbase.HBaseConfiguration;  
  7. import org.apache.hadoop.hbase.HColumnDescriptor;  
  8. import org.apache.hadoop.hbase.HTableDescriptor;  
  9. import org.apache.hadoop.hbase.KeyValue;  
  10. import org.apache.hadoop.hbase.MasterNotRunningException;  
  11. import org.apache.hadoop.hbase.ZooKeeperConnectionException;  
  12. import org.apache.hadoop.hbase.client.Delete;  
  13. import org.apache.hadoop.hbase.client.Get;  
  14. import org.apache.hadoop.hbase.client.HBaseAdmin;  
  15. import org.apache.hadoop.hbase.client.HTable;  
  16. import org.apache.hadoop.hbase.client.HTablePool;  
  17. import org.apache.hadoop.hbase.client.Put;  
  18. import org.apache.hadoop.hbase.client.Result;  
  19. import org.apache.hadoop.hbase.client.ResultScanner;  
  20. import org.apache.hadoop.hbase.client.Scan;  
  21. import org.apache.hadoop.hbase.filter.Filter;  
  22. import org.apache.hadoop.hbase.filter.FilterList;  
  23. import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;  
  24. import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;  
  25. import org.apache.hadoop.hbase.util.Bytes;  


  26. public class test { //start class
  27.         
  28.     public static Configuration conf;  
  29.     static {  
  30.              conf = HBaseConfiguration.create();
  31.     }  
  32.         
  33.     /**
  34.      * 查詢所有數據
  35.      * @param tableName
  36.      */
  37.    
  38.         public static void createTable(String tableName, int column)
  39.         {   
  40.               
  41.                     Scanner scanner = new Scanner(System.in);
  42.                     System.out.println("Start create table ......");  
  43.                     
  44.                      try
  45.                      {
  46.                              HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);  
  47.                      if (hBaseAdmin.tableExists(tableName))  // 如果要創建的表已經存在,將會提示資料表已存在
  48.                      {  
  49.                         
  50.                          System.out.println(tableName + " is exist");
  51.                          return ;
  52.                          }
  53.                      else
  54.                      {
  55.                              HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);  
  56.                         
  57.                          for(int i=0 ; i < column ; i++ )
  58.                          {
  59.                                  System.out.println(i+1 + ") Insert the columnName:");
  60.                                  String columnName= scanner.next();
  61.                                  tableDescriptor.addFamily(new HColumnDescriptor(columnName));  
  62.                          }  
  63.                      
  64.                          hBaseAdmin.createTable(tableDescriptor);  
  65.                      }      
  66.                      
  67.                      }
  68.                      catch (MasterNotRunningException e){
  69.                              e.printStackTrace();  
  70.                      }
  71.                      catch (ZooKeeperConnectionException e) {  
  72.                      e.printStackTrace();  
  73.                  }
  74.                      catch (IOException e) {  
  75.                      e.printStackTrace();  
  76.                  }  
  77.                  System.out.println("End create table ......");  
  78.          
  79.                     
  80.             }
  81.             
  82.          /**
  83.      * 插入數據
  84.      * @param tableName
  85.      */
  86.          public static void insertData(String tableName)
  87.          {
  88.                   
  89.                    Scanner scanner = new Scanner(System.in);
  90.                    System.out.println("start insert data ......");   
  91.                   
  92.                     try
  93.                     {
  94.                               HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);  
  95.                               if (!hBaseAdmin.tableExists(tableName))  // 如果存在要創建的表,
  96.                            {  
  97.                                 
  98.                                 System.out.println(tableName + " no exist");
  99.                                 return ;
  100.                                  }
  101.                               else
  102.                                 {
  103.                                       
  104.                                          HTable table = new HTable(conf,tableName);
  105.                                     
  106.                                        
  107.                                         System.out.println("How many number of row you want to insert?");
  108.                                            int row =scanner.nextInt();
  109.                                        
  110.                            
  111.                                    for(int i=0;i<row;i++)
  112.                                    {
  113.                                            System.out.println(i+1 + ") Insert Row Name:");
  114.                                         String rowName= scanner.next();
  115.                                           
  116.                                         Put put = new Put(rowName.getBytes());
  117.                                           
  118.                                         System.out.println("How many number of column you want to insert?");
  119.                                         int column =scanner.nextInt();
  120.                                    
  121.                                         for(int j=0; j<column; j++)
  122.                                     {
  123.                                           System.out.println(j+1 + ") Insert Column Name:");
  124.                                           String columnName= scanner.next();
  125.                                           System.out.println(j+1 + ") Insert Key value");
  126.                                           String keyValue= scanner.next();
  127.                                           put.add(columnName.getBytes(), null, keyValue.getBytes());
  128.                                           
  129.                                     }
  130.                                         table.put(put);
  131.                                        
  132.                                    }
  133.                                 }         
  134.                     }
  135.                     catch(ZooKeeperConnectionException e)
  136.                     {
  137.                             e.printStackTrace();
  138.                     }
  139.                     catch(IOException e)
  140.                     {
  141.                             e.printStackTrace();
  142.                     }
  143.                   
  144.                 System.out.println("end insert data ......");  
  145.          }
  146.   
  147.            /**
  148.            * 查詢所有數據
  149.            * @param tableName
  150.            */  
  151.             public static void QueryAll(String tableName) {  
  152.                     
  153.                     System.out.println("start query all data ......");
  154.                     
  155.                     try {
  156.                             HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);  
  157.                            if (!hBaseAdmin.tableExists(tableName))  // 如果資料表不存在,將會提示錯誤信息
  158.                             {  
  159.                                 
  160.                                 System.out.println(tableName + " no exist");
  161.                                 return ;
  162.                                    }
  163.                            else
  164.                            {
  165.                                  HTable table = new HTable(conf,tableName);
  166.                              ResultScanner rs = table.getScanner(new Scan());  
  167.                              for (Result r : rs)
  168.                              {  
  169.                                 System.out.println("獲得到rowkey:" + new String(r.getRow()));  
  170.                                 for (KeyValue keyValue : r.raw())
  171.                                 {  
  172.                                     System.out.println("列:" + new String(keyValue.getFamily()) + "====值:" + new String(keyValue.getValue()));  
  173.                                 }  
  174.                             }   
  175.                          }
  176.                         
  177.                 } catch (IOException e) {  
  178.                     e.printStackTrace();  
  179.                 }  
  180.                     
  181.                     System.out.println("end query all data ......");
  182.             }  
  183.             
  184.             
  185.             /**
  186.              * 根據 rowkey刪除一條記錄
  187.              * @param tablename
  188.              * @param rowkey
  189.              */  
  190.             
  191.             public static void deleteRow(String tablename, String rowkey)  {  
  192.                     
  193.                     System.out.println("start delete row ......");
  194.                     
  195.                     try {  
  196.                     HTable table = new HTable(conf, tablename);  
  197.                     List list = new ArrayList();  
  198.                     Delete d1 = new Delete(rowkey.getBytes());  
  199.                     list.add(d1);  
  200.                      
  201.                     table.delete(list);  
  202.                     System.out.println(rowkey + "delete successful");  
  203.                      
  204.                 } catch (IOException e) {  
  205.                     e.printStackTrace();  
  206.                 }  
  207.                   
  208.                     System.out.println("end delete row ......");
  209.             }  
  210.             
  211.             
  212.          
  213.             /**
  214.              * 刪除一張表
  215.              * @param tableName
  216.              */  
  217.          public static void dropTable(String tableName)
  218.      {  
  219.             System.out.println("Start delete table ......");  
  220.             System.out.println(tableName);
  221.             try
  222.         {  
  223.      
  224.             HBaseAdmin admin = new HBaseAdmin(conf);  
  225.             admin.disableTable(tableName);   
  226.             admin.deleteTable(tableName);  
  227.         }
  228.             catch (MasterNotRunningException e) {  
  229.             e.printStackTrace();  
  230.         } catch (ZooKeeperConnectionException e) {  
  231.             e.printStackTrace();  
  232.         } catch (IOException e) {  
  233.             e.printStackTrace();  
  234.         }  
  235.             
  236.              System.out.println("End delete table ......");  
  237.   
  238.     }  
  239.    
  240.    
  241.    
  242.     public static void main(String[] args) { //start main
  243.       
  244.       final int CREATETABLE=1;
  245.       final int INSERTDATA=2;
  246.       final int DELETETABLE=3;
  247.       final int QUERYALL=4;
  248.       final int DELETEROW=5;
  249.       final int EXIT=6;
  250.       
  251.       
  252.       int chosen=0;
  253.              String tableName="";
  254.              int column=0;
  255.                
  256.              while(chosen!=EXIT)
  257.                {
  258.                        System.out.println("Choose the operation:");
  259.                        System.out.println("1) Create Table");
  260.                        System.out.println("2) Insert Data");
  261.                        System.out.println("3) Delete Table");
  262.                        System.out.println("4) QueryALL");
  263.                        System.out.println("5) Delete row");
  264.                        System.out.println("4) Exit");
  265.                        
  266.                        Scanner scanner = new Scanner(System.in);
  267.                            chosen=scanner.nextInt();
  268.                        
  269.                        switch(chosen)
  270.                        {
  271.                           case CREATETABLE:
  272.                                    System.out.println("Insert the Table Name");
  273.                                tableName=scanner.next();
  274.                                    System.out.println("How many number of familyName you want to create?");
  275.                                    column =scanner.nextInt();
  276.                                    createTable(tableName,column);
  277.                                    break;
  278.                            case INSERTDATA:
  279.                                System.out.println("Insert the Table Name");
  280.                             tableName=scanner.next();
  281.                                    insertData(tableName);
  282.                                    break;
  283.                            case DELETETABLE:
  284.                                    System.out.println("Insert the Table Name");
  285.                                tableName=scanner.next();
  286.                                    dropTable(tableName);
  287.                                    break;
  288.                            case QUERYALL:
  289.                                 System.out.println("Insert the Table Name");
  290.                             tableName=scanner.next();
  291.                             QueryAll(tableName);
  292.                                 break;
  293.                            case DELETEROW:
  294.                                 System.out.println("Insert the Table Name");
  295.                             tableName=scanner.next();
  296.                             System.out.println("Insert the Row key that you wan delete");
  297.                             String rowKey=scanner.next();
  298.                             deleteRow(tableName,rowKey);
  299.                                 break;   
  300.                           case EXIT:
  301.                                    System.out.println("Exit Program");
  302.                                 break;
  303.                                 default:
  304.                                        System.out.println("Please Insert the number of operation provide");

  305.                        }
  306.                }


  307.         } // end main

  308. }// end class
複製代碼
...
瀏覽完整內容,請先 註冊登入會員
分享分享0收藏收藏0支持支持0
若對尊貴或贊助會員有任何疑問,歡迎向我們查詢。我們的即時通或MSN: admin@eyny.com

使用道具檢舉

Rank: 1

帖子
300
積分
193 點
潛水值
11150 米
頭香
發表於 2015-7-29 04:24 PM|只看該作者
回覆中加入附件並不會使你增加積分,請使用主題方式發佈附件。
最近在自學安裝hadoop生態架構,但hbase架好後雖可以正常啟動,但我的slave1一直掛掉(regionserver失效)請問是什麼問題呢?(架構:Master,slave1,slave2,slave3;hadoop2.6.0,hbase1.0.1.1)

點評

snowflying 不要去回 2012 的帖子,另開一帖呀!  發表於 2015-7-29 05:37 PM

使用道具檢舉

您需要登錄後才可以回帖 登錄 | 註冊

Powered by Discuz!

© Comsenz Inc.

重要聲明:本討論區是以即時上載留言的方式運作,對所有留言的真實性、完整性及立場等,不負任何法律責任。而一切留言之言論只代表留言者個人意見,並非本網站之立場,用戶不應信賴內容,並應自行判斷內容之真實性。於有關情形下,用戶應尋求專業意見(如涉及醫療、法律或投資等問題)。 由於本討論區受到「即時上載留言」運作方式所規限,故不能完全監察所有留言,若讀者發現有留言出現問題,請聯絡我們。有權刪除任何留言及拒絕任何人士上載留言,同時亦有不刪除留言的權利。切勿上傳和撰寫 侵犯版權(未經授權)、粗言穢語、誹謗、渲染色情暴力或人身攻擊的言論,敬請自律。本網站保留一切法律權利。
回頂部