HBase篇--HBase操作Api和Java操作Hbase相關Api

LHBlog發表於2018-01-16

一.前述。

Hbase shell啟動命令視窗,然後再Hbase shell中對應的api命令如下。

二.說明

Hbase shell中刪除鍵是空格+Ctrl鍵。

三.程式碼

1.封裝所有的API

package com.sxt.hbase;

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

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.TableName;
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.HTableInterface;
import org.apache.hadoop.hbase.client.HTablePool;
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.PrefixFilter;
import org.apache.hadoop.hbase.filter.RowFilter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.filter.SubstringComparator;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Test;


public class HBaseDAOImp {

    HConnection hTablePool = null;
    static Configuration conf =null;
    public HBaseDAOImp()
    {
         conf = new Configuration();
        String zk_list = "node1,node2,node3";//只指定zookeeper叢集即可,因為在zookeeper中存貯所有Region的定址入口,而客戶端只需要知道需要儲存的具體位置後,
//即可請求對應的regionServer上去寫入資料。
conf.set(
"hbase.zookeeper.quorum", zk_list); try { hTablePool = HConnectionManager.createConnection(conf) ;//申請一個HTablePool可以解決HTable存在的執行緒不安全問題,
// 同時通過維護固定數量的HTable物件,能夠在程式執行期間複用這些HTable資源物件
}
catch (IOException e) { e.printStackTrace(); } } public void save(Put put, String tableName) { // TODO Auto-generated method stub HTableInterface table = null; try { table = hTablePool.getTable(tableName) ; table.put(put) ; } catch (Exception e) { e.printStackTrace() ; }finally{ try { table.close() ; } catch (IOException e) { e.printStackTrace(); } } } /** * 插入一個cell * @param tableName * @param rowKey * @param family * @param quailifer * @param value */ public void insert(String tableName, String rowKey, String family, String quailifer, String value) { // TODO Auto-generated method stub HTableInterface table = null; try { table = hTablePool.getTable(tableName) ;//針對哪張表操作 Put put = new Put(rowKey.getBytes());//增添資料通過Put物件操作,新增一條rowkey put.add(family.getBytes(), quailifer.getBytes(), value.getBytes()) ;//新增哪個列族,列,value值。 table.put(put);//放置到hbase的物件中去。 } catch (Exception e) { e.printStackTrace(); }finally { try { table.close() ; } catch (IOException e) { e.printStackTrace(); } } } /** * 在一個列族下插入多個單元格 * @param tableName * @param rowKey * @param family * @param quailifer * @param value */ public void insert(String tableName,String rowKey,String family,String quailifer[],String value[]) { HTableInterface table = null; try { table = hTablePool.getTable(tableName) ; Put put = new Put(rowKey.getBytes()); // 批量新增 for (int i = 0; i < quailifer.length; i++) { String col = quailifer[i]; String val = value[i]; put.add(family.getBytes(), col.getBytes(), val.getBytes()); } table.put(put); } catch (Exception e) { e.printStackTrace(); }finally { try { table.close() ; } catch (IOException e) { e.printStackTrace(); } } } public void save(List<Put> Put, String tableName) { // TODO Auto-generated method stub HTableInterface table = null; try { table = hTablePool.getTable(tableName) ; table.put(Put) ; } catch (Exception e) { // TODO: handle exception }finally { try { table.close() ; } catch (IOException e) { e.printStackTrace(); } } } public Result getOneRow(String tableName, String rowKey) { // TODO Auto-generated method stub HTableInterface table = null; Result rsResult = null; try { table = hTablePool.getTable(tableName) ; Get get = new Get(rowKey.getBytes()) ;/通過get獲取一條資料 rsResult = table.get(get) ;//返回一個result物件 } catch (Exception e) { e.printStackTrace() ; } finally { try { table.close() ; } catch (IOException e) { e.printStackTrace(); } } return rsResult; } /** * 最常用的方法,優化查詢 * 查詢一行資料, * @param tableName * @param rowKey * @param cols * @return */ public Result getOneRowAndMultiColumn(String tableName, String rowKey,String[] cols) { // TODO Auto-generated method stub HTableInterface table = null; Result rsResult = null; try { table = hTablePool.getTable(tableName) ; Get get = new Get(rowKey.getBytes()) ; for (int i = 0; i < cols.length; i++) { get.addColumn("cf".getBytes(), cols[i].getBytes()) ; } rsResult = table.get(get) ; } catch (Exception e) { e.printStackTrace() ; } finally { try { table.close() ; } catch (IOException e) { e.printStackTrace(); } } return rsResult; } public List<Result> getRows(String tableName, String rowKeyLike) { // TODO Auto-generated method stub HTableInterface table = null; List<Result> list = null; try { FilterList fl = new FilterList(FilterList.Operator.MUST_PASS_ALL); table = hTablePool.getTable(tableName) ; PrefixFilter filter = new PrefixFilter(rowKeyLike.getBytes()); SingleColumnValueFilter filter1 = new SingleColumnValueFilter( "order".getBytes(), "order_type".getBytes(), CompareOp.EQUAL, Bytes.toBytes("1") ); fl.addFilter(filter); fl.addFilter(filter1); Scan scan = new Scan(); scan.setFilter(fl); ResultScanner scanner = table.getScanner(scan) ; list = new ArrayList<Result>() ; for (Result rs : scanner) { list.add(rs) ; } } catch (Exception e) { e.printStackTrace() ; } finally { try { table.close() ; } catch (IOException e) { e.printStackTrace(); } } return list; } public List<Result> getRows(String tableName, String rowKeyLike ,String cols[]) { // TODO Auto-generated method stub HTableInterface table = null; List<Result> list = null; try { table = hTablePool.getTable(tableName) ; PrefixFilter filter = new PrefixFilter(rowKeyLike.getBytes()); Scan scan = new Scan(); for (int i = 0; i < cols.length; i++) { scan.addColumn("cf".getBytes(), cols[i].getBytes()) ; } scan.setFilter(filter); ResultScanner scanner = table.getScanner(scan) ; list = new ArrayList<Result>() ; for (Result rs : scanner) { list.add(rs) ; } } catch (Exception e) { e.printStackTrace() ; } finally { try { table.close() ; } catch (IOException e) { e.printStackTrace(); } } return list; } public List<Result> getRowsByOneKey(String tableName, String rowKeyLike ,String cols[]) { // TODO Auto-generated method stub HTableInterface table = null; List<Result> list = null; try { table = hTablePool.getTable(tableName) ; PrefixFilter filter = new PrefixFilter(rowKeyLike.getBytes()); Scan scan = new Scan(); for (int i = 0; i < cols.length; i++) { scan.addColumn("cf".getBytes(), cols[i].getBytes()) ; } scan.setFilter(filter); ResultScanner scanner = table.getScanner(scan) ; list = new ArrayList<Result>() ; for (Result rs : scanner) { list.add(rs) ; } } catch (Exception e) { e.printStackTrace() ; } finally { try { table.close() ; } catch (IOException e) { e.printStackTrace(); } } return list; } /** * 範圍查詢 * @param tableName * @param startRow * @param stopRow * @return */ public List<Result> getRows(String tableName,String startRow,String stopRow) { HTableInterface table = null; List<Result> list = null; try { table = hTablePool.getTable(tableName) ; Scan scan = new Scan() ; scan.setStartRow(startRow.getBytes()) ; scan.setStopRow(stopRow.getBytes()) ; ResultScanner scanner = table.getScanner(scan) ; list = new ArrayList<Result>() ; for (Result rsResult : scanner) { list.add(rsResult) ; } }catch (Exception e) { e.printStackTrace() ; } finally { try { table.close() ; } catch (IOException e) { e.printStackTrace(); } } return list; } public void deleteRecords(String tableName, String rowKeyLike){ HTableInterface table = null; try { table = hTablePool.getTable(tableName) ; PrefixFilter filter = new PrefixFilter(rowKeyLike.getBytes()); Scan scan = new Scan(); scan.setFilter(filter); ResultScanner scanner = table.getScanner(scan) ; List<Delete> list = new ArrayList<Delete>() ; for (Result rs : scanner) { Delete del = new Delete(rs.getRow()); list.add(del) ; } table.delete(list); } catch (Exception e) { e.printStackTrace() ; } finally { try { table.close() ; } catch (IOException e) { e.printStackTrace(); } } } public void deleteCell(String tableName, String rowkey,String cf,String column){ HTableInterface table = null; try { table = hTablePool.getTable(tableName) ; Delete del = new Delete(rowkey.getBytes()); del.deleteColumn(cf.getBytes(), column.getBytes()); table.delete(del); } catch (Exception e) { e.printStackTrace() ; } finally { try { table.close() ; } catch (IOException e) { e.printStackTrace(); } } } public void createTable(String tableName, String[] columnFamilys){ try { // admin 物件 HBaseAdmin admin = new HBaseAdmin(conf); if (admin.tableExists(tableName)) { System.err.println("此表,已存在!"); } else { HTableDescriptor tableDesc = new HTableDescriptor( TableName.valueOf(tableName)); for (String columnFamily : columnFamilys) { tableDesc.addFamily(new HColumnDescriptor(columnFamily)); } admin.createTable(tableDesc); System.err.println("建表成功!"); } admin.close();// 關閉釋放資源 } catch (MasterNotRunningException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ZooKeeperConnectionException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 刪除一個表 * * @param tableName * 刪除的表名 * */ public void deleteTable(String tableName) { try { HBaseAdmin admin = new HBaseAdmin(conf); if (admin.tableExists(tableName)) { admin.disableTable(tableName);// 禁用表 admin.deleteTable(tableName);// 刪除表 System.err.println("刪除表成功!"); } else { System.err.println("刪除的表不存在!"); } admin.close(); } catch (MasterNotRunningException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ZooKeeperConnectionException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 查詢表中所有行 * @param tablename */ public void scaner(String tablename) { try { HTable table =new HTable(conf, tablename); Scan s =new Scan(); // s.addColumn(family, qualifier) // s.addColumn(family, qualifier) ResultScanner rs = table.getScanner(s); for (Result r : rs) { for(Cell cell:r.rawCells()){ System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" "); System.out.println("Timetamp:"+cell.getTimestamp()+" "); System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" "); System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" "); System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" "); } } } catch (IOException e) { e.printStackTrace(); } } public void scanerByColumn(String tablename) { try { HTable table =new HTable(conf, tablename); Scan s =new Scan(); s.addColumn("cf".getBytes(), "201504052237".getBytes()); s.addColumn("cf".getBytes(), "201504052237".getBytes()); ResultScanner rs = table.getScanner(s); for (Result r : rs) { for(Cell cell:r.rawCells()){ System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" "); System.out.println("Timetamp:"+cell.getTimestamp()+" "); System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" "); System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" "); System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" "); } } } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { // 建立表 // String tableName="test"; // String cfs[] = {"cf"}; // dao.createTable(tableName,cfs); // 存入一條資料 // Put put = new Put("bjsxt".getBytes()); // put.add("cf".getBytes(), "name".getBytes(), "cai10".getBytes()) ; // dao.save(put, "test") ; // 插入多列資料 // Put put = new Put("bjsxt".getBytes()); // List<Put> list = new ArrayList<Put>(); // put.add("cf".getBytes(), "addr".getBytes(), "shanghai1".getBytes()) ; // put.add("cf".getBytes(), "age".getBytes(), "30".getBytes()) ; // put.add("cf".getBytes(), "tel".getBytes(), "13889891818".getBytes()) ; // list.add(put) ; // dao.save(list, "test"); // 插入單行資料 // dao.insert("test", "testrow", "cf", "age", "35") ; // dao.insert("test", "testrow", "cf", "cardid", "12312312335") ; // dao.insert("test", "testrow", "cf", "tel", "13512312345") ; // List<Result> list = dao.getRows("test", "testrow",new String[]{"age"}) ; // for(Result rs : list) // { // for(Cell cell:rs.rawCells()){ // System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" "); // System.out.println("Timetamp:"+cell.getTimestamp()+" "); // System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" "); // System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" "); // System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" "); // } // } // Result rs = dao.getOneRow("test", "testrow"); // System.out.println(new String(rs.getValue("cf".getBytes(), "age".getBytes()))); // Result rs = dao.getOneRowAndMultiColumn("cell_monitor_table", "29448-513332015-04-05", new String[]{"201504052236","201504052237"}); // for(Cell cell:rs.rawCells()){ // System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" "); // System.out.println("Timetamp:"+cell.getTimestamp()+" "); // System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" "); // System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" "); // System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" "); // } // dao.deleteTable("cell_monitor_table"); // 建立表 String tableName="cell_monitor_table"; String cfs[] = {"cf"}; // dao.createTable(tableName,cfs); } public static void testRowFilter(String tableName){ try { HTable table =new HTable(conf, tableName); Scan scan = new Scan(); scan.addColumn(Bytes.toBytes("column1"),Bytes.toBytes("qqqq")); Filter filter1 = new RowFilter(CompareOp.LESS_OR_EQUAL,new BinaryComparator(Bytes.toBytes("laoxia157"))); scan.setFilter(filter1); ResultScanner scanner1 = table.getScanner(scan); for (Result res : scanner1) { System.out.println(res); } scanner1.close(); // // Filter filter2 = new RowFilter(CompareFilter.CompareOp.EQUAL,new RegexStringComparator("laoxia4\\d{2}")); // scan.setFilter(filter2); // ResultScanner scanner2 = table.getScanner(scan); // for (Result res : scanner2) { // System.out.println(res); // } // scanner2.close(); Filter filter3 = new RowFilter( CompareOp.EQUAL,new SubstringComparator("laoxia407")); scan.setFilter(filter3); ResultScanner scanner3 = table.getScanner(scan); for (Result res : scanner3) { System.out.println(res); } scanner3.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Test public void testTrasaction(){ try{ HTableInterface table = null; table = hTablePool.getTable("t_test".getBytes()); // Put put1 =new Put("002".getBytes()); // put1.add("cf1".getBytes(), "name".getBytes(), "王五".getBytes()); // table.put(put1); Put newput =new Put("001".getBytes()); newput.add("cf1".getBytes(), "like".getBytes(), "看書".getBytes()); boolean f= table.checkAndPut("001".getBytes(), "cf1".getBytes(), "age".getBytes(), "24".getBytes(), newput); System.out.println(f); }catch (Exception e){ e.printStackTrace(); } } }

 2.原始Api

package com.sxt.hbase;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
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.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.PrefixFilter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class HBaseDemo {

    Configuration conf;
    HBaseAdmin admin;
    HTable htable;
    byte[] family = "cf".getBytes();
    
    // 1、刪除 cell??
    // 2、表設計
    
    
    String TN = "phone";
    
    @Before
    public void begin() throws Exception {
        conf = new Configuration();
        
        // 分散式hbase  zk列表指定zk叢集
        conf.set("hbase.zookeeper.quorum", "node05");
        
        admin = new HBaseAdmin(conf);//通過admin物件操作DDL語言
        htable = new HTable(conf, TN);//通過Htable物件操作表DML語言
    }
    
    @After
    public void end() throws Exception {
        if(admin != null) {
            admin.close();
        }
        if(htable != null) {
            htable.close();
        }
    }

    @Test
    public void createTbl() throws Exception {
        if(admin.tableExists(TN)) {
            admin.disableTable(TN);
            admin.deleteTable(TN);
        }
        
        HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(TN));//表的描述

        HColumnDescriptor cf = new HColumnDescriptor("cf");
        cf.setInMemory(true);//設定讀快取
        cf.setMaxVersions(1);
        
        desc.addFamily(cf);//建立表的時候必須制定列族,相當於一個DDL語言描述。
        
        admin.createTable(desc);
    }
    
    @Test
    public void insertDB1() throws Exception {
        String rowkey = "123";
        
        Put put = new Put(rowkey.getBytes());
        put.add("cf".getBytes(), "name".getBytes(), "xiaoming".getBytes());
        put.add("cf".getBytes(), "sex".getBytes(), "man".getBytes());
        
        htable.put(put);
    }
    
    @Test
    public void getDB1() throws Exception {
        String rowkey = "123";
        Get get = new Get(rowkey.getBytes());
        get.addColumn("cf".getBytes(), "name".getBytes());
        
        Result rs = htable.get(get);
        Cell cell = rs.getColumnLatestCell("cf".getBytes(), "name".getBytes());//result返回的是一個Cell物件
        
        System.out.println(new String(CellUtil.cloneValue(cell)));//取出Cell物件中的值
    }
    
    /**
     * 通話詳單 
     * 包含:手機號,對方手機號,日期,通話時長,主叫被叫型別...
     * 
     * Rowkey設計:手機號_(Long.Max-時間戳)
     * 
     * 1、查詢某個月份 的  所有的通話詳單  時間降序
     * 
     * 2、查詢某個手機號   所有主叫型別   通話記錄
     * @throws Exception
     */
    
    HTools t = new HTools();
    
    Random r = new Random();

    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    
    /**
     * 生成測試資料
     * 
     * 十個使用者 產生一百條通話記錄 
     */
    @Test
    public void insertDB2() throws Exception {
        
        
        List<Put> puts = new ArrayList<Put>();
        
        for (int i = 0; i < 10; i++) {
            String pnum = t.getPhoneNum("186");
            
            for (int j = 0; j < 100; j++) {
                String dnum = t.getPhoneNum("177");
                String datestr = t.getDate("2018");
                String length = r.nextInt(99) + "";
                String type = r.nextInt(2) + "";
                
                String rowkey = pnum + "_" + (Long.MAX_VALUE-sdf.parse(datestr).getTime());//預設Hbase是按照row_key的字典升序排列,此處降序。
                
                Put put = new Put(rowkey.getBytes());

                put.add(family, "dnum".getBytes(), dnum.getBytes());
                put.add(family, "date".getBytes(), datestr.getBytes());
                put.add(family, "length".getBytes(), length.getBytes());
                put.add(family, "type".getBytes(), type.getBytes());
                
                puts.add(put);
            }
        }
        
        htable.put(puts);
    }
    
    /**
     * 查詢某個手機號  某個月份所有的通話記錄
     * 範圍
     * @throws Exception
     */
    @Test
    public void scanDB1() throws Exception {
        Scan scan = new Scan();
        
        String pnum = "18692739289_";

        String startRowkey = pnum + (Long.MAX_VALUE-sdf.parse("20181001000000").getTime());
        String stopRowkey = pnum + (Long.MAX_VALUE-sdf.parse("20180901000000").getTime());
        
        scan.setStartRow(startRowkey.getBytes());
        scan.setStopRow(stopRowkey.getBytes());//scan操作設定起始和結束的rowkey
        
        ResultScanner rss = htable.getScanner(scan);//返回一個Result集合。
        for (Result rs : rss) {//遍歷Result集合
            System.out.print(new String(CellUtil.cloneValue(rs.getColumnLatestCell(family, "dnum".getBytes()))));//得到一條條資料。
            System.out.print(" - " + new String(CellUtil.cloneValue(rs.getColumnLatestCell(family, "date".getBytes()))));
            System.out.print(" - " + new String(CellUtil.cloneValue(rs.getColumnLatestCell(family, "type".getBytes()))));
            System.out.println(" - " + new String(CellUtil.cloneValue(rs.getColumnLatestCell(family, "length".getBytes()))));
        }
    }
    
    /**
     * 查詢某個手機號  所有的主叫type=1
     * 過濾器
     * @throws Exception 
     */
    @Test
    public void scanDB2() throws Exception {
        Scan scan = new Scan();
        
        FilterList list = new FilterList(FilterList.Operator.MUST_PASS_ALL);//定義多個過濾器,必須全部篩選
        
        PrefixFilter filter1 = new PrefixFilter("18692739289".getBytes());//字首過濾器,先返回所有以這個判定條件的rowkey
        list.addFilter(filter1);
        
        SingleColumnValueFilter filter2 = new SingleColumnValueFilter(family, //列值過濾器,因為是兩個查詢條件,所以需要兩個過濾器
                "type".getBytes(), CompareOp.EQUAL, "1".getBytes()); //判斷所有type等於1的值
        
        list.addFilter(filter2);//加第二個過濾器
        
        scan.setFilter(list);
        
        ResultScanner rss = htable.getScanner(scan);
        for (Result rs : rss) {
            System.out.print(new String(CellUtil.cloneValue(rs.getColumnLatestCell(family, "dnum".getBytes()))));
            System.out.print(" - " + new String(CellUtil.cloneValue(rs.getColumnLatestCell(family, "date".getBytes()))));
            System.out.print(" - " + new String(CellUtil.cloneValue(rs.getColumnLatestCell(family, "type".getBytes()))));
            System.out.println(" - " + new String(CellUtil.cloneValue(rs.getColumnLatestCell(family, "length".getBytes()))));
        }
        
    }
    
}

 解析:

1.

String rowkey = pnum + "_" + (Long.MAX_VALUE-sdf.parse(datestr).getTime());//預設Hbase是按照row_key的字典升序排列,此處降序。時間戳越來越大,則此數越來越小,達到按照時間降序的結果。

2.過濾器介紹

 

FilterList代表一個過濾器列表
        FilterList.Operator.MUST_PASS_ALL --> 取交集 相當一and操作
        FilterList.Operator.MUST_PASS_ONE --> 取並集 相當於or 操作
        FilterList list = new FilterList(FilterList.Operator.MUST_PASS_ONE);
        2、SingleColumnValueFilter 列值過濾器
        ColumnPrefixFilter用於指定列名字首值相等
        MultipleColumnPrefixFilter和ColumnPrefixFilter行為差不多,但可以指定多個字首。
        QualifierFilter是基於列名的過濾器。
        2、RowFilter 行過濾器
        RegexStringComparator是支援正規表示式的比較器。
        SubstringComparator用於檢測一個子串是否存在於值中,大小寫不敏感。

 

相關文章