hbaseAPI 常用方法 、 常用过滤器 、过滤比较器与运算符 、代码示例

1.获取连接

//创建配置文件对象
Configuration conf = HBaseConfiguration.create();
//设置连接zookeeper
conf.set("hbase.zookeeper.quorum", "hadoop01:2181,hadoop02:2181,hadoop03:2181");

2.创建连接

Connection connection = ConnectionFactory.createConnection(conf);

3.获取 表名 和连接表

TableName tableName = TableName.valueOf("hbase_race");
Table table = connection.getTable(tableName);

4.获取 管理员身份

Admin admin = connection.getAdmin();
使用admin 进行表操作
方法含义
boolean tableExists(TableName tableName)判断表是否存在
void createTable(TableName tableName)创建表
void enableTable(TableName tableName)禁用表
boolean isTableEnabled(TableName tableName)判断表是否启用
boolean isTableAvailable(TableName tableName)判断表是否可用
void addColumnFamily(TableName tableName,ColumnFamilyDescriptor columnFamily)往指定的表中添加列族
void deleteColumn(TableName tableName,byte[] columnFamily)删除指定的表中的列
void deleteColumnFamily(TableName tableName,byte[] columnFamily)删除指定的表中的列族
使用Delete删除表
/**
 * 根据 rowkey 删除 数据
 */
@Test
public void deleteByRowKey() throws IOException {
    //获取连接
    Configuration conf = HBaseConfiguration.create();
    conf.set("hbase.zookeeper.quorum", "hadoop01:2181,hadoop02:2181,hadoop03:2181");
    Connection connection = ConnectionFactory.createConnection(conf);
    //连接表
    TableName myUser = TableName.valueOf("myUser");
    Table table = connection.getTable(myUser);
    Delete delete = new Delete("0001".getBytes());
    table.delete(delete);
    table.close();
}
使用get 根据rowkey 查询
	/**
     * 查询数据,按照rowkey进行查询
     */
    @Test
    public void searchData() throws IOException {
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "hadoop01:2181,hadoop02:2181,hadoop03:2181");
        Connection connection = ConnectionFactory.createConnection(configuration);
        Table myuser = connection.getTable(TableName.valueOf("myUser"));

        Get get = new Get("0003".getBytes());
//        get.addColumn("f1".getBytes(), "id".getBytes());

        //result 是一行数据
        Result result = myuser.get(get);

        byte[] row = result.getRow();
        System.out.println("row = " + Bytes.toString(row));

        //遍历一行数据的所有的列
        Cell[] cells = result.rawCells();
        //获取所有的列名称以及列的值
        for (Cell cell : cells) {
            //注意,如果列属性是int类型,那么这里就不会显示
            if ("id".equals(Bytes.toString(CellUtil.cloneQualifier(cell))) || "age".equals(Bytes.toString(CellUtil.cloneQualifier(cell)))) {
                String s1 = Bytes.toString(CellUtil.cloneQualifier(cell));
                int i2 = Bytes.toInt(CellUtil.cloneValue(cell));
                System.out.println("s1 = " + s1);
                System.out.println("i2 = " + i2);
            } else {
                String s1 = Bytes.toString(CellUtil.cloneQualifier(cell));
                String s2 = Bytes.toString(CellUtil.cloneValue(cell));
                System.out.println("s1 = " + s1);
                System.out.println("s2 = " + s2);
            }
        }
        myuser.close();
    }
使用admin 操作命名空间
方法含义
namespaceDescriptor getNamespaceDescriptor(String namespace)获取命名空间
void createNamespace(NamespaceDescriptor namespace)创建命名空间
void deleteNamespace(String namespace)删除命名空间
释放资源
方法含义
void close()释放资源
代码示例
    /**
     * 创建命名空间 hbase_result
     * 并在命名空间下创建一个 test01 表
     * 表中有 info 这个列族
     */
    @Test
    public void testNamespace() throws Exception {
        //获取连接
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "hadoop01:2181,hadoop02:2181,hadoop03:2181");
        Connection connection = ConnectionFactory.createConnection(conf);
        //获取admin
        Admin admin = connection.getAdmin();


        try {
            //获取命名空间看是否存在,不存在会直接抛 NamespaceNotFoundException 异常  使用 try catch 捕捉
            NamespaceDescriptor rel = admin.getNamespaceDescriptor("hbase_result");
        } catch (NamespaceNotFoundException e) {
            // 不存在创建一个命名空间
            NamespaceDescriptor namespace = NamespaceDescriptor.create("hbase_result").build();
            admin.createNamespace(namespace);
        }

        //创建表和列族
        TableName tableName = TableName.valueOf("hbase_result:test01");
        HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
        hTableDescriptor.addFamily(new HColumnDescriptor("info"));
        //判断表是否存在
        boolean exists = admin.tableExists(tableName);
        //创建表
        if (!exists) {
            admin.createTable(hTableDescriptor);
        }


        //删除命名空间  命名空间中不能有表
