Hbase数据库完全分布式搭建以及java中操作Hbase

简介: Hbase数据库完全分布式搭建以及java中操作Hbase

文章目录

1.基础的环境准备

基础的环境准备不在赘述,包括jdk安装,防火墙关闭,网络配置,环境变量的配置,各个节点之间进行免密等操作等。使用的版本2.0.5.

2.完全分布式 Fully-distributed

参考官方文档

分布式的部署,都是在单节点服务的基础配置好配置,直接分发到其他节点即可。

2.1 配置文件hase-env.sh

jdk路径的配置,以及不适用内部自带的zk.

export JAVA_HOME=/usr/java/default

export HBASE_MANAGES_ZK=false

2.2 hbase-site.xml

<configuration>

 <property>

   <name>hbase.rootdir</name>

   <value>hdfs://muycluster/hbase</value>

 </property>

 <property>

   <name>hbase.cluster.distributed</name>

   <value>true</value>

 </property>

 <property>

   <name>hbase.zookeeper.quorum</name>

   <value>node02,node03,node04</value>

 </property>

</configuration>

2.3 配置regionservers

配置集群regionserver的节点

node02

node03

node04

2.4 配置备用的master

conf/backup-masters

vi backup-masters

node03

2.5 HDFS客户端配置

官方提供三种方式进行配置

Add a pointer to your HADOOP_CONF_DIR to the HBASE_CLASSPATH environment variable in hbase-env.sh.


Add a copy of hdfs-site.xml (or hadoop-site.xml) or, better, symlinks, under ${HBASE_HOME}/conf, or


if only a small set of HDFS client configurations, add them to hbase-site.xml.

一般我们都选择第二种,直接将hadoop-site.xml配置拷贝到 ${HBASE_HOME}/conf即可

2.6 启动

[root@node01 /]# start-hbase.sh

SLF4J: Class path contains multiple SLF4J bindings.

SLF4J: Found binding in [jar:file:/opt/bigdata/hbase-2.0.5/lib/slf4j-log4j12-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]

SLF4J: Found binding in [jar:file:/opt/bigdata/hadoop-2.6.5/share/hadoop/common/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]

SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.

SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]

running master, logging to /opt/bigdata/hbase-2.0.5/logs/hbase-root-master-node01.out

SLF4J: Class path contains multiple SLF4J bindings.

SLF4J: Found binding in [jar:file:/opt/bigdata/hbase-2.0.5/lib/slf4j-log4j12-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]

SLF4J: Found binding in [jar:file:/opt/bigdata/hadoop-2.6.5/share/hadoop/common/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]

SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.

SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]

node02: running regionserver, logging to /opt/bigdata/hbase-2.0.5/bin/../logs/hbase-root-regionserver-node02.out

node04: running regionserver, logging to /opt/bigdata/hbase-2.0.5/bin/../logs/hbase-root-regionserver-node04.out

node03: running regionserver, logging to /opt/bigdata/hbase-2.0.5/bin/../logs/hbase-root-regionserver-node03.out

node04: running master, logging to /opt/bigdata/hbase-2.0.5/bin/../logs/hbase-root-master-node04.out

2.7 通过页面查看节点信息

2e4fa260348b4de3a675f9719ee01015.png

3. java中客户端操作Hbase

3.1 引入依赖

  <!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-client -->

   <dependency>

     <groupId>org.apache.hbase</groupId>

     <artifactId>hbase-client</artifactId>

     <version>2.0.5</version>

   </dependency>

   <dependency>

     <groupId>org.apache.hbase</groupId>

     <artifactId>hbase-mapreduce</artifactId>

     <version>2.0.5</version>

   </dependency>

3.2 初始化创建连接

操作表Java API中主要提供了一个Admin对象进行表的 操作。


HBase schemas can be created or updated using the The Apache HBase Shell or by using Admin in the Java API.

  Configuration conf = null;

   Connection conn = null;

   //表的管理对象

   Admin admin = null;

   Table table = null;

   //创建表的对象

   TableName tableName = TableName.valueOf("user");

   @Before

   public void init() throws IOException {

       //创建配置文件对象

       conf = HBaseConfiguration.create();

       //加载zookeeper的配置

       conf.set("hbase.zookeeper.quorum","node02,node03,node04");

       //获取连接

       conn = ConnectionFactory.createConnection(conf);

       //获取对象

       admin = conn.getAdmin();

       //获取数据操作对象

       table = conn.getTable(tableName);

   }

