Java 实现 Hbase Scan 按行键范围扫描

数据的设计及更多Hbase查询命令看前一篇Hbase scan扫描大全
数据结构如下

scan 'stu'
ROW                            COLUMN+CELL
 c1_s1                         column=base:name, timestamp=1588153968060, value=jack
 c1_s2                         column=base:name, timestamp=1588153968114, value=jack2
 c1_s3                         column=base:name, timestamp=1588153968207, value=jack3
 c2_s1                         column=base:name, timestamp=1588153968324, value=tom1
 c2_s2                         column=base:name, timestamp=1588153968367, value=tom2
 c2_s2                         column=base:weight, timestamp=1588154167692, value=70kg
 c2_s3                         column=base:height, timestamp=1588154125060, value=1.70m
 c2_s3                         column=base:name, timestamp=1588153968409, value=tom3
 c2_s3                         column=base:weight, timestamp=1588154124202, value=85kg
 c3_s55                        column=base:name, timestamp=1588162870203, value=Lucy
7 row(s)
Took 0.1723 seconds

Hbase Shell Rowkey范围扫描命令

hbase(main):004:0> scan 'stu',{STARTROW=>'c1',STOPROW=>'c2'}
ROW                            COLUMN+CELL
 c1_s1                         column=base:name, timestamp=1588153968060, value=jack
 c1_s2                         column=base:name, timestamp=1588153968114, value=jack2
 c1_s3                         column=base:name, timestamp=1588153968207, value=jack3
3 row(s)
Took 0.0235 seconds

Java 实现Hbase按行键来进行范围扫描

package com.javaer.test;
 
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.BinaryPrefixComparator;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.RowFilter;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
 
 
public class ScanHbase {
    public static Configuration configuration; // 管理Hbase的配置信息
    public static Connection connection; // 管理Hbase连接
    public static Admin admin; // 管理Hbase数据库的信息
 
 
 
    public static void main(String[] args) throws IOException{
        System.out.println("start hbase java-er.com test...");
        init();
        scanRangRow("stu","c1","c2");
 
        System.out.println("read data.........");
 
        close();
    }
    public static void init(){
        Logger.getRootLogger().setLevel(Level.ERROR);
        configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "localhost");
        try{
            connection = ConnectionFactory.createConnection(configuration);
            admin = connection.getAdmin();
        }catch(IOException e){
            e.printStackTrace();
        }
 
    }
    /**
     *区间扫描通过列扫描
     */
    public static ArrayList scanRangRow(String tableName,String Start,String End)  throws IOException{
        Table table = connection.getTable(TableName.valueOf(tableName));
        byte[] startRow = Bytes.toBytes(Start);
        byte[] endRow = Bytes.toBytes(End);
 
        Scan s = new Scan(startRow,endRow);
 
        ResultScanner rs = table.getScanner(s);
 
        ArrayList L = new ArrayList();
        String colFamily = "base";
        String col = "name";
 
        for(Result result:rs){
             System.out.println("--------" + Bytes.toString(result.getRow()));
             System.out.println(new String(result.getValue(colFamily.getBytes(),col==null?null:col.getBytes())));
        }
        table.close();
        return L;
    }
 
 
    // 操作数据库之后,关闭连接
    public static void close(){
        try{
            if(admin!=null){
                admin.close(); // 退出用户
            }
            if(null != connection){
                connection.close(); // 关闭连接
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }
 
 
 
}

这是我用来测试算法的代码

我的实际相应用中的行键是这样的

clickAprSale2020-04-0100001
clickAprSale2020-04-0100002
click 点击, AprSale 四月促销, 2020-04-01 时间, 00001 唯一序列

这样我可以方便的扫描2020-04-01 到 2020-04-13(任意时间)的数据

scanRangRow("myTable","clickAprSale2020-04-01","clickAprSale2020-04-13");


This entry was posted in JAVA and tagged , . Bookmark the permalink.
月小升QQ 2651044202, 技术交流QQ群 178491360
首发地址:月小升博客https://java-er.com/blog/java-scan-hbase-rang-row/
无特殊说明,文章均为月小升原创,欢迎转载,转载请注明本文地址,谢谢
您的评论是我写作的动力.
2020.03.24 评论已经全局关闭,有事加QQ聊天