HBase之Java API实操

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.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
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.thrift.generated.Hbase;
import org.apache.hadoop.hbase.util.Bytes;
public class HBaseAPI {
	public static Configuration conf= HBaseConfiguration.create();
	//创建table
	public static void create(String tablename,String columnFamily) throws MasterNotRunningException, ZooKeeperConnectionException, IOException{
		HBaseAdmin admin = new HBaseAdmin(conf);
		if(admin.tableExists(TableName.valueOf(tablename))){
			System.out.println(tablename+" exists!");
			System.exit(0);
		}else{
			HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf(tablename));
			tableDesc.addFamily(new HColumnDescriptor(columnFamily));
			admin.createTable(tableDesc);	
		}
	}
	//通过put为已经存在的表添加一条数据
	public static void put(String tablename,String rowkey,String columnFamily,String qualifier,String value) throws IOException{
		HTable htable = new HTable(conf, tablename);
		Put p1= new Put(Bytes.toBytes(rowkey));
		p1.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(qualifier), Bytes.toBytes(value));
		htable.put(p1);
		System.out.println("Put complete!");
	}
	//通过Get获得数据
	public static void get(String tablename,String rowkey,String columnFamily,String qualifier) throws IOException{
		HTable htable = new HTable(conf, tablename);
		Get g1 = new Get(Bytes.toBytes(rowkey));
		g1.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(qualifier));
		Result result=htable.get(g1);
		System.out.println("Get: "+result);
	}
	public static void scan(String tablename,String columnFamily) throws IOException{
		HTable htable = new HTable(conf, tablename);
		//Scan s = new Scan();
		ResultScanner rs = htable.getScanner(Bytes.toBytes(columnFamily));
		for(Result r:rs){
			System.out.println("Scan: "+r);
		}
	}
	//删除table
	public static void delete(String tablename) throws MasterNotRunningException, ZooKeeperConnectionException, IOException{
		HBaseAdmin admin = new HBaseAdmin(conf);
		if(admin.tableExists(tablename)){
			admin.disableTable(tablename);
			admin.deleteTable(tablename);
			System.out.println(tablename+" is deleted");
		}
	}
	public static void main(String[] args){
		String tablename = "table_test";
		String columnFamily = "cf1";
		try{
			create(tablename, columnFamily);
			put(tablename, "row1", columnFamily, "test001", "succed");
			put(tablename, "row1", columnFamily, "test002", "failed");
			get(tablename, "row1",columnFamily,"test001");
			scan(tablename,columnFamily);
			delete(tablename);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}

运行结果:


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值