3.3 操作Hbase数据库

3.3.1 创建表

/**

    * 创建表 主要使用Admin对象进行表的创建

    * @throws IOException

    */

   @Test

   public void createTable() throws IOException {

       //定义表描述对象

       TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(tableName);

       //定义列族描述对象

       ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder("cf".getBytes());

       //添加列族信息给表

       tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptorBuilder.build());

       if(admin.tableExists(tableName)){

           //禁用表

           admin.disableTable(tableName);

           admin.deleteTable(tableName);

       }

       //创建表

       admin.createTable(tableDescriptorBuilder.build());

   }

3.3.2 往创建的user表插入数据

 @Test

   public void insert() throws IOException {

       Put put = new Put(Bytes.toBytes("row1"));

       put.addColumn(Bytes.toBytes("cf"),Bytes.toBytes("name"),Bytes.toBytes("elite"));

       put.addColumn(Bytes.toBytes("cf"),Bytes.toBytes("age"),Bytes.toBytes("22"));

       put.addColumn(Bytes.toBytes("cf"),Bytes.toBytes("address"),Bytes.toBytes("gz"));

       table.put(put);

   }


e5ac9500eafe4e42b130953ae2e727d1.png

3.3.3 使用get 查询单条数据

@Test

   public void get() throws IOException {

       Get get = new Get(Bytes.toBytes("row1"));

       //在服务端做数据过滤,挑选出符合需求的列

       get.addColumn(Bytes.toBytes("cf"),Bytes.toBytes("name"));

       get.addColumn(Bytes.toBytes("cf"),Bytes.toBytes("age"));

       get.addColumn(Bytes.toBytes("cf"),Bytes.toBytes("address"));

       Result result = table.get(get);

       Cell cell1 = result.getColumnLatestCell(Bytes.toBytes("cf"),Bytes.toBytes("name"));

       Cell cell2 = result.getColumnLatestCell(Bytes.toBytes("cf"),Bytes.toBytes("age"));

       Cell cell3 = result.getColumnLatestCell(Bytes.toBytes("cf"),Bytes.toBytes("address"));

       System.out.print(Bytes.toString(CellUtil.cloneValue(cell1))+" ");

       System.out.print(Bytes.toString(CellUtil.cloneValue(cell2))+" ");

       System.out.print(Bytes.toString(CellUtil.cloneValue(cell3)));

   }

3.3.4 scan 查询数据

 /**

    * 获取表中所有的记录

    */

   @Test

   public void scan() throws IOException {

       Scan scan = new Scan();

       ResultScanner rss = table.getScanner(scan);

       for (Result rs: rss) {

           Cell cell1 = rs.getColumnLatestCell(Bytes.toBytes("cf"),Bytes.toBytes("name"));

           Cell cell2 = rs.getColumnLatestCell(Bytes.toBytes("cf"),Bytes.toBytes("age"));

           Cell cell3 = rs.getColumnLatestCell(Bytes.toBytes("cf"),Bytes.toBytes("address"));

           System.out.print(Bytes.toString(CellUtil.cloneValue(cell1))+" ");

           System.out.print(Bytes.toString(CellUtil.cloneValue(cell2))+" ");

           System.out.println(Bytes.toString(CellUtil.cloneValue(cell3)));

       }

   }

3.3.5 删除数据

/**

    * 删除数据

    * @throws IOException

    */

   @Test

   public void delete() throws IOException {

       Delete delete = new Delete("row2".getBytes());

       table.delete(delete);

   }


3.4 关闭连接

@After

   public void close(){

       try {

           table.close();

       } catch (IOException e) {

           e.printStackTrace();

       }

       try {

           admin.close();

       } catch (IOException e) {

           e.printStackTrace();

       }

       try {

           conn.close();

       } catch (IOException e) {

           e.printStackTrace();

       }

   }


