hbase docker本地开发环境

部门需求hbase做数据存储,只有一个正式环境集群,没有测试环境.
所以考虑本地部署一个hbase测试环境.

hbase本地环境部署

安装docker容器,windows只支持win10.
我是mac,直接从官网下载https://www.docker.com/docker-mac
安装启动docker:

#拉取容器
docker pull harisekhon/hbase
#启动容器
docker run -d -h myhbase -p 2181:2181 -p 8080:8080 -p 8085:8085 -p 9090:9090 -p 9095:9095 -p 16000:16000 -p 16010:16010 -p 16201:16201 -p 16301:16301 --name hbase1.3 harisekhon/hbase

-d表示后台
-h 定义容器host
-p表示端口映射
–name 表示容器别名 (我这里命名hbase1.3)
harisekhon/hbase是image镜像

访问页面http://localhost:16010/master-status

这里写图片描述


#进入容器
docker exec -it hbase1.3 /bin/bash
#执行hbase命令
hbase shell

这里写图片描述

可以看到hbase正常启动


修改本地开发环境的hosts,

vim /etc/hosts
#添加
127.0.0.1 myhbase

代码测试

maven

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>hbase</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>hbase</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>1.2.4</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

代码测试

package com.example.demo.hbase;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;


/**
 * @author allen
 */
public class Hbase {

    static Configuration conf=null;
    static {
        conf= HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","myhbase");
        conf.set("hbase.zookeeper.property.clientPort","2181");
        conf.set("log4j.logger.org.apache.hadoop.hbase","WARN");
    }

