用java API来操作HBase

本文介绍如何在Eclipse环境中配置HBase,并演示了通过Java API进行表的创建与删除、记录的添加与获取以及全表扫描等基本操作。

我们在日常工作中,用eclipse来编写具体的程序,来操作HBase数据库,首先在eclispe中配置HBase环境

新建项目--》导入HBase中的jar包

下面直接贴代码

package com.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.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;

public class HBaseTest {

	public static final String TABLE_NAME = "table1";

	public static final String FAMILY_NAME = "family1";

	public static final String ROW_KEY = "rowkey1";

	public static void main(String[] args) throws Exception {
		Configuration conf = HBaseConfiguration.create();
		conf.set("hbase.rootdir", "hdfs://master.dragon.org:9000/hbase");
		// 使用eclipse,必须使用这个配置定位
		conf.set("hbase.zookeeper.quorum", "master.dragon.org");

		// HBaseAdmin用于创建表和删除表
		HBaseAdmin admin = new HBaseAdmin(conf);
		createTable(admin);//创建表

		deleteTable(admin);//删除表

		// HTable适用于添加记录,查看记录
		HTable hTable = new HTable(conf, TABLE_NAME);

		addRecord(hTable);//添加记录

		getRecord(hTable);//得到记录

		scannerTable(hTable);//全表扫描
		hTable.close();
	}

	private static void scannerTable(HTable hTable) throws IOException {
		Scan scan = new Scan();
		ResultScanner scanner = hTable.getScanner(scan);
		for (Result result : scanner) {
			byte[] value = result.getValue(FAMILY_NAME.getBytes(), "info".getBytes());
			System.out.println(result + "\t" + new String(value));
		}
	}

	private static void getRecord(HTable hTable) throws IOException {
		Get get = new Get(ROW_KEY.getBytes());
		Result result = hTable.get(get);
		byte[] value = result.getValue(FAMILY_NAME.getBytes(), "info".getBytes());
		System.out.println(result + "\t" + new String(value));
	}

	private static void addRecord(HTable hTable) throws IOException {
		Put put = new Put(ROW_KEY.getBytes());
		put.add(FAMILY_NAME.getBytes(), "info".getBytes(), "50".getBytes());
		hTable.put(put);
	}

	private static void deleteTable(HBaseAdmin admin) throws IOException {
		admin.disableTable(TABLE_NAME);
		admin.deleteTable(TABLE_NAME);
	}

	private static void createTable(HBaseAdmin admin) throws IOException {
		if (!admin.tableExists(TABLE_NAME)) {
			HTableDescriptor desc = new HTableDescriptor(TABLE_NAME);
			HColumnDescriptor family = new HColumnDescriptor(FAMILY_NAME);
			desc.addFamily(family);
			admin.createTable(desc);
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

火柴有猿

您的鼓励,将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值