Hbase 1.2.1 Java API简单demo

11 篇文章 0 订阅

自从hbase1.1.3后,hbase的api有很多改动,比如HTablePool,HTable等的类的很多方法已经过时

这里简单写几个

导入包

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

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.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
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;
import org.junit.Before;
import org.junit.Test;

public class HBaseDemo {
	Configuration conf;
	Connection conn;

喜闻乐见的Junit测试

	@Before
	public void init() {
		conf = HBaseConfiguration.create();
		conf.set("hbase.zookeeper.property.clientPort", "2181");
		conf.set("hbase.zookeeper.quorum", "hadoop,hadoop1,hadoop2");
		try {
			conn = ConnectionFactory.createConnection(conf);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
创建表

	@Test
	public void createTable() throws IOException {

		HBaseAdmin admin = (HBaseAdmin) conn.getAdmin();
		HTableDescriptor htd = new HTableDescriptor(TableName.valueOf("people"));
		HColumnDescriptor htd_info = new HColumnDescriptor("info");
		htd.addFamily(htd_info);
		htd.addFamily(new HColumnDescriptor("data"));
		htd_info.setMaxVersions(3);

		admin.createTable(htd);
		admin.close();

	}
单行put
	@Test
	public void testPut() throws IOException {
		HTableDescriptor htd = new HTableDescriptor(TableName.valueOf("people"));
		HTable table = (HTable) conn.getTable(TableName.valueOf("people"));

		Put put = new Put(Bytes.toBytes("rk0001"));
		put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"),
				Bytes.toBytes("zhangsan"));
		put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("age"),
				Bytes.toBytes("25"));
		put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("money"),
				Bytes.toBytes("10w"));
		table.put(put);
	}
批量插入
    @Test
    public void testPutAll() throws IOException {
        // HTablePool pool =

        HTable table = (HTable) conn.getTable(TableName.valueOf("people"));

        List<Put> puts = new ArrayList<Put>(10000);
        for (int i = 1; i <= 100001; i++) {
            Put put = new Put(Bytes.toBytes("rk" + i));
            put.addImmutable(Bytes.toBytes("info"), Bytes.toBytes("money"),
                    Bytes.toBytes("" + i));
            puts.add(put);
            if (i % 10000 == 0) {
                table.put(puts);
                puts = new ArrayList<Put>(10000);
            }
        }
通过测试插入十万条记录,也就几秒的事情,充分证明了hbase的读写速度
单行get

	@Test
	public void testGet() throws IOException {

		HTable table = (HTable) conn.getTable(TableName.valueOf("people"));
		Get get = new Get(Bytes.toBytes("rk9999"));
		Result result = table.get(get);
		String str = Bytes.toString(result.getValue(Bytes.toBytes("info"),
				Bytes.toBytes("money")));
		System.out.println(str);
		table.close();
	}
多行scan
	@Test
	public void testDelete() throws IOException {
		HTable table = (HTable) conn.getTable(TableName.valueOf("people"));
		Delete delete = new Delete(Bytes.toBytes("rk9999"));
		table.delete(delete);
		table.close();
	}
通过scan可以验证hbase的字典排序规则,和半开半闭区间,获取结果如下


单行删除

	@Test
	public void testScan() throws IOException {
		HTable table = (HTable) conn.getTable(TableName.valueOf("people"));
		Scan scan = new Scan(Bytes.toBytes("rk29990"), Bytes.toBytes("rk30000"));
		ResultScanner resultScaner = table.getScanner(scan);
		for (Result result : resultScaner) {
			String str = Bytes.toString(result.getValue(Bytes.toBytes("info"),
					Bytes.toBytes("money")));
			System.out.println(str);
		}
		table.close();
	}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值