eclipse构建HBase开发环境并运行实例对Hbase建表增删改查

时间:2021-03-23 08:31:18

首先确定hbase与Hdoop版本一致,网上都能查,我的是hadoop2.6.0+hbase1.0.0安装配置好后,明确了在终端命令行下面可以对数据库进行操作

eclipse构建HBase开发环境并运行实例对Hbase建表增删改查

然后对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选上。

eclipse构建HBase开发环境并运行实例对Hbase建表增删改查

eclipse构建HBase开发环境并运行实例对Hbase建表增删改查

3:在项目qin下增加一个文件夹conf(对准项目右键new->folder),将Hbase集群的配置文件hbase-site.xml复制到该目录(直接在eclipse的workspace下找到qin的文件夹复制粘贴,在eclipse中刷新),然后选择项目属性(右键build path——>configure build path)在Libraries->Add Class Folder,将刚刚增加的conf目录选上。

eclipse构建HBase开发环境并运行实例对Hbase建表增删改查

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.即可成功运行。

eclipse构建HBase开发环境并运行实例对Hbase建表增删改查

终端下查看hbase可以看到

eclipse构建HBase开发环境并运行实例对Hbase建表增删改查

运行hbase的代码实例参考:

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

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