1. 程式人生 > >eclipse構建HBase開發環境並執行例項對Hbase建表增刪改查

eclipse構建HBase開發環境並執行例項對Hbase建表增刪改查

首先確定hbase與Hdoop版本一致,網上都能查,我的是hadoop2.6.0+hbase1.0.0安裝配置好後,明確了在終端命令列下面可以對資料庫進行操作

然後對eclipse開發環境進行配置:

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

2.在eclipse裡新建一個java專案qin,然後選擇專案屬性,右鍵build path——>configure build path,在Libraries->Add External JARs...,然後選擇/home/hadoop/hbase/lib下相關的JAR包,如果只是測試用的話,就簡單一點,將所有的JAR選上。


3:在專案qin下增加一個資料夾conf(對準專案右鍵new->folder),將Hbase叢集的配置檔案hbase-site.xml複製到該目錄(直接在eclipse的workspace下找到qin的資料夾複製貼上,在eclipse中重新整理),然後選擇專案屬性(右鍵build path——>configure build path)在Libraries->Add Class Folder,將剛剛增加的conf目錄選上。


4.然後和建立普通的eclipse專案一樣,新建class,碼程式碼;

package qin;
import java.io.IOException;
//import java.util.Map;
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 yin {//宣告靜態HBaseConfiguration
	static Configuration cfg=HBaseConfiguration.create();
 public static void create(String tablename,String columnFamily)throws Exception{
	HBaseAdmin admin=new HBaseAdmin(cfg);
	 if(admin.tableExists(tablename)){
		 System.out.println("table Exists!");
		 System.exit(0);
	 }
	 else{
		 @SuppressWarnings("deprecation")
		HTableDescriptor tableDesc =new HTableDescriptor(tablename);
		 tableDesc.addFamily(new HColumnDescriptor(columnFamily));
		 admin.createTable(tableDesc);
		 System.out.println("create table success!");
	 }
 }
 public static void put(String tablename,String row,String columnFamily,String column,String data)
		 throws IOException{
	 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);
 }
 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 [] args){
	 String tablename="hbase_tb";
	 String columnFamily="cf";
	 try{
		 yin.create(tablename,columnFamily);
		 yin.put(tablename, "row1", columnFamily, "cl1", "data");
		 yin.get(tablename,"row1");
		 yin.scan(tablename);
		/* if(true==yin.delete(tablename)){
			 System.out.println("Delete table:"+tablename+"success!");
		 }*/
	 }catch(Exception e){
		 e.printStackTrace();
	 }
 }
}
5.保證執行前先開啟hbase,然後右鍵yin.java——>run as application.即可成功執行。


終端下檢視hbase可以看到


執行hbase的程式碼例項參考:

http://www.aboutyun.com/thread-7149-1-1.html(增刪改查)

http://www.tuicool.com/articles/amEFVz(mapreduce)