大数据之hbase 数据读取 GET/SCAN 过滤器使用实战

大数据阿木 发布于 4 天前 1 次阅读


HBase 数据读取(GET/SCAN)过滤器使用实战

HBase 是一个分布式、可伸缩、支持列存储的NoSQL数据库,它建立在Hadoop文件系统之上,提供了对大数据的随机实时读取和写入。在HBase中,数据存储在行、列、版本和时间戳上,这使得它非常适合于非关系型数据的存储。本文将围绕HBase的数据读取操作,特别是GET和SCAN命令,结合过滤器(Filter)的使用进行实战讲解。

HBase 简介

在开始实战之前,我们先简要介绍一下HBase的基本概念:

- 行键(Row Key):HBase中的每行数据都有一个唯一的行键,它是数据检索的主要依据。

- 列族(Column Family):列族是一组列的集合,它们共享相同的存储和访问策略。

- 列(Column):列是列族中的成员,每个列都有一个唯一的列限定符。

- 时间戳(Timestamp):每个单元格可以存储多个版本的数据,每个版本都有一个时间戳。

GET 和 SCAN 操作

在HBase中,读取数据主要有两种方式:GET 和 SCAN。

- GET:用于读取单行数据,需要提供行键。

- SCAN:用于读取多行数据,可以指定起始行键和终止行键,以及过滤条件。

过滤器(Filter)

过滤器是HBase的一个重要特性,它允许用户在读取数据时指定条件,从而只获取满足条件的行或单元格。HBase提供了多种内置的过滤器,包括:

- SingleColumnValueFilter:根据列值过滤。

- PrefixFilter:根据行键的前缀过滤。

- PageFilter:分页过滤器。

- RowFilter:根据行键过滤。

实战案例

以下是一个使用HBase Java API进行数据读取和过滤器的实战案例。

1. 环境准备

确保你已经安装了HBase和Hadoop环境,并且已经启动了HBase服务。

2. 创建表

java

import org.apache.hadoop.hbase.HBaseConfiguration;


import org.apache.hadoop.hbase.TableName;


import org.apache.hadoop.hbase.client.Admin;


import org.apache.hadoop.hbase.client.Connection;


import org.apache.hadoop.hbase.client.ConnectionFactory;


import org.apache.hadoop.hbase.HTableDescriptor;


import org.apache.hadoop.hbase.HColumnDescriptor;

public class HBaseCreateTable {


public static void main(String[] args) throws Exception {


// 创建连接


Connection connection = ConnectionFactory.createConnection(HBaseConfiguration.create());


Admin admin = connection.getAdmin();

// 创建表描述符


HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf("testTable"));


HColumnDescriptor columnFamily = new HColumnDescriptor("cf");


tableDescriptor.addFamily(columnFamily);

// 创建表


admin.createTable(tableDescriptor);


admin.close();


connection.close();


}


}


3. 插入数据

java

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.Put;


import org.apache.hadoop.hbase.client.Table;

public class HBaseInsertData {


public static void main(String[] args) throws Exception {


// 创建连接


Connection connection = ConnectionFactory.createConnection(HBaseConfiguration.create());


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

// 创建Put对象


Put put = new Put("row1".getBytes());


put.addColumn("cf".getBytes(), "col1".getBytes(), "value1".getBytes());


put.addColumn("cf".getBytes(), "col2".getBytes(), "value2".getBytes());

// 插入数据


table.put(put);


table.close();


connection.close();


}


}


4. 使用过滤器读取数据

java

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.Result;


import org.apache.hadoop.hbase.client.ResultScanner;


import org.apache.hadoop.hbase.client.Scan;


import org.apache.hadoop.hbase.client.Table;


import org.apache.hadoop.hbase.filter.Filter;


import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;

public class HBaseReadDataWithFilter {


public static void main(String[] args) throws Exception {


// 创建连接


Connection connection = ConnectionFactory.createConnection(HBaseConfiguration.create());


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

// 创建过滤器


Filter filter = new SingleColumnValueFilter(


"cf".getBytes(),


"col1".getBytes(),


CompareOperator.EQUAL,


new BytesToBytesComparator()


);

// 创建Scan对象


Scan scan = new Scan();


scan.setFilter(filter);

// 执行扫描


ResultScanner scanner = table.getScanner(scan);


for (Result result : scanner) {


System.out.println("Row Key: " + new String(result.getRow()));


for (Cell cell : result.rawCells()) {


System.out.println("Column: " + new String(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength()));


System.out.println("Value: " + new String(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()));


}


}


scanner.close();


table.close();


connection.close();


}


}


5. 使用GET命令读取数据

java

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.Get;


import org.apache.hadoop.hbase.client.Result;


import org.apache.hadoop.hbase.client.Table;

public class HBaseGetRow {


public static void main(String[] args) throws Exception {


// 创建连接


Connection connection = ConnectionFactory.createConnection(HBaseConfiguration.create());


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

// 创建GET命令


Get get = new Get("row1".getBytes());

// 执行GET命令


Result result = table.get(get);


for (Cell cell : result.rawCells()) {


System.out.println("Column: " + new String(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength()));


System.out.println("Value: " + new String(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()));


}


table.close();


connection.close();


}


}


总结

本文通过实战案例介绍了HBase中的GET和SCAN操作,以及如何使用过滤器来读取满足特定条件的数据。通过这些操作,我们可以有效地从HBase中检索所需的数据,这对于大数据处理和分析至关重要。在实际应用中,根据具体需求选择合适的读取方式和过滤器,可以大大提高数据处理的效率和准确性。