Linux下Hbase在eclipse上的hadoop环境配置

一.环境配置

         当第三方访问hbase的时候,首先需要访问ZooKeeper,因为hbse的重要信息保存在ZooKeeper当中。我们知道,ZooKeeper集群的信息是由$HBASE_HOME/conf/hbase-site.xml文件指定。因此需要通过classpath来指定hbase配置文件的配置,即$HBASE_HOME/conf的位置。

         使用hbase客户端进行编程时,hbase,hadoop,log4j,ZooKeeper等JAR包对于程序来说是必不可少的,编程是必须加载到工程目录下。

         下面演示具体的配置

         1.运行Eclipse,创建一个新的Java工程“HBaseClient”,右键项目根目录,选择 “Properties”->“Java Build Path”->“Library”->“Add External JARs”,将HBase解压后根目录下的lib子目录下所有jar 包添加到本工程的Classpath下。 

         2.在工程根目录下创建Conf文件夹,将$HBASE_HOME/conf目录中的hbase-site.xml文件复制到该文件夹中。通过右键项目根目录,选择 “Properties”->“Java Build Path”->“Library”->"Add ClassFolder",然后Conf文件夹进行添加。

               上述配置完成后就可以进行HBase程序的编写了。

二.HBase Java API 简单示例

package MyHBase;

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 Demo1 {
	//声明静态配置
	static Configuration cfg=HBaseConfiguration.create();
	public static void create(String tablename)throws Exception{
		HBaseAdmin admin=new HBaseAdmin(cfg);
		if(admin.tableExists(tablename)){
			System.out.println("table Exists");
			System.exit(0);
		}else{
		    HTableDescriptor htd=new HTableDescriptor(tablename);
		    admin.createTable(htd);
		    System.out.println("Create table success");
		}
		admin.close();
	} 
	
	public static void addColumnFamily(String tablename,String cfname) throws Exception{
		HBaseAdmin admin=new HBaseAdmin(cfg);
		HTableDescriptor htd=new HTableDescriptor(tablename);
	    HColumnDescriptor family=new HColumnDescriptor(cfname);
		htd.addFamily(family);
		admin.addColumn(tablename, family);
	    System.out.println("add cf success");
	    admin.close();
	}
	
	public static void put(String tablename,String rowkey,String cf,String cq,String data)throws Exception{
		HTable table=new HTable(cfg,tablename);
		Put put=new Put(Bytes.toBytes(rowkey));
		put.add(Bytes.toBytes(cf),
				Bytes.toBytes(cq),
				Bytes.toBytes(data));
		table.put(put);
		System.out.println("put  '"+rowkey+"', '"+cf+"', '"+cq+"', '"+data+"'");
		table.close();
	}
	
	public static void get(String tablename,String rowkey)throws IOException{
		HTable table=new HTable(cfg,tablename);
		Get get=new Get(Bytes.toBytes(rowkey));
		Result r =table.get(get);
		System.out.println("Get:"+r);
		table.close();
	}
	
	public static void get(String tablename,String rowkey,String cf)throws IOException{
		HTable table=new HTable(cfg,tablename);
		Get get=new Get(Bytes.toBytes(rowkey));
		get.addFamily(Bytes.toBytes(cf));
		Result r =table.get(get);
		table.close();
		System.out.println("Get:"+r);
	}
	
	public static void get(String tablename,String rowkey,String cf,String cq)throws IOException{
		HTable table=new HTable(cfg,tablename);
		Get get=new Get(Bytes.toBytes(rowkey));
		get.addColumn(Bytes.toBytes(cf),Bytes.toBytes(cq));
		Result r =table.get(get);
		table.close();
		System.out.println("Get:"+r);
	}
	
	public static void scan(String tablename) throws IOException{
		HTable table=new HTable(cfg,tablename);
		Scan scan=new Scan();
		ResultScanner rs=table.getScanner(scan);
		for(Result r:rs){
			System.out.println("Scan:"+r);
		}
		table.close();
	}
	
	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();
				admin.close();
				return false;
			}
		}
		admin.close();
		return true;
	}
	
	public static void main(String []agrs){
		String tablename="test1";
		String columnfamily="info";
		
		try{
			//create(tablename);
			//addColumnFamily(tablename, columnfamily);
			//put(tablename,"1", columnfamily, "cq", "cq1");
			//get(tablename, "1");
			//scan(tablename);
			if(delete(tablename)){
				System.out.println("Delete table:"+tablename+"  success");
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值