HBase學習(十四)LINUX下用Eclipse構建HBase開發環境

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   io   os   使用   ar   

Eclipse,HBase版本目前沒有發現需要特別指定


1:從HBase叢集中複製一份Hbase部署檔案,放置在開發端某一目錄下(如在/app/hadoop/hbase096目錄下)。

2:在eclipse裡建立一個java項目HBase,然後選擇項目屬性,在Libraries->Add External JARs...,然後選擇/app/hadoop/hbase096/lib下相關的JAR包,如果只是測試用的話,就簡單一點,將所有的JAR選上。
 




 





3:在項目HBase下增加一個檔案夾conf,將Hbase叢集的設定檔hbase-site.xml複製到該目錄,然後選擇項目屬性在Libraries->Add Class Folder,將剛剛增加的conf目錄選上。


 




 



  

 




4:在HBase項目中增加一個chapter12的package,然後增加一個HBaseTestCase的class,然後將陸嘉恒老師的《Hadoop實戰第2版》12章的代碼複製進去,做適當的修改,代碼如下:

package chapter12;import java.io.IOException; import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.HBaseConfiguration;import org.apache.hadoop.hbase.HColumnDescriptor;import org.apache.hadoop.hbase.HTableDescriptor;import org.apache.hadoop.hbase.client.Get;import org.apache.hadoop.hbase.client.HBaseAdmin;import org.apache.hadoop.hbase.client.HTable;import org.apache.hadoop.hbase.client.Put;import org.apache.hadoop.hbase.client.Result;import org.apache.hadoop.hbase.client.ResultScanner;import org.apache.hadoop.hbase.client.Scan;import org.apache.hadoop.hbase.util.Bytes;public class HBaseTestCase {               //聲明靜態配置 HBaseConfiguration    static Configuration cfg=HBaseConfiguration.create();    //建立一張表,通過HBaseAdmin HTableDescriptor來建立    public static void creat(String tablename,String columnFamily) throws Exception {        HBaseAdmin admin = new HBaseAdmin(cfg);        if (admin.tableExists(tablename)) {            System.out.println("table Exists!");            System.exit(0);        }        else{            HTableDescriptor tableDesc = new HTableDescriptor(tablename);            tableDesc.addFamily(new HColumnDescriptor(columnFamily));            admin.createTable(tableDesc);            System.out.println("create table success!");        }    }      //添加一條資料,通過HTable Put為已經存在的表來添加資料    public static void put(String tablename,String row, String columnFamily,String column,String data) throws Exception {        HTable table = new HTable(cfg, tablename);        Put p1=new Put(Bytes.toBytes(row));        p1.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(data));        table.put(p1);        System.out.println("put '"+row+"','"+columnFamily+":"+column+"','"+data+"'");    }      public static void get(String tablename,String row) throws IOException{            HTable table=new HTable(cfg,tablename);            Get g=new Get(Bytes.toBytes(row));                Result result=table.get(g);                System.out.println("Get: "+result);    }    //顯示所有資料,通過HTable Scan來擷取已有表的資訊    public static void scan(String tablename) throws Exception{         HTable table = new HTable(cfg, tablename);         Scan s = new Scan();         ResultScanner rs = table.getScanner(s);         for(Result r:rs){             System.out.println("Scan: "+r);         }    }        public static boolean delete(String tablename) throws IOException{                        HBaseAdmin admin=new HBaseAdmin(cfg);            if(admin.tableExists(tablename)){                    try                    {                            admin.disableTable(tablename);                            admin.deleteTable(tablename);                    }catch(Exception ex){                            ex.printStackTrace();                            return false;                    }                                }            return true;    }      public static void  main (String [] agrs) {            String tablename="hbase_tb";        String columnFamily="cf";                      try {                                 HBaseTestCase.creat(tablename, columnFamily);            HBaseTestCase.put(tablename, "row1", columnFamily, "cl1", "data");            HBaseTestCase.get(tablename, "row1");            HBaseTestCase.scan(tablename); /*           if(true==HBaseTestCase.delete(tablename))                    System.out.println("Delete table:"+tablename+"success!"); */                   }        catch (Exception e) {            e.printStackTrace();        }    }}

5:設定回合組態,然後運行。運行前將Hbase叢集先啟動。


 




6:檢驗,使用hbase shell查看hbase,發現已經建立表hbase_tb。




期間自己遇到點問題:

1.引入jar包,有兩個包沒有讀取許可權,導致項目出現紅色驚嘆號

2.因為我是虛擬機器,每次都要重新啟動hadoop和HBase,所以也不要忘了設定hadoop為非安全模式


HBase學習(十四)LINUX下用Eclipse構建HBase開發環境

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.