HBase的JAVA API

本文介绍了一个使用Java API操作HBase数据库的示例程序,包括创建表、插入记录、获取记录及扫描表等功能。示例中详细展示了如何通过HBaseAdmin和HTable接口进行表管理和数据操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package 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.TableDescriptors;
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.Processor.createTable;
import org.apache.hadoop.hbase.thrift.generated.Hbase.Processor.deleteTable;

import com.sun.org.apache.bcel.internal.generic.NEW;


public class HBaseApp {
	

	private static final String TABLE_NAME = "table2";
	private static final String FAMILY_NAME = "family2";
	private static final String ROW_KEY = "rowkey2";
	public static void main(String[] args) throws Exception {

		Configuration conf = HBaseConfiguration.create();
		conf.set("hbase.rootdir", "hdfs://hadoop:9000/hbase");
	    //使用eclipse时必须添加这个,否则无法定位
		conf.set("hbase.zookeeper.quorum", "hadoop,hadoop1,hadoop2");
		//创建表,删除表使用HBaseAdmin
		final HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);
		//createTable(hBaseAdmin);
		//deleteTable(hBaseAdmin);
		
		//插入,查询,遍历使用HTable
		final HTable hTable = new HTable(conf, TABLE_NAME);
	
		//putRecord(hTable);
		getRecord(hTable);
		//scanTable(hTable);	
		hTable.close();
	}
	
	
	private static void scanTable(final HTable hTable) throws IOException {
		Scan scan = new Scan();
		final ResultScanner scanner = hTable.getScanner(scan);
		for (Result result : scanner) {
			final byte[] value = result.getValue(FAMILY_NAME.getBytes(), "age".getBytes());
			System.out.println(result+"\t"+new String(value));
		}
	}
	private static void getRecord(final HTable hTable) throws IOException {
		Get get = new Get(ROW_KEY.getBytes());
		final Result result = hTable.get(get);//keyvalues={rowkey2/family2:age/1522807817820/Put/vlen=2/seqid=0}
		final byte[] value = result.getValue(FAMILY_NAME.getBytes(), "age".getBytes());//25
		System.out.println(result+"\t"+new String(value));
	}
	private static void putRecord(final HTable hTable) throws IOException {
		Put put = new Put(ROW_KEY.getBytes());
		put.add(FAMILY_NAME.getBytes(), "age".getBytes(), "25".getBytes());
		hTable.put(put);
	}
	private static void deleteTable(final HBaseAdmin hBaseAdmin) throws IOException {
		hBaseAdmin.disableTable(TABLE_NAME);
		hBaseAdmin.deleteTable(TABLE_NAME);
	}
	private static void createTable(final HBaseAdmin hBaseAdmin) throws IOException {
		if(!hBaseAdmin.tableExists(TABLE_NAME)) {
			HTableDescriptor tableDescriptor = new HTableDescriptor(TABLE_NAME);
			HColumnDescriptor family = new HColumnDescriptor(FAMILY_NAME);
			tableDescriptor.addFamily(family);
			hBaseAdmin.createTable(tableDescriptor);
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值