java 访问部署在hdfs上hbase数据库

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.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Delete;
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 HbaseConnetion {
	
	 Configuration cfg;
	 HBaseAdmin admin;
	
	//初始化建立连接
	public HbaseConnetion() throws MasterNotRunningException, ZooKeeperConnectionException{
		cfg=HBaseConfiguration.create();
		admin=new HBaseAdmin(cfg);
	}
	
	
	
	
	//关闭数据库连接
	public   void  close() throws IOException{
		if (admin != null) admin.close();
	}
	
	//判断表是否存在
	public  boolean isTableExist(String tablename) throws IOException{
		if(admin.tableExists(tablename)){
			return true;
		}
		else{
			System.out.println("Table not exists");
			return false;
		}
	}
	
	//新建一个表
	public  void creat(String tablename,String[] culumnFamlilies) throws IOException{
		
		if(admin==null) {
			System.out.println("Database not Connected");
			return;
		}
		if(isTableExist(tablename))  return;
		
		HTableDescriptor desc =new HTableDescriptor(tablename);
		for(int i=0;i<culumnFamlilies.length;i++){
				desc.addFamily(new HColumnDescriptor(culumnFamlilies[i]));
		}
		admin.createTable(desc);
		
	}
	
	
	//添加一条记录
	public  void put(String tablename,String row_key,String family,String value)throws Exception{
		
		if (!isTableExist(tablename))  return;	
		HTable table=new HTable(cfg, Bytes.toBytes(tablename));
		Put put=new Put(Bytes.toBytes(row_key));
		put.add(Bytes.toBytes(family),null,Bytes.toBytes(value));
		table.put(put);
		table.close();
		
	}
	
	//添加一条记录
	public  void put(String tablename,String row_key,String family,String column,String value)throws Exception{
		
		if (!isTableExist(tablename))  return;	
		HTable table=new HTable(cfg, Bytes.toBytes(tablename));
		Put put=new Put(Bytes.toBytes(row_key));
		put.add(Bytes.toBytes(family),Bytes.toBytes(column),Bytes.toBytes(value));
		table.put(put);
		table.close();
		
	}
	
	
	//按照行键查询记录
	public  Result get(String tablename ,String row_key) throws IOException{
		if(!isTableExist(tablename)) return null;
		Get get=new Get(Bytes.toBytes(row_key));
		HTable table=new HTable(cfg, tablename);
		Result result=table.get(get);
		table.close();
		System.out.println(result);
		return result;
	}
	
	
	//按行键删除记录
	public  boolean deleteRow(String tablename,String row_key) throws IOException{
		
		if(!isTableExist(tablename)) return false;
		HTable table=new HTable(cfg,Bytes.toBytes(tablename));
		Delete delete=new Delete(Bytes.toBytes(row_key));
		table.delete(delete);
		table.close();
		return true;
		
	}
	
	//删除table
	public  boolean deleteTable(String tablename) throws IOException{
		
		if(!isTableExist(tablename))  
			return false;
		admin.enableTable(tablename);
		admin.deleteTable(tablename);
		return true;
		
	}
	
	//显示所有数据
	public  void scan(String tablename) throws IOException{
		if(!isTableExist(tablename)) return;
		Scan scan=new Scan();
		HTable table=new HTable(cfg, tablename);
		ResultScanner rs=table.getScanner(scan);
		if(rs==null){System.out.println("No result");}
		for(Result r:rs){
			System.out.println("Scan: "+r);
		}
		table.close();
	}
	
	
	
	//测试
	public static void main(String[] args) throws Exception{
		HbaseConnetion hbaseConnetion=new HbaseConnetion();
		hbaseConnetion.put("teachers", "no1", "name", "famlilyName", "bai");
		hbaseConnetion.put("teachers", "no1", "age", "24");
		hbaseConnetion.get("teachers", "no1");
		hbaseConnetion.close();

	}
	
}

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值