//        admin.deleteNamespace("hbase_result");

        //释放资源
        admin.close();
        connection.close();
    }

5. put插入数据 部分示例

//获取表
Table myuser = connection.getTable(TableName.valueOf("myUser"));
//创建put对象,并指定rowkey
Put put = new Put("0001".getBytes());
put.addColumn("f1".getBytes(), "id".getBytes(), Bytes.toBytes(1));
//插入数据
myuser.put(put);

6.scan 全表扫描

方法含义
setStartRow(byte[] StartRowKey)从第几个rowkey开始扫描
setStopRow(byte[] StartRowKey)到第几个rowkey结束
setFilter(Filter filter)设置过滤器
setMaxVersions(int maxVersion)指定最大的版本个数。
setTimeRange(Long min,Long max)指定最大的时间戳和最小的时间戳
setTimeStamp(Long long)指定时间戳
setBatch(int batch)指定最多返回的Cell数目

7.Filter 过滤器

过滤器含义
FilterList过滤器列表,用于使用多个过滤器时
SingleColumnValueFilter单列值过滤器,用于过滤单列的值
PrefixFilterrowkey前缀过滤器PrefixFilter,如果包含前缀,返回结果
ColumnPrefixFilter单列前缀过滤器,判断当前列是否包含某个前缀,是这返回匹配列的值
MultipleColumnPrefixFilter多列过滤器,但可以指定包含多个列的前缀,返回匹配值
QualifierFilter列过滤器
RowFilter行键过滤器用于过滤rowkey
FamilyFilter列族过滤器
SingleColumnValueExcludeFilter单列值排除过滤器
pageFilter实现分页过滤器
valueFilter列值过滤器

8.Hbase中的运算符

// CompareFilter.CompareOp.LESS_OR_EQUAL
LESS				<
LESS_OR_EQUAL		<=
EQUAL				=
NOT_EQUAL			<>
GREATER_OR_EQUAL	>=
GREATER				>
NO_OP               排除所有

9.Hbase 过滤器的比较器

BinaryComparator(byte[])    按字节索引顺序比较指定直接数组,采用 Bytes.commpareTo(byte[]) BinaryPrefixComparator(byte [])  跟前面相同,只比较左端的数据是否相同
NullComparator  				按位比较
RegexStringComparator       提供一个正则的比较器 仅支持 EQUAL 和 NOT_EQUAL
SubStringComparator         判断提供的字串是否出现在value中  相当于 contains() 方法
代码示例
	/**
     * 多过滤器组合使用
     */
    @Test
    public void manyFilter() throws IOException {
        //获取连接
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "hadoop01:2181,hadoop02:2181,hadoop03:2181");
        Connection connection = ConnectionFactory.createConnection(conf);
        //连接表
        TableName myUser = TableName.valueOf("myUser");
        Table table = connection.getTable(myUser);
        Scan scan = new Scan();

        //创建多条件过滤器对象
        FilterList filterList = new FilterList();
        SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter("f1".getBytes(), "name".getBytes(), CompareFilter.CompareOp.EQUAL, "刘备".getBytes());
        PrefixFilter prefixFilter = new PrefixFilter("00".getBytes());
        //把过滤器添加到 多条件过滤器 对象中
        filterList.addFilter(singleColumnValueFilter);
        filterList.addFilter(prefixFilter);
        //设置过滤器
        scan.setFilter(filterList);
        //拿到数据
        ResultScanner scanner = table.getScanner(scan);
        //遍历
        for (Result result : scanner) {
            //获取rowkey
            System.out.println(Bytes.toString(result.getRow()));
            //指定列族以及列打印列当中的数据出来
            System.out.println(Bytes.toString(result.getValue("f1".getBytes(), "name".getBytes())));
        }
        table.close();
    }

10.结果集 ResultScanner

方法含义
result.getValue(byte[] family, byte[] qualifier)获取值
result.getRow()获取rowkey
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值