HBase的javaApi一个应用(创建删除表等)

来源:互联网 发布:winrar软件 编辑:程序博客网 时间:2024/05/22 08:01

本文为传智播客hadoop八天——第六天学习笔记

目的:使用HBase提供的api在eclipse中创建表,删除表,查询数据,使用过滤器有选择的查询数据

在启动HBase之前一定要启动Hadoop和Zookeeper!!今天调了一上午的错,竟然是因为没启动Zookeeper。o(╥﹏╥)o

以下为java代码。

package cn.bigdata;import java.util.ArrayList;import java.util.List;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.Cell;import org.apache.hadoop.hbase.HBaseConfiguration;import org.apache.hadoop.hbase.HColumnDescriptor;import org.apache.hadoop.hbase.HTableDescriptor;import org.apache.hadoop.hbase.KeyValue;import org.apache.hadoop.hbase.TableName;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.filter.BinaryComparator;import org.apache.hadoop.hbase.filter.BinaryPrefixComparator;import org.apache.hadoop.hbase.filter.ByteArrayComparable;import org.apache.hadoop.hbase.filter.ColumnPrefixFilter;import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;import org.apache.hadoop.hbase.filter.FamilyFilter;import org.apache.hadoop.hbase.filter.Filter;import org.apache.hadoop.hbase.filter.MultipleColumnPrefixFilter;import org.apache.hadoop.hbase.filter.PrefixFilter;import org.apache.hadoop.hbase.filter.QualifierFilter;import org.apache.hadoop.hbase.filter.RegexStringComparator;import org.apache.hadoop.hbase.filter.RowFilter;import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;import org.apache.hadoop.hbase.filter.SubstringComparator;import org.apache.hadoop.hbase.util.Bytes;import org.junit.Before;import org.junit.Test;public class HbaseDao {    private Configuration conf;    private HTable testDemo;    @Before    public void init() throws Exception {        conf = HBaseConfiguration.create();        //设置Hbase所依赖的Zookeeper集群,我使用的是伪分布模式,所以只有一个节点        conf.set("hbase.zookeeper.quorum", "localhost:2181");        //表名        testDemo = new HTable(conf, "testDemo01");    }    /**     * 根据键名插入数据     *      * @throws Exception     */    @Test    public void testInsert() throws Exception {        //键名        Put name = new Put(Bytes.toBytes("rk0002"));        name.add(Bytes.toBytes("base_info"), Bytes.toBytes("name"),                Bytes.toBytes("天使"));        Put age = new Put(Bytes.toBytes("rk0002"));        age.add(Bytes.toBytes("base_info"), Bytes.toBytes("age"),                Bytes.toBytes("28"));        ArrayList<Put> puts = new ArrayList<Put>();        puts.add(name);        puts.add(age);        testDemo.put(puts);    }    /**     * 删除表     *      * @throws Exception     */    @Test    public void testDrop() throws Exception {        //创建表和删除表都需要HBaseAdmin        HBaseAdmin admin = new HBaseAdmin(conf);        admin.disableTable("testDemo01");        admin.deleteTable("testDemo01");        admin.close();    }    /**     * 根据键名取出数据     *      * @throws Exception     */    @Test    public void testGet() throws Exception {        Get get = new Get(Bytes.toBytes("rk0002"));        get.setMaxVersions(5);        Result result = testDemo.get(get);        List<Cell> cell = result.listCells();        for (KeyValue kv : result.list()) {            String family = new String(kv.getFamily());            System.out.println(family);            String qualifier = new String(kv.getQualifier());            System.out.println(qualifier);            System.out.println(new String(kv.getValue()));        }    }    public static void main(String[] args) throws Exception {        Configuration conf = HBaseConfiguration.create();        conf.set("hbase.zookeeper.quorum", "localhost:2181");        HBaseAdmin admin = new HBaseAdmin(conf);        //表名        TableName name = TableName.valueOf("testDemo01");        HTableDescriptor desc = new HTableDescriptor(name);        //列族        HColumnDescriptor base_info = new HColumnDescriptor("base_info");        HColumnDescriptor extra_info = new HColumnDescriptor("base_info");        //版本数        base_info.setMaxVersions(5);        desc.addFamily(base_info);        desc.addFamily(extra_info);        admin.createTable(desc);    }}

HBase提供了很多筛选的过滤器,用于scan方法

    /**     * 多种过滤条件的使用方法     * @throws Exception     */    @Test    public void testScan() throws Exception{        HTable table = new HTable(conf, "person_info".getBytes());        Scan scan = new Scan(Bytes.toBytes("person_rk_bj_zhang_000001"), Bytes.toBytes("person_rk_bj_zhang_000002"));        // 前缀过滤器----针对行键        Filter filter = new PrefixFilter(Bytes.toBytes("rk"));        // 行过滤器        ByteArrayComparable rowComparator = new BinaryComparator(                Bytes.toBytes("person_rk_bj_zhang_000001"));        RowFilter rf = new RowFilter(CompareOp.LESS_OR_EQUAL, rowComparator);        /**         * 假设rowkey格式为:创建日期_发布日期_ID_TITLE 目标:查找 发布日期 为 2014-12-21 的数据         */        rf = new RowFilter(CompareOp.EQUAL, new SubstringComparator(                "_2014-12-21_"));        // 单值过滤器 1 完整匹配字节数组        new SingleColumnValueFilter("base_info".getBytes(), "name".getBytes(),                CompareOp.EQUAL, "zhangsan".getBytes());        // 单值过滤器2 匹配正则表达式        ByteArrayComparable comparator = new RegexStringComparator("zhang.");        new SingleColumnValueFilter("info".getBytes(), "NAME".getBytes(),                CompareOp.EQUAL, comparator);        // 单值过滤器2 匹配是否包含子串,大小写不敏感        comparator = new SubstringComparator("wu");        new SingleColumnValueFilter("info".getBytes(), "NAME".getBytes(),                CompareOp.EQUAL, comparator);        // 键值对元数据过滤-----family过滤----字节数组完整匹配        FamilyFilter ff = new FamilyFilter(CompareOp.EQUAL,                new BinaryComparator(Bytes.toBytes("base_info")) // 表中不存在inf列族,过滤结果为空        );        // 键值对元数据过滤-----family过滤----字节数组前缀匹配        ff = new FamilyFilter(CompareOp.EQUAL, new BinaryPrefixComparator(                Bytes.toBytes("inf")) // 表中存在以inf打头的列族info,过滤结果为该列族所有行        );        // 键值对元数据过滤-----qualifier过滤----字节数组完整匹配        filter = new QualifierFilter(CompareOp.EQUAL, new BinaryComparator(                Bytes.toBytes("na")) // 表中不存在na列,过滤结果为空        );        filter = new QualifierFilter(CompareOp.EQUAL,                new BinaryPrefixComparator(Bytes.toBytes("na")) // 表中存在以na打头的列name,过滤结果为所有行的该列数据        );        // 基于列名(即Qualifier)前缀过滤数据的ColumnPrefixFilter        filter = new ColumnPrefixFilter("na".getBytes());        // 基于列名(即Qualifier)多个前缀过滤数据的MultipleColumnPrefixFilter        byte[][] prefixes = new byte[][] { Bytes.toBytes("na"),                Bytes.toBytes("me") };        filter = new MultipleColumnPrefixFilter(prefixes);        // 为查询设置过滤条件        scan.setFilter(filter);        scan.addFamily(Bytes.toBytes("base_info"));        ResultScanner scanner = testDemo.getScanner(scan);        for (Result r : scanner) {            // 直接从result中取到某个特定的value            byte[] value = r.getValue(Bytes.toBytes("base_info"),                    Bytes.toBytes("name"));            System.out.println(new String(value));        }        testDemo.close();    }
0 0
原创粉丝点击