    public static void createTable(String tableName,String... families) throws Exception{
        HTableDescriptor tableDescriptor=new HTableDescriptor(TableName.valueOf(tableName));
        try{
            Connection connection=ConnectionFactory.createConnection(conf);
            Admin admin=connection.getAdmin();
            for(String family:families){
                tableDescriptor.addFamily(new HColumnDescriptor(family));
            }
            if(admin.tableExists(TableName.valueOf(tableName))){
                System.out.println("Table Exists");
                System.exit(0);
            }else{
                admin.createTable(tableDescriptor);
                System.out.println("Create table Success!!!Table Name:["+tableName+"]");
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    public static void deleteTable(String tableName){
        try{
            Connection connection=ConnectionFactory.createConnection(conf);
            Admin admin=connection.getAdmin();
            TableName table=TableName.valueOf(tableName);
            admin.disableTable(table);
            admin.deleteTable(table);
            System.out.println("delete table "+tableName+" ok!");
        }catch (IOException e){
            e.printStackTrace();
        }
    }


    public static void updateTable(String tableName,String rowKey,String familyName,String columnName,String value) throws Exception{
        try{
            Connection connection=ConnectionFactory.createConnection(conf);
            Table table=connection.getTable(TableName.valueOf(tableName));
            Put put=new Put(Bytes.toBytes(rowKey));
            put.addColumn(Bytes.toBytes(familyName),Bytes.toBytes(columnName),Bytes.toBytes(value));
            table.put(put);
            System.out.println("Update table success");
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    public static void addData(String rowKey,String tableName,String[] column,String[] value){
        try{
            Connection connection=ConnectionFactory.createConnection(conf);
            Table table=connection.getTable(TableName.valueOf(tableName));
            Put put=new Put(Bytes.toBytes(rowKey));
            HColumnDescriptor[] columnFamilies=table.getTableDescriptor().getColumnFamilies();
            for(int i=0;i<columnFamilies.length;i++){
                String familyName=columnFamilies[i].getNameAsString();
                if(familyName.equals("version")){
                    for(int j=0;j<column.length;j++){
                        put.addColumn(Bytes.toBytes(familyName),Bytes.toBytes(column[j]),Bytes.toBytes(value[j]));
                    }
                    table.put(put);
                    System.out.println("Add Data Success!");
                }
            }
        }catch(IOException e){

        }
    }



    public static void deleteAllColumn(String tableName,String rowKey){
        try{
            Connection connection=ConnectionFactory.createConnection(conf);
            Table table=connection.getTable(TableName.valueOf(tableName));
            Delete delAllColumn=new Delete(Bytes.toBytes(rowKey));
            table.delete(delAllColumn);
            System.out.println("Delete AllColumn Success");
        }catch(IOException e){
            e.printStackTrace();
        }
    }

    public static void deleteColumn(String tableName,String rowKey,String familyName,String columnName){
        try{
            Connection connection=ConnectionFactory.createConnection(conf);
            Table table=connection.getTable(TableName.valueOf(tableName));
            Delete delColumn=new Delete(Bytes.toBytes(rowKey));
            delColumn.addColumn(Bytes.toBytes(familyName),Bytes.toBytes(columnName));
            table.delete(delColumn);
            System.out.println("Delete Column Success");
        }catch (IOException e){
            e.printStackTrace();
        }
    }


    public  static void getResultByVersion(String tableName,String rowKey,String familyName,String columnName){
        try{
            Connection connection=ConnectionFactory.createConnection(conf);
            Table table =connection.getTable(TableName.valueOf(tableName));
            Get get =new Get(Bytes.toBytes(rowKey));
            get.addColumn(Bytes.toBytes(familyName),Bytes.toBytes(columnName));
            get.setMaxVersions(3);
            Result result=table.get(get);
            for(Cell cell :result.listCells()){
                System.out.println("family:"+Bytes.toString(cell.getFamilyArray(),cell.getFamilyOffset(),cell.getFamilyLength()));
                System.out.println("qualifier:"+Bytes.toString(cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength()));
                System.out.println("value:"+Bytes.toString(cell.getValueArray(),cell.getValueOffset(),cell.getValueLength()));
                System.out.println("Timestamp:"+cell.getTimestamp());
                System.out.println("---------------");
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {
        System.setProperty("hadoop.home.dir", "/usr/local/hadoop");

                Hbase.createTable("ota_pre_record","version");
//      Hbase.deleteTable("ota_pre_record");
//      Hbase.updateTable("ota_pre_record","324b1f27c982ea87XyZf+1502176018+20900","version","check_time","2017-11-08 10:46:34");
//      Hbase.updateTable("ota_pre_record","324b1f27c982ea87XyZf+1502176018+20900","version","download_time","2017-11-08 14:37:34");
//      Hbase.updateTable("ota_pre_record","24b1f27c982ea87XyZf+1502176018+20900","version","upgrade_time","2017-11-08 15:35:34");
//      Hbase.deleteColumn("ota_pre_record","24b1f27c982ea87XyZf+1502176018+20900","version","upgrade_time");
//      Hbase.deleteAllColumn("ota_pre_record","24b1f27c982ea87XyZf+1502176018+20900");
//      Hbase.getResultByVersion("ota_pre_record","24b1f27c982ea87XyZf+1502176018+20900","version","check_time");


    }
}

hbase-java代码参考了这个博客

https://blog.liyang.io/360.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Hadoop、Zookeeper、HBaseDocker都是大数据领域中常用的技术工具。 Hadoop是一个分布式计算框架,它可以将大数据分散到多个计算机集群上进行处理和存储。Hadoop的主要组件包括HDFS(分布式文件系统)和MapReduce(分布式计算模型),通过这两个组件,Hadoop可以实现对大规模数据的分布式处理和分布式存储。 Zookeeper是一个开源的分布式协调服务,它可以用于在分布式系统中管理和协调各个节点之间的状态和配置信息。Zookeeper提供了高可用性、一致性以及可靠性的特性,可以用于处理分布式应用程序中的诸多问题,比如选举机制、配置管理、命名服务等。 HBase是建立在Hadoop之上的分布式列式数据库,它提供了高度可扩展、高性能和高可靠性的存储和访问大规模结构化数据的能力。HBase基于Hadoop HDFS存储数据,同时将数据按照列族存储,具有快速随机读写的特性,适合处理海量数据。 Docker是一种容器化平台,它可以在操作系统层面上实现虚拟化,将应用程序及其依赖项打包成一个独立的容器,从而实现跨平台、快速部署和可移植性。在使用Docker时,可以将Hadoop、Zookeeper和HBase等组件打包成容器,方便在不同环境中进行部署和管理。同时,Docker还提供了简单易用的容器管理工具,可以进行容器的快速启动、停止和扩展。 综上所述,Hadoop、Zookeeper、HBaseDocker都是大数据领域中常用的技术工具。Hadoop用于分布式计算和存储,Zookeeper用于分布式协调和管理,HBase用于大规模结构化数据的存储和访问,而Docker则提供了容器化平台,方便部署和管理这些大数据技术组件。这些工具的使用可以帮助提高大数据处理的性能、可靠性和可扩展性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值