• 073 HBASE的读写以及client API


    一:读写思想

    1.系统表

      hbase:namespace

        存储hbase中所有的namespace的信息

      hbase:meta   

        rowkey:hbase中所有表的region的名称
        column:regioninfo:region的名称,region的范围
        server:该region在哪台regionserver上

    2.读写流程

      tbname,rowkey  ->   region  ->  regionserver  ->  store ->  storefile

      但是这些都是加载过meta表之后,然后meta表如何寻找?

    3.读的流程  

      -》根据表名和rowkey找到对应的region
      -》zookeeper中存储了meta表的region信息
      -》从meta表中获取相应的region的信息
      -》找到对应的regionserver
      -》查找对应的region
      -》读memstore
      -》storefile

    4.写的流程  

      -》根据表名和rowkey找到对应的region
      -》zookeeper中存储了meta表的region信息
      -》从meta表中获取相应的region的信息
      -》找到对应的regionserver
      -》正常情况
      -》WAL(write ahead log预写日志),一个regionserver维护一个hlog
      -》memstore (达到一定大小,flush到磁盘)
      -》当多个storefile达到一定大小以后,会进行compact,合并成一个storefile
      -》当单个storefile达到一定大小以后,会进行split操作,等分割region

    5.注意点

      关于版本的合并和删除是在compact阶段完成的。hbase只负责数据的增加存储
      hmaster短暂的不参与实际的读写

    二:HBase Client API 的书写

    1.添加依赖

      

    2.添加配置文件

      core-site.xml

      hdfs-site.xml

      hbase-site.xml

      log4j.properties

      regionservers

    3.get的书写

      

    4.put的书写

      

     5.delete的书写

      

      注意全部删除:

      

     

    6.scan的书写

      

    7.过滤条件的scan的书写

                                                                                          

    三:复制源代码

      

      1 package com.beifeng.bigdat;
      2 
      3 import java.io.IOException;
      4 
      5 import org.apache.hadoop.conf.Configuration;
      6 import org.apache.hadoop.hbase.Cell;
      7 import org.apache.hadoop.hbase.CellUtil;
      8 import org.apache.hadoop.hbase.HBaseConfiguration;
      9 import org.apache.hadoop.hbase.client.Delete;
     10 import org.apache.hadoop.hbase.client.Get;
     11 import org.apache.hadoop.hbase.client.HTable;
     12 import org.apache.hadoop.hbase.client.Put;
     13 import org.apache.hadoop.hbase.client.Result;
     14 import org.apache.hadoop.hbase.client.ResultScanner;
     15 import org.apache.hadoop.hbase.client.Scan;
     16 import org.apache.hadoop.hbase.filter.Filter;
     17 import org.apache.hadoop.hbase.filter.PrefixFilter;
     18 import org.apache.hadoop.hbase.util.Bytes;
     19 
     20 public class HbaseClientTest {
     21     public static HTable getTable(String name) throws Exception{
     22         Configuration conf=HBaseConfiguration.create();
     23         HTable table=new HTable(conf,name); 
     24         return table;
     25         
     26     }
     27     public static void getData(HTable table) throws Exception{
     28         Get get=new Get(Bytes.toBytes("103"));
     29         get.addFamily(Bytes.toBytes("info"));
     30         Result rs=table.get(get);
     31         for(Cell cell:rs.rawCells()){
     32             System.out.println(
     33                     Bytes.toString(CellUtil.cloneFamily(cell))+"--"+
     34                     Bytes.toString(CellUtil.cloneQualifier(cell))+"---"+
     35                     Bytes.toString(CellUtil.cloneValue(cell))+"----"+
     36                     cell.getTimestamp()
     37                     );
     38             System.out.println("----------------------------------------------");
     39         }
     40     }
     41     
     42     public static void putData(HTable table) throws Exception{
     43         Put put=new Put(Bytes.toBytes("103"));
     44         put.add(Bytes.toBytes("info"),
     45                 Bytes.toBytes("name"),
     46                 Bytes.toBytes("zhaoliu"));
     47         table.put(put);
     48         getData(table);
     49     }
     50     
     51     public static void deleteData(HTable table) throws Exception{
     52         Delete delete =new Delete(Bytes.toBytes("103"));
     53         delete.deleteColumns(Bytes.toBytes("info"), Bytes.toBytes("name"));
     54         table.delete(delete);
     55         getData(table);
     56     }
     57     
     58     public static void scanData(HTable table) throws Exception{
     59         Scan scan =new Scan();
     60         ResultScanner rs=table.getScanner(scan);
     61         for(Result r:rs){
     62             System.out.println(Bytes.toString(r.getRow()));
     63             for(Cell cell:r.rawCells()){
     64                 System.out.println(
     65                         Bytes.toString(CellUtil.cloneFamily(cell))+"---"+
     66                         Bytes.toString(CellUtil.cloneQualifier(cell))+"---"+
     67                         Bytes.toString(CellUtil.cloneValue(cell))+"--"+
     68                         cell.getTimestamp()
     69                     );
     70             System.out.println();
     71             }
     72         }
     73     }
     74     
     75     public static void filterScan(HTable table) throws Exception{
     76         Scan scan =new Scan();
     77         Filter filter=new PrefixFilter(Bytes.toBytes("10"));
     78         scan.setFilter(filter);
     79         scan.setCacheBlocks(true);
     80         scan.setCaching(1000);
     81         scan.setBatch(100);
     82         ResultScanner rs=table.getScanner(scan);
     83         for(Result r:rs){
     84             System.out.println(Bytes.toString(r.getRow()));
     85             for(Cell cell:r.rawCells()){
     86                 System.out.println(
     87                         Bytes.toString(CellUtil.cloneFamily(cell))+"---"+
     88                         Bytes.toString(CellUtil.cloneQualifier(cell))+"---"+
     89                         Bytes.toString(CellUtil.cloneValue(cell))+"--"+
     90                         cell.getTimestamp()
     91                     );
     92             System.out.println();
     93             }
     94         }
     95         
     96     }
     97     
     98     public static void main(String[] args) throws Exception {
     99         HTable table=getTable("nstest1:tb1");
    100         //getData(table);
    101         //putData(table);
    102         //deleteData(table);
    103         //scanData(table);
    104         filterScan(table);
    105     }
    106 
    107     
    108 
    109 }
  • 相关阅读:
    Spring Boot四:配置文件详解properties
    程序员常去的14个顶级开发社区
    Spring Boot二:创建第一个web工程 hello world
    Java架构工程师知识图
    Java高效开发12个精品库
    华为员工:表面光鲜 工作十年买不起房
    如果你是一个程序员,又要踏足人工智能领域,应该要知道哪些语言
    H5表单提交上传图片
    转:正则表达式
    转载:jsp九大内置对象和四大作用域
  • 原文地址:https://www.cnblogs.com/juncaoit/p/6146160.html
Copyright © 2020-2023  润新知