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();

       }

   }


相关实践学习
云数据库HBase版使用教程
&nbsp; 相关的阿里云产品:云数据库 HBase 版 面向大数据领域的一站式NoSQL服务,100%兼容开源HBase并深度扩展,支持海量数据下的实时存储、高并发吞吐、轻SQL分析、全文检索、时序时空查询等能力,是风控、推荐、广告、物联网、车联网、Feeds流、数据大屏等场景首选数据库,是为淘宝、支付宝、菜鸟等众多阿里核心业务提供关键支撑的数据库。 了解产品详情:&nbsp;https://cn.aliyun.com/product/hbase &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
JavaScript 关系型数据库 MySQL
❤Nodejs 第六章(操作本地数据库前置知识优化)
【4月更文挑战第6天】本文介绍了Node.js操作本地数据库的前置配置和优化,包括处理接口跨域的CORS中间件,以及解析请求数据的body-parser、cookie-parser和multer。还讲解了与MySQL数据库交互的两种方式:`createPool`(适用于高并发,通过连接池管理连接)和`createConnection`(适用于低负载)。
18 0
|
1月前
|
Java 数据库连接 数据库
hibernate正向生成数据库表以及配置——TestStu.java
hibernate正向生成数据库表以及配置——TestStu.java
18 1
|
1月前
|
Java 数据库连接 数据库
hibernate正向生成数据库表以及配置——Teacher.java
hibernate正向生成数据库表以及配置——Teacher.java
11 0
|
1月前
|
Java 数据库连接 数据库
hibernate正向生成数据库表以及配置——Student.java
hibernate正向生成数据库表以及配置——Student.java
10 0
|
1月前
|
SQL 数据库连接 数据库
你不知道ADo.Net中操作数据库的步骤【超详细整理】
你不知道ADo.Net中操作数据库的步骤【超详细整理】
16 0
|
23天前
|
存储 NoSQL Java
Java数据库编程指南:实现高效数据存储与访问
【4月更文挑战第2天】Java开发者必须掌握数据库编程,尤其是JDBC,它是连接数据库的标准接口。使用Spring JDBC或JPA能简化操作。选择合适的JDBC驱动,如MySQL Connector/J,对性能至关重要。最佳实践包括事务管理、防SQL注入、优化索引和数据库设计。NoSQL数据库如MongoDB也日益重要,Java有对应的驱动支持。理解这些概念和技术是构建高效数据库应用的基础。
Java数据库编程指南:实现高效数据存储与访问
|
1天前
|
数据采集 前端开发 测试技术
《手把手教你》系列技巧篇(三十一)-java+ selenium自动化测试- Actions的相关操作-番外篇(详解教程)
【4月更文挑战第23天】本文介绍了网页中的滑动验证码的实现原理和自动化测试方法。作者首先提到了网站的反爬虫机制,并表示在本地创建一个没有该机制的网页,然后使用谷歌浏览器进行验证。接着,文章详细讲解了如何使用WebElement的click()方法以及Action类提供的API来模拟鼠标的各种操作,如右击、双击、悬停和拖动。
6 2
|
2天前
|
存储 分布式计算 Hadoop
基于Hadoop分布式数据库HBase1.0部署及使用
基于Hadoop分布式数据库HBase1.0部署及使用
|
2天前
|
SQL Java 数据库连接
Java从入门到精通:2.3.2数据库编程——了解SQL语言,编写基本查询语句
Java从入门到精通:2.3.2数据库编程——了解SQL语言,编写基本查询语句
|
2天前
|
SQL Java 数据库连接
Java从入门到精通:2.3.1数据库编程——学习JDBC技术,掌握Java与数据库的交互
ava从入门到精通:2.3.1数据库编程——学习JDBC技术,掌握Java与数据库的交互

热门文章

最新文章