HBase学习心得之HBase原理Java接口操作增删改查

2024-05-12 17:48

本文主要是介绍HBase学习心得之HBase原理Java接口操作增删改查,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

HBase之伟大(总结)

  一:region是按大小分割的,每个表开始只有一个region。随着数据不断插入表,region会不断增大,增大到了某个阀值,HRegion就会等分成两个新的HRegion。

  二:HRegion是Hbase中分布式存储和负载均衡的最小单位,最小单位就表示不同的HRegion能分布在不同的HRegion Server上,但一个HRegion是不能拆分到多个Server上的。

  三:HRegion虽是分布式存储的最小单位,但并不是存储的最小单位。HRegion由一个或多个Store组成,每个Store保存一个Column Family。每个Store又由一个memstore和0至多个storeFile组成。

  四:HBase各个组成部分的作用

         1.Client

            包含访问HBase的接口,client维护着一些cache来加快对HBase的访问,比如region的位置信息。

         2.Zookeeper

            1.保证任何时刻,集群中只有一个master。

            2.存储所有的Region的寻址入口。(Client能够通过Zookeeper访问到Region Server)

            3.实时监控Region Sever状态,将Region Server的上线和下线信息能够实时通知Master。(心跳机制RPC)

            4.存储HBase的schema,包括有哪些table,每个table有哪些Column Family。

         3.Master

            1.为Region Server分配region。

            2.负责Region Server的负载均衡。

            3.发现失效的Region Server,并重新分配其上的region。

            4.HDFS的垃圾回收。

            5.处理schema更新请求。

         4.Region Server

            1.维护Master分配的region,处理对这些的region的I/O请求。

            2.负责切分这些运行过程中变得过大的region。

PS:

      Client访问HBase上的数据过程并不需要master的参与。(寻址访问Zookeeper和Region Server)master仅仅维护着table和region的元数据信息,负载很低。

 

Java接口操作HBase

package cn.hbase;

import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
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.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.util.Bytes;
import org.junit.Before;
import org.junit.Test;

public class HBaseDemo {

 private Configuration conf;

 /*
  * 在增删改查之前操作
  */
 @Before
 public void init() {
  conf = HBaseConfiguration.create();
  conf.set("hbase.zookeeper.quorum", "192.168.56.110");
 }

 public static void main(String[] args) throws Exception {
  Configuration conf = HBaseConfiguration.create();
  // 客户端连的zookeeper
  /*
   * zookeeper集群
   */
  // conf.set("hbase.zookeeper.quorum",
  // "centos1:2181,centos2:2181,centos3:2181");
  /*
   * zookeeper单节点集群(hadoop+zookeeper+hbase)
   */
  conf.set("hbase.zookeeper.quorum", "192.168.56.110");
  HBaseAdmin admin = new HBaseAdmin(conf);
  // 创建user表
  HTableDescriptor htd = new HTableDescriptor(TableName.valueOf("user"));
  // 增添列族
  HColumnDescriptor hcd_info = new HColumnDescriptor("info");
  HColumnDescriptor hcd_data = new HColumnDescriptor("data");
  // 增添列族属性
  // 3
  hcd_info.setMaxVersions(3);
  // 2
  htd.addFamily(hcd_data);
  htd.addFamily(hcd_info);
  // 1
  admin.createTable(htd);

  admin.close();
 }

 /*
  * 插入
  */
 @Test
 public void testPut() throws Exception {
  // 得到表对象
  HTable table = new HTable(conf, "user");
  // put对象
  Put put = new Put(Bytes.toBytes("rk0001"));
  // 增加列族。列名。值
  put.add(Bytes.toBytes("info"), Bytes.toBytes("name"),
    Bytes.toBytes("zhangsan"));
  put.add(Bytes.toBytes("info"), Bytes.toBytes("age"),
    Bytes.toBytes("120"));
  put.add(Bytes.toBytes("info"), Bytes.toBytes("money"),
    Bytes.toBytes(10000));
  table.put(put);
  table.close();
 }

 /*
  * 批量插入100万条
  */
 public void testPut2() throws Exception {
  // 得到表对象
  HTable table = new HTable(conf, "user");
  // put对象数组
  List<Put> list = new ArrayList<Put>(10000);
  for (int i = 0; i < 1000001; i++) {
   Put put = new Put(Bytes.toBytes("rk0001"));
   // 增加列族。列名。值
   put.add(Bytes.toBytes("info"), Bytes.toBytes("name"),
     Bytes.toBytes("zhangsan" + i));
   list.add(put);
   if (i % 10000 == 0) {
    // 每10000条提交一次
    table.put(list);
    list = new ArrayList<Put>(10000);
   }
  }
  table.put(list);
  table.close();
 }

