HBASE的javaAPI使用,建表,增,删,查,过滤器简单实用

来源:互联网 发布:慕尼黑协定 知乎 编辑:程序博客网 时间:2024/05/16 13:53

-涉及主要对象认识
- HBaseConfiguration: HBase配置类
- HConnectionManager: HBase的java连接类
- HBaseAdmin: HBase的管理类,主要对表的管理,创建,禁用,删除等
- HTableDescriptor: 表属性的描述,添加列族等
- HTable: HBase的表对象,用操作表,添加数据,表扫面等

package com.fang.demo;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.KeyValue;import org.apache.hadoop.hbase.ZooKeeperConnectionException;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.HConnection;import org.apache.hadoop.hbase.client.HConnectionManager;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.CompareFilter.CompareOp;import org.apache.hadoop.hbase.filter.Filter;import org.apache.hadoop.hbase.filter.FilterList;import org.apache.hadoop.hbase.filter.PageFilter;import org.apache.hadoop.hbase.filter.PrefixFilter;import org.apache.hadoop.hbase.filter.RowFilter;import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;import org.apache.hadoop.hbase.filter.TimestampsFilter;import org.apache.hadoop.hbase.util.Bytes;import org.junit.After;import org.junit.Before;import org.junit.Test;public class HBaseFilterTest {    public static Configuration configuration;    public static HConnection hConnection;    @Before    public void testBefore() {        try {            configuration = HBaseConfiguration.create();            configuration.set("hbase.zookeeper.property.clientPort", "2181");            configuration.set("hbase.zookeeper.quorum", "redhat");            configuration.set("hbase.master", "redhat:600000");            hConnection = HConnectionManager.createConnection(configuration);        } catch (ZooKeeperConnectionException e) {            e.printStackTrace();        }    }    @After    public void testAfter() {        try {            hConnection.close();            System.out.println("close");        } catch (IOException e) {            e.printStackTrace();        }    }    /**     *      * @Description: 创建表     * 通过HBaseAdmin管理hbase中的表     * HTableDescriptor管理hbase中的列族信息     */    @Test    public void createTable() throws Exception {        String tableName = "user";        HBaseAdmin hBaseAdmin = new HBaseAdmin(configuration);        if (hBaseAdmin.tableExists(tableName)) {            // hBaseAdmin.disableTable(tableName);            // hBaseAdmin.deleteTable(tableName);            System.out.println(tableName + "表已经存在,并删除表");        }        HTableDescriptor hDescriptor = new HTableDescriptor(tableName);        hDescriptor.addFamily(new HColumnDescriptor("name"));        hDescriptor.addFamily(new HColumnDescriptor("age"));        hDescriptor.addFamily(new HColumnDescriptor("dept"));        hBaseAdmin.createTable(hDescriptor);        hBaseAdmin.close();    }    /***     *      * @Description: 插入表     */    @Test    public void testPut() throws Exception {        String tableName = "user";        HTable table = (HTable) hConnection.getTable(tableName);        table.setAutoFlush(false);// 关闭自动刷新,提交IO吞吐率        List<Put> list = new ArrayList<Put>(1000);        for (int i = 0; i < 200000; i++) {            Put put = new Put(Bytes.toBytes("user_" + i));            put.add(Bytes.toBytes("name"), Bytes.toBytes("FirstName"), Bytes.toBytes("First" + i));            put.add(Bytes.toBytes("name"), Bytes.toBytes("SecondName"), Bytes.toBytes("Second" + i));            put.add(Bytes.toBytes("age"), null, Bytes.toBytes(i + ""));            put.add(Bytes.toBytes("dept"), null, Bytes.toBytes("dept" + i));            list.add(put);            if (i % 1000 == 0) {                table.put(list);                list = new ArrayList<Put>(1000);            }        }        if (list.size() > 0) {            table.put(list);        }        table.close();        hConnection.close();    }    /**     *     * @Description: 通过key获取值     */    @Test    public void testGet() throws Exception {        HTable table = (HTable) hConnection.getTable("user");        Get get = new Get(Bytes.toBytes("user_9985"));        Result result = table.get(get);        if (result != null && result.size() > 0) {            System.out.print("key=" + new String(result.getRow()) + "   ");            for (KeyValue key : result.raw()) {                System.out.print(new String(key.getFamily()) + "=" + new String(key.getQualifier()) + "=" + new String(key.getValue()) + "   ");            }        } else {            System.out.println("没有数据");        }        hConnection.close();    }    /**     * @Description: hbase扫描器     */    @Test    public void testScan() throws Exception {        HTable table = (HTable) hConnection.getTable("user");        Scan scan = new Scan();        scan.setCaching(1000);// 设置扫描器缓存区的大小        ResultScanner rs = table.getScanner(scan);        for (Result r : rs) {            System.out.print("key=" + new String(r.getRow()) + "   ");            for (KeyValue key : r.raw()) {                System.out.print(new String(key.getFamily()) + "=" + new String(key.getQualifier()) + "=" + new String(key.getValue()) + "   ");            }            System.out.println();        }    }    /**     *      * @Description: HBase过滤器     * 单列值过滤器(SingleColumnValueFilter)     */    @Test    public void testScanFilter() throws Exception {        HTable table = (HTable) hConnection.getTable("user");        Scan scan = new Scan();        scan.setCaching(1000);// 设置扫描器缓存区的大小        Filter filter = new SingleColumnValueFilter(Bytes.toBytes("name"), Bytes.toBytes("FirstName"), CompareOp.EQUAL, Bytes.toBytes("First22"));        scan.setFilter(filter);        ResultScanner rs = table.getScanner(scan);        for (Result r : rs) {            System.out.print("key=" + new String(r.getRow()) + "   ");            for (KeyValue key : r.raw()) {                System.out.print(new String(key.getFamily()) + "=" + new String(key.getQualifier()) + "=" + new String(key.getValue()) + "   ");            }            System.out.println();        }    }    /**     *      * @Description: 多扫描器使用     */    @Test    public void testScanFilterList() throws Exception {        HTable table = (HTable) hConnection.getTable("user");        Scan scan = new Scan();        scan.setCaching(1000);// 设置扫描器缓存区的大小        List<Filter> filterList = new ArrayList<>();        Filter filter = new SingleColumnValueFilter(Bytes.toBytes("name"), Bytes.toBytes("FirstName"), CompareOp.EQUAL, Bytes.toBytes("First22"));        Filter filter2 = new SingleColumnValueFilter(Bytes.toBytes("age"), null, CompareOp.EQUAL, Bytes.toBytes("22"));        filterList.add(filter);        filterList.add(filter2);        FilterList fList = new FilterList(filterList);        scan.setFilter(fList);        ResultScanner rs = table.getScanner(scan);        for (Result r : rs) {            System.out.print("key=" + new String(r.getRow()) + "   ");            for (KeyValue key : r.raw()) {                System.out.print(new String(key.getFamily()) + "=" + new String(key.getQualifier()) + "=" + new String(key.getValue()) + "   ");            }            System.out.println();        }    }    @Test    public void deleteTableRow() {        try {            HTable table = new HTable(configuration, "user");            List<Delete> list = new ArrayList<Delete>();            Delete d1 = new Delete("user19".getBytes());            list.add(d1);            table.delete(list);            System.out.println("删除行成功!");        } catch (IOException e) {            e.printStackTrace();        }    }    /**     *     * @Description: 行过滤器     */    @Test    public void testRowFilter() throws Exception {        HTable table = (HTable) hConnection.getTable("user");        Scan scan = new Scan();        scan.setCaching(1000);// 设置扫描器缓存区的大小        Filter filter = new  RowFilter(CompareOp.EQUAL, new  BinaryComparator(Bytes.toBytes("user_22")));        scan.setFilter(filter);        ResultScanner rs = table.getScanner(scan);        for (Result r : rs) {            System.out.print("key=" + new String(r.getRow()) + "   ");            for (KeyValue key : r.raw()) {                System.out.print(new String(key.getFamily()) + "=" + new String(key.getQualifier()) + "=" + new String(key.getValue()) + "   ");            }            System.out.println();        }    }    /**     *     * @Description: 分页过滤器     */    @Test    public void testPageFilter() throws Exception {        HTable table = (HTable) hConnection.getTable("user");        Scan scan = new Scan();        scan.setCaching(1000);// 设置扫描器缓存区的大小        PageFilter filter = new  PageFilter(20);//每页20条        scan.setStartRow(Bytes.toBytes(20));//从20行开始,就是查询第二页//      scan.setStopRow(s)设置结束行        scan.setFilter(filter);        ResultScanner rs = table.getScanner(scan);        for (Result r : rs) {            System.out.print("key=" + new String(r.getRow()) + "   ");            for (KeyValue key : r.raw()) {                System.out.print(new String(key.getFamily()) + "=" + new String(key.getQualifier()) + "=" + new String(key.getValue()) + "   ");            }            System.out.println();        }    }    /**     *     * @throws Exception      * @Description: 前缀过滤器(PrefixFilter)     */    @Test    public void testPrefixFilter() throws Exception{        HTable table = (HTable) hConnection.getTable("user");        Scan scan = new Scan();        scan.setCaching(1000);// 设置扫描器缓存区的大小        Filter filter = new PrefixFilter(Bytes.toBytes("user_222"));        scan.setFilter(filter);        ResultScanner rs = table.getScanner(scan);        for (Result r : rs) {            System.out.print("key=" + new String(r.getRow()) + "   ");            for (KeyValue key : r.raw()) {                System.out.print(new String(key.getFamily()) + "=" + new String(key.getQualifier()) + "=" + new String(key.getValue()) + "   ");            }            System.out.println();        }    }    /**     * 时间戳过滤器(TimestampsFilter)     */    @Test    public void testTimestampsFilter() throws Exception{        HTable table = (HTable) hConnection.getTable("user");        Scan scan = new Scan();        scan.setCaching(1000);// 设置扫描器缓存区的大小        List<Long> ts = new ArrayList<Long>();        ts.add(new Long(1462618668891L));        ts.add(new Long(1462618657998L));        ts.add(new Long(1462618668392L));        Filter filter = new TimestampsFilter(ts);        scan.setFilter(filter);        ResultScanner rs = table.getScanner(scan);        for (Result r : rs) {            System.out.print("key=" + new String(r.getRow()) + "   ");            for (KeyValue key : r.raw()) {                System.out.print(new String(key.getFamily()) + "=" + new String(key.getQualifier()) + "=" + new String(key.getValue()) + "   ");            }            System.out.println();        }    }}
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 借钱不还怎么办没欠条 私人欠货款不还怎么办 公司欠货款不还怎么办 两个人离婚一方不同意怎么办 比亚迪l3油耗高怎么办 u盘密码忘记了怎么办 主板没有m.2接口怎么办 点痣留下了疤怎么办 危险三角区长痘痘怎么办 挤了危险三角区怎么办 三角区长痘挤了怎么办 三角区发红长痘怎么办 激光祛斑碰水了怎么办 激光打痣留下坑怎么办 点痣之后留下坑怎么办 去痣留下的红印怎么办 激光点痦子留疤怎么办 激光点痣的疤痕怎么办 做完眉毛碰水了怎么办 脸上疤掉了有坑怎么办 结痂不小心抠掉怎么办 脸上肉松弛怎么办19岁 点痣留下来的疤怎么办 激光祛斑的红印怎么办 脸上疤掉了红印怎么办 痘痘发炎了红肿怎么办 脸上的斑越来越多了怎么办 点痣留下的疤痕怎么办 额头又高又大怎么办 脸太长额头太高怎么办 动车因台风停运怎么办 爸妈50了要离婚怎么办 鸿利彩票黑了钱怎么办 忘了锁屏图案怎么办 黄金被水银沾上怎么办 被股东了我该怎么办 异地恋没话题聊怎么办 谈了半年分手了怎么办 博士6年没毕业怎么办 发现孩子早恋家长应该怎么办 异地恋想嘿嘿嘿怎么办