相关文章
|
负载均衡 算法 关系型数据库
大数据大厂之MySQL数据库课程设计:揭秘MySQL集群架构负载均衡核心算法:从理论到Java代码实战,让你的数据库性能飙升!
本文聚焦 MySQL 集群架构中的负载均衡算法,阐述其重要性。详细介绍轮询、加权轮询、最少连接、加权最少连接、随机、源地址哈希等常用算法,分析各自优缺点及适用场景。并提供 Java 语言代码实现示例,助力直观理解。文章结构清晰,语言通俗易懂,对理解和应用负载均衡算法具有实用价值和参考价值。
大数据大厂之MySQL数据库课程设计:揭秘MySQL集群架构负载均衡核心算法:从理论到Java代码实战,让你的数据库性能飙升!
|
分布式计算 Java Hadoop
java使用hbase、hadoop报错举例
java使用hbase、hadoop报错举例
421 4
|
SQL 存储 分布式数据库
分布式存储数据恢复—hbase和hive数据库数据恢复案例
分布式存储数据恢复环境: 16台某品牌R730xd服务器节点,每台服务器节点上有数台虚拟机。 虚拟机上部署Hbase和Hive数据库。 分布式存储故障: 数据库底层文件被误删除,数据库不能使用。要求恢复hbase和hive数据库。
504 12
|
人工智能 JavaScript 关系型数据库
【02】Java+若依+vue.js技术栈实现钱包积分管理系统项目-商业级电玩城积分系统商业项目实战-ui设计图figmaUI设计准备-figma汉化插件-mysql数据库设计-优雅草卓伊凡商业项目实战
【02】Java+若依+vue.js技术栈实现钱包积分管理系统项目-商业级电玩城积分系统商业项目实战-ui设计图figmaUI设计准备-figma汉化插件-mysql数据库设计-优雅草卓伊凡商业项目实战
524 14
【02】Java+若依+vue.js技术栈实现钱包积分管理系统项目-商业级电玩城积分系统商业项目实战-ui设计图figmaUI设计准备-figma汉化插件-mysql数据库设计-优雅草卓伊凡商业项目实战
|
人工智能 JavaScript 安全
【01】Java+若依+vue.js技术栈实现钱包积分管理系统项目-商业级电玩城积分系统商业项目实战-需求改为思维导图-设计数据库-确定基础架构和设计-优雅草卓伊凡商业项目实战
【01】Java+若依+vue.js技术栈实现钱包积分管理系统项目-商业级电玩城积分系统商业项目实战-需求改为思维导图-设计数据库-确定基础架构和设计-优雅草卓伊凡商业项目实战
765 13
【01】Java+若依+vue.js技术栈实现钱包积分管理系统项目-商业级电玩城积分系统商业项目实战-需求改为思维导图-设计数据库-确定基础架构和设计-优雅草卓伊凡商业项目实战
|
Java 数据库
案例一:去掉数据库某列中的所有英文,利用java正则表达式去做,核心:去掉字符串中的英文
这篇文章介绍了如何使用Java正则表达式从数据库某列中去除所有英文字符。
425 15
|
IDE Java 分布式数据库
Apache HBase 落地JAVA 实战
Apache HBase 落地 Java 实战主要涉及使用 Java API 来操作 HBase 数据库,包括表的创建、删除、数据的插入、查询等操作。以下是一个基于 Java 的 HBase 实战指南,包括关键步骤和示例代码。
963 23
|
Java 关系型数据库 MySQL
数据库的连接用Java
本文介绍了如何使用Java连接MySQL数据库,包括注册JDBC驱动、创建数据库连接URL、设置数据库用户和密码、建立连接以及关闭连接的完整代码示例。
416 0
数据库的连接用Java
|
安全 算法 Java
数据库信息/密码加盐加密 —— Java代码手写+集成两种方式,手把手教学!保证能用!
本文提供了在数据库中对密码等敏感信息进行加盐加密的详细教程,包括手写MD5加密算法和使用Spring Security的BCryptPasswordEncoder进行加密,并强调了使用BCryptPasswordEncoder时需要注意的Spring Security配置问题。
1459 0
数据库信息/密码加盐加密 —— Java代码手写+集成两种方式,手把手教学!保证能用!
|
缓存 Java Linux
java操作hbase报错:KeeperErrorCode=NoNode for /hbase-unsecure/master
java操作hbase报错:KeeperErrorCode=NoNode for /hbase-unsecure/master
1182 2