eclipse 连接并操作单机版Hbase

在自己电脑上面配置了一个单击版的Hbase,想用Eclipse来连接并操作Hbase,看了网上一些教程基本上都是配置的分布式的Hbase集群。

自己试着能让单机版的HBase在eclipse下面跑起来。



单机版的Hbase配置比较简单,文件系统直接用的本地文件系统,不像分布式下面需要HDFS


首先启动Hbase       


<span style="font-size:18px;">root@bigfish-System-Product-Name:/usr/local/hbase-0.92.1/bin# ./start-hbase.sh 
starting master, logging to /usr/local/hbase-0.92.1/bin/../logs/hbase-root-master-bigfish-System-Product-Name.out
root@bigfish-System-Product-Name:/usr/local/hbase-0.92.1/bin# 
</span>

启动后在本地网页中输入

  http://localhost:60010/   会显示一些基本的信息


然后打开Hbase shell  


<span style="font-size:18px;">root@bigfish-System-Product-Name:/usr/local/hbase-0.92.1/bin# ./hbase shell
HBase Shell; enter 'help<RETURN>' for list of supported commands.
Type "exit<RETURN>" to leave the HBase Shell
Version 0.92.1, r1298924, Fri Mar  9 16:58:34 UTC 2012

hbase(main):001:0> list
TABLE                                                                                                                                               
mytable                                                                         
2 row(s) in 0.7870 seconds

</span>

这是Hbase已经启动了

下面开始搭建eclipse环境


1    新建一个java项目  取名为  HbaseTest


2  导入相关的Jar包(包位于Hbase    lib 文件下面)






3:在项目HBase下增加一个文件夹conf,将Hbase集群的配置文件hbase-site.xml复制到该目录,然后选择项目属性在Libraries->Add Class Folder,将刚刚增加的conf目录选上。

我的hbase-site.xml配置如下


<span style="font-size:18px;"><configuration>

<property>
<name>hbase.rootdir</name>
<value>file:///home/bigfish/myhbase1/</value>
</property>
 <property>
	        <name>hbase.zookeeper.property.dataDir</name>
	        <value>/home/bigfish/myhbase1/zookeeper</value>
	    </property>
</configuration></span>

4:在建好的项目中新建一个包和新建一个类

编辑如下测试代码


<span style="font-size:18px;">package bigfish.com;

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();
        }    
}
}</span>


5:设置运行配置,然后运行.


运行结果如下:


<span style="font-size:18px;">create table success!
put 'row1','cf:cl1','data'
Get: keyvalues={row1/cf:cl1/1413277900260/Put/vlen=4}
Scan: keyvalues={row1/cf:cl1/1413277900260/Put/vlen=4}
</span>

可以看到已经在Hbase中建好了表


<span style="font-size:18px;">hbase(main):001:0> list
TABLE 
hbase_tb                                                                                                                                              
mytable                                                                         
2 row(s) in 0.7870 seconds</span>

查看数据   scan   ‘hbase_tb’


<span style="font-size:18px;">hbase(main):002:0> scan 'hbase_tb'
ROW                   COLUMN+CELL                                               
 row1                 column=cf:cl1, timestamp=1413277900260, value=data        
1 row(s) in 0.1700 seconds
</span>

至此,我们就能够用eclipse来在单机Hbase中进行操作了



  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值