一:读写思想

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的书写

  

三:复制源代码

  

 package com.beifeng.bigdat;

 import java.io.IOException;

 import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
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.Filter;
import org.apache.hadoop.hbase.filter.PrefixFilter;
import org.apache.hadoop.hbase.util.Bytes; public class HbaseClientTest {
public static HTable getTable(String name) throws Exception{
Configuration conf=HBaseConfiguration.create();
HTable table=new HTable(conf,name);
return table; }
public static void getData(HTable table) throws Exception{
Get get=new Get(Bytes.toBytes("103"));
get.addFamily(Bytes.toBytes("info"));
Result rs=table.get(get);
for(Cell cell:rs.rawCells()){
System.out.println(
Bytes.toString(CellUtil.cloneFamily(cell))+"--"+
Bytes.toString(CellUtil.cloneQualifier(cell))+"---"+
Bytes.toString(CellUtil.cloneValue(cell))+"----"+
cell.getTimestamp()
);
System.out.println("----------------------------------------------");
}
} public static void putData(HTable table) throws Exception{
Put put=new Put(Bytes.toBytes("103"));
put.add(Bytes.toBytes("info"),
Bytes.toBytes("name"),
Bytes.toBytes("zhaoliu"));
table.put(put);
getData(table);
} public static void deleteData(HTable table) throws Exception{
Delete delete =new Delete(Bytes.toBytes("103"));
delete.deleteColumns(Bytes.toBytes("info"), Bytes.toBytes("name"));
table.delete(delete);
getData(table);
} public static void scanData(HTable table) throws Exception{
Scan scan =new Scan();
ResultScanner rs=table.getScanner(scan);
for(Result r:rs){
System.out.println(Bytes.toString(r.getRow()));
for(Cell cell:r.rawCells()){
System.out.println(
Bytes.toString(CellUtil.cloneFamily(cell))+"---"+
Bytes.toString(CellUtil.cloneQualifier(cell))+"---"+
Bytes.toString(CellUtil.cloneValue(cell))+"--"+
cell.getTimestamp()
);
System.out.println();
}
}
} public static void filterScan(HTable table) throws Exception{
Scan scan =new Scan();
Filter filter=new PrefixFilter(Bytes.toBytes("10"));
scan.setFilter(filter);
scan.setCacheBlocks(true);
scan.setCaching(1000);
scan.setBatch(100);
ResultScanner rs=table.getScanner(scan);
for(Result r:rs){
System.out.println(Bytes.toString(r.getRow()));
for(Cell cell:r.rawCells()){
System.out.println(
Bytes.toString(CellUtil.cloneFamily(cell))+"---"+
Bytes.toString(CellUtil.cloneQualifier(cell))+"---"+
Bytes.toString(CellUtil.cloneValue(cell))+"--"+
cell.getTimestamp()
);
System.out.println();
}
} } public static void main(String[] args) throws Exception {
HTable table=getTable("nstest1:tb1");
//getData(table);
//putData(table);
//deleteData(table);
//scanData(table);
filterScan(table);
} }

最新文章

  1. springmvc 动态代理 JDK实现与模拟JDK纯手写实现。
  2. 使用小米天气API获取天气信息
  3. java 某字符串在另一字符串中是否存在
  4. lua中for循环的四种遍历方式
  5. TFS(Team Foundation Server)敏捷使用教程(四):工作项跟踪(1)
  6. Xamarin Anroid App访问网站失败
  7. EventDemoandStyleDemoandThemeDemo
  8. 20141017--异常语句try-catch
  9. 【数据结构】非常有用的hash表
  10. 一些有用的webservice
  11. Linux自定义命令
  12. [HDU 2049] 不容易系列之(4)——考新郎 (错排问题)
  13. UVA10487(二分)
  14. centos7安装nagios步骤
  15. Java学习笔记16(面向对象九:补充内容)
  16. Struts2拦截SQL注入
  17. Silverlight数据绑定之 绑定一个int类型的属性
  18. VNC黑屏解决办法
  19. maven+testng+reportng的pom设置
  20. 深入解析QML引擎, 第1部分:QML文件加载

热门文章

  1. JavaScript之子类构建工具
  2. JavaScript之函数存储[摘]
  3. 第16月第17天 contentMode
  4. jquery中选择checkbox拼接成字符串,然后到后台拆分取值
  5. 【逆向工具】逆向工具101editor使用-游戏快速通关
  6. vsftpd控制用户禁止访问上级目录 只能访问自己目录
  7. ARMV8 datasheet学习笔记4:AArch64系统级体系结构之Generic timer
  8. vim常用命令总结 (转)【转】
  9. Expm 8_1 区间划分问题
  10. TCP template 代码