Hbase 1.2.1 Java API简单demo

来源:互联网 发布:二胡软件 编辑:程序博客网 时间:2024/06/01 08:19

自从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测试

@Beforepublic 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();}}
创建表

@Testpublic 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
@Testpublic 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

@Testpublic 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
@Testpublic 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的字典排序规则,和半开半闭区间,获取结果如下


单行删除

@Testpublic 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();}



0 0