Hbase Java编程基本操作

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.io.BytesWritable;

import org.apache.hadoop.hbase.util.Bytes;

public class HbaseTestCase {

	// 声明静态配置
	static Configuration configuration = HBaseConfiguration.create();

	// 创建表
	public static void create(String tablename, String colunmnFamily)
			throws IOException {
		HBaseAdmin admin = new HBaseAdmin(configuration);
		if (admin.tableExists(tablename)) {
			System.out.println("table exists!");
			admin.disableTable(tablename);
			admin.deleteTable(tablename);
		}
		HTableDescriptor descriptor = new HTableDescriptor(tablename);
		descriptor.addFamily(new HColumnDescriptor(colunmnFamily));
		admin.createTable(descriptor);
		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(configuration, 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);
		System.out.println("===========================================");
	}

	// 得到一条记录
	public static void get(String tablename, String row) throws IOException {
		HTable table = new HTable(configuration, tablename);
		Get get = new Get(Bytes.toBytes(row));
		Result result = table.get(get);
		System.out.println("Get:" + result);
		System.out.println("===========================================");
	}

	// 扫描表
	public static void scan(String tablename) throws Exception {
		HTable table = new HTable(configuration, tablename);
		Scan scan = new Scan();
		ResultScanner rscanner = table.getScanner(scan);
		for (Result result : rscanner) {
			System.out.println("Scan:" + result);
		}
		System.out.println("===========================================");
	}

	// 删除表
	public static boolean delete(String tablename) throws Exception {
		HBaseAdmin admin = new HBaseAdmin(configuration);
		try {
			if (admin.tableExists(tablename)) {
				admin.disableTable(tablename);
				admin.deleteTable(tablename);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return true;
	}

	public static void main(String[] args) throws Exception {
		String tablename = "hbase_tb";
		String columnFamily = "cf";
		create(tablename, columnFamily);
		put(tablename, "row1", columnFamily, "cf1", "data");
		get(tablename, "row1");
		scan(tablename);
		delete(tablename);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值