Hbase的drop(),del(),create(),scan()条件过滤,put(),get() 的Java方法

package com.bynear.HBase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.util.Bytes;

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

/**
 * 2018/6/22
 * 9:42
 */
public class HBaseDemo {
    private Configuration conf = null;

    public void init() {
        conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "192.168.2.15:2181,192.168.2.16:2181");
    }

    public void Drop() throws IOException {
        HBaseAdmin admin = new HBaseAdmin(conf);
        admin.disableTable("account");
        admin.deleteTable("account");
        admin.close();
    }

    public void Put() throws IOException {
        HTable table = new HTable(conf, "person_info");

        Put name = new Put(Bytes.toBytes("rk001"));
        name.add("base_info".getBytes(), "name".getBytes(), "zhangwuji".getBytes());
        Put age = new Put(Bytes.toBytes("rk001"));
        age.add("base_info".getBytes(), "age".getBytes(), Bytes.toBytes(18));
        ArrayList<Put> puts = new ArrayList<Put>();
        puts.add(name);
        puts.add(age);
        table.put(puts);
        table.close();
    }

    public void Get() throws IOException {
        HTable table = new HTable(conf, "person_info");
        Get name = new Get(Bytes.toBytes("rk001"));
        name.setMaxVersions(5);
        Result result = table.get(name);
        List<Cell> cells = result.listCells();
        //遍历出result中所有的键值对
        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()));
        }
        table.close();
    }

    /**
     * 多种过滤条件的使用方法
     *
     * @throws Exception
     */
    public void Scan() throws IOException {
        HTable table = new HTable(conf, "person_info");
        Scan scan = new Scan(Bytes.toBytes("rk001"));

        //前缀过滤器----针对行键
        PrefixFilter filter = new PrefixFilter(Bytes.toBytes("rk"));
        //行过滤器
        BinaryComparator rowComparator = new BinaryComparator(Bytes.toBytes("rk001"));
        RowFilter rf = new RowFilter(CompareFilter.CompareOp.LESS_OR_EQUAL, rowComparator);
        /**
         * 假设rowkey格式为:创建日期_发布日期_ID_TITLE
         * 目标:查找  发布日期  为  2014-12-21  的数据
         */
        rf = new RowFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator("_2014-12-21_"));

        //单值过滤器 1 完整匹配字节数组
        new SingleColumnValueFilter("base_info".getBytes(), "name".getBytes(), CompareFilter.CompareOp.EQUAL, "zhangsan".getBytes());

        //单值过滤器2 匹配正则表达式
        RegexStringComparator comparator = new RegexStringComparator("zhang.");
        new SingleColumnValueFilter("base_info".getBytes(), "name".getBytes(), CompareFilter.CompareOp.EQUAL, comparator);

        //单值过滤器2 匹配是否包含子串,大小写不敏感
        SubstringComparator comparator1 = new SubstringComparator("wu");
        new SingleColumnValueFilter("base_info".getBytes(), "name".getBytes(), CompareFilter.CompareOp.EQUAL, comparator1);

        //键值对元数据过滤-----family过滤----字节数组完整匹配
        FamilyFilter ff = new FamilyFilter(CompareFilter.CompareOp.EQUAL,
                new BinaryComparator(Bytes.toBytes("base_info")));//表中不存在inf列族,过滤结果为空

        //键值对元数据过滤-----family过滤----字节数组前缀匹配
        FamilyFilter ff1 = new FamilyFilter(CompareFilter.CompareOp.EQUAL,
                //表中存在以inf打头的列族info,过滤结果为该列族所有行
                new BinaryPrefixComparator(Bytes.toBytes("inf")));

        //键值对元数据过滤-----qualifier过滤----字节数组完整匹配
        QualifierFilter nn = new QualifierFilter(CompareFilter.CompareOp.EQUAL,
                //表中不存在na列,过滤结果为空
                new BinaryComparator(Bytes.toBytes("na")));

        QualifierFilter nn1 = new QualifierFilter(CompareFilter.CompareOp.EQUAL,
                //表中存在以na打头的列name,过滤结果为所有行的该列数据
                new BinaryPrefixComparator("na".getBytes()));

        //基于列名(即Qualifier)前缀过滤数据的ColumnPrefixFilter
        ColumnPrefixFilter cf = new ColumnPrefixFilter("na".getBytes());

        //基于列名(即Qualifier)多个前缀过滤数据的MultipleColumnPrefixFilter
        byte[][] prefixes = new byte[][]{Bytes.toBytes("na"), Bytes.toBytes("me")};
        MultipleColumnPrefixFilter filter1 = new MultipleColumnPrefixFilter(prefixes);


        //为查询设置过滤条件
        scan.setFilter(filter);

        scan.addFamily(Bytes.toBytes("base_info"));
    }

    public void Del() throws IOException {
        HTable table = new HTable(conf, "user");
        Delete del = new Delete("rk001".getBytes());
        del.deleteColumn(Bytes.toBytes("data"), Bytes.toBytes("pic"));
        table.delete(del);
        table.close();
    }

    public void Create() throws IOException {
        HBaseAdmin admin = new HBaseAdmin(conf);
        TableName tableName = TableName.valueOf("person_info");
        HTableDescriptor tabledescripte = new HTableDescriptor(tableName);
        HColumnDescriptor cd = new HColumnDescriptor("base_info");
        cd.setMaxVersions(10);
        tabledescripte.addFamily(cd);
        admin.createTable(tabledescripte);
        admin.close();

    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值