 /*
  * 取数据
  */
 @Test
 public void testGet() throws Exception {
  HTable table = new HTable(conf, "user");
  Get get = new Get(Bytes.toBytes("rk0001"));
  Result result = table.get(get);
  // 由result对象读取
  String r = Bytes.toString(result.getValue(Bytes.toBytes("info"),
    Bytes.toBytes("name")));
  System.out.println(r);
  table.close();
 }

 /*
  * 取多个
  */
 @Test
 public void testGets() throws Exception {
  HTable table = new HTable(conf, "user");
  Scan scan = new Scan(Bytes.toBytes("rk0001"), Bytes.toBytes("rk0005")); // 包含前面不包含后面
  // 返回结果集
  ResultScanner scanner = table.getScanner(scan);
  for (Result result : scanner) {
   String r = Bytes.toString(result.getValue(Bytes.toBytes("info"),
     Bytes.toBytes("name")));
   System.out.println(r);
  }
  table.close();
 }

 /*
  * 删除
  */
 @Test
 public void testDel() throws Exception {
  HTable table = new HTable(conf, "user");
  Delete delete = new Delete(Bytes.toBytes("rk0001"));
  table.delete(delete);
  table.close();
 }
}

 

 

 

       

 

这篇关于HBase学习心得之HBase原理Java接口操作增删改查的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/983319

相关文章

java实现延迟/超时/定时问题

《java实现延迟/超时/定时问题》:本文主要介绍java实现延迟/超时/定时问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java实现延迟/超时/定时java 每间隔5秒执行一次,一共执行5次然后结束scheduleAtFixedRate 和 schedu

Java Optional避免空指针异常的实现

《JavaOptional避免空指针异常的实现》空指针异常一直是困扰开发者的常见问题之一,本文主要介绍了JavaOptional避免空指针异常的实现,帮助开发者编写更健壮、可读性更高的代码,减少因... 目录一、Optional 概述二、Optional 的创建三、Optional 的常用方法四、Optio

Spring Boot项目中结合MyBatis实现MySQL的自动主从切换功能

《SpringBoot项目中结合MyBatis实现MySQL的自动主从切换功能》:本文主要介绍SpringBoot项目中结合MyBatis实现MySQL的自动主从切换功能,本文分步骤给大家介绍的... 目录原理解析1. mysql主从复制(Master-Slave Replication)2. 读写分离3.

idea maven编译报错Java heap space的解决方法

《ideamaven编译报错Javaheapspace的解决方法》这篇文章主要为大家详细介绍了ideamaven编译报错Javaheapspace的相关解决方法,文中的示例代码讲解详细,感兴趣的... 目录1.增加 Maven 编译的堆内存2. 增加 IntelliJ IDEA 的堆内存3. 优化 Mave

Java String字符串的常用使用方法

《JavaString字符串的常用使用方法》String是JDK提供的一个类,是引用类型,并不是基本的数据类型,String用于字符串操作,在之前学习c语言的时候,对于一些字符串,会初始化字符数组表... 目录一、什么是String二、如何定义一个String1. 用双引号定义2. 通过构造函数定义三、St

springboot filter实现请求响应全链路拦截

《springbootfilter实现请求响应全链路拦截》这篇文章主要为大家详细介绍了SpringBoot如何结合Filter同时拦截请求和响应,从而实现​​日志采集自动化,感兴趣的小伙伴可以跟随小... 目录一、为什么你需要这个过滤器?​​​二、核心实现:一个Filter搞定双向数据流​​​​三、完整代码

SpringBoot利用@Validated注解优雅实现参数校验

《SpringBoot利用@Validated注解优雅实现参数校验》在开发Web应用时,用户输入的合法性校验是保障系统稳定性的基础,​SpringBoot的@Validated注解提供了一种更优雅的解... 目录​一、为什么需要参数校验二、Validated 的核心用法​1. 基础校验2. php分组校验3

Java Predicate接口定义详解

《JavaPredicate接口定义详解》Predicate是Java中的一个函数式接口,它代表一个判断逻辑,接收一个输入参数,返回一个布尔值,:本文主要介绍JavaPredicate接口的定义... 目录Java Predicate接口Java lamda表达式 Predicate<T>、BiFuncti

Spring Security基于数据库的ABAC属性权限模型实战开发教程

《SpringSecurity基于数据库的ABAC属性权限模型实战开发教程》:本文主要介绍SpringSecurity基于数据库的ABAC属性权限模型实战开发教程,本文给大家介绍的非常详细,对大... 目录1. 前言2. 权限决策依据RBACABAC综合对比3. 数据库表结构说明4. 实战开始5. MyBA

Spring Security方法级安全控制@PreAuthorize注解的灵活运用小结

《SpringSecurity方法级安全控制@PreAuthorize注解的灵活运用小结》本文将带着大家讲解@PreAuthorize注解的核心原理、SpEL表达式机制,并通过的示例代码演示如... 目录1. 前言2. @PreAuthorize 注解简介3. @PreAuthorize 核心原理解析拦截与