docker安装hbase 并启用

发布于:2023-01-22 ⋅ 阅读:(11) ⋅ 点赞:(0) ⋅ 评论:(0)

1. 首先在终端上 输入命令 docker search hbase

2. 拉取镜像 docker pull harisekhon/hbase

 看到这个 就代表安装好了 这个是拉取最新版本的

然后在进行启动     docker run -d -h hbase -p 2181:2181 -p 8080:8080 -p 8085:8085 -p 9090:9090 -p 9095:9095 -p 16000:16000 -p 16010:16010 -p 16020:16020 -p 16201:16201 -p 16301:16301 --name hbase harisekhon/hbase

***注意*** 如果是服务器 别忘记把这些端口打开

在通过http://IP:16010可以访问了

这个是它的一个web页面  下边在进行容器创建表结构 数据加入

命令:docker exec -it hbase bash

 cd /hbase/bin

./hbase shell

看到这个 就代表进入成功

建表 create 'sen', 'info'

 加入数据 put ''sen','10010','info:name','zhangsan'

 查看表结构 scan 'sen'

给指定的id put 'sen','10010','info:sex','MAN'

查看表体数据结构  describe 'sen'

 获取指定id数据 get'sen' ,'10010'

清空表 truncate 'sen'

查看表的行数(rowkey的数量)count 'sen'

删除一列数据/一列族数据 delete 'sen', '10010','info:name'  deleteall 'sen','10010'

 

 现在进行Java代码连接 用Java代码进行创建表 和添加数据 获取数据

1.首先 获取在docker运行中的hbase容器的容器id docker ps -a

 2. 在进行域名映射配置 vi /etc/hosts 前边是自己的ip 后边跟着的是 容器id

 3. 在把容器名称复制 在windows上的windows/system32/drivers/etc/hosts文件 加入这个 ip 容器名称

在用到Java代码写的时候 编译的时候 需要本地安装一个Hadoop 需要一个环境变量

链接: https://pan.baidu.com/s/1Kinj3SZXCVKkb66CzJOqGg?pwd=yv5i 提取码: yv5i

hadoop 压缩包地址

还需要配置一个path

以上操作完毕之后 在写入Java代码 进行连接

项目结构大致是这样的

配置文件

pom依赖

<!-- hbase-client依赖 -->
<dependency>
    <groupId>org.apache.hbase</groupId>
    <artifactId>hbase-client</artifactId>
    <version>2.4.3</version>
</dependency>
HbaseProperties
package com.hbase.hbaseapi.config;

import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.Map;

@ConfigurationProperties(prefix = "hbase")
public class HbaseProperties {

    private Map<String,String> config;

    public void setConfig(Map<String, String> config) {
        this.config = config;
    }

    public Map<String, String> getConfig() {
        return config;
    }

}
HbasePropertiesConfig

package com.hbase.hbaseapi.config;

import java.io.IOException;
import java.util.Map;

import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

    @Configuration
    @EnableConfigurationProperties(HbaseProperties.class)
    public class HbasePropertiesConfig {
        private final HbaseProperties props;

        public HbasePropertiesConfig(HbaseProperties props) {
            this.props = props;
        }

        @Bean
        public org.apache.hadoop.conf.Configuration configuration(){
            org.apache.hadoop.conf.Configuration conf = HBaseConfiguration.create();
            Map<String, String> config = props.getConfig();
            config.forEach(conf::set);
            return conf;
        }

        @Bean
        public Connection getConnection() throws IOException{
            return ConnectionFactory.createConnection(configuration());
        }

        @Bean
        public HBaseAdmin hBaseAdmin() throws IOException {
            return (HBaseAdmin) getConnection().getAdmin();
        }

}

HbaseService
package com.hbase.hbaseapi.service;

import java.io.IOException;
import java.util.Map;

import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.HBaseAdmin;
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.client.Table;
import org.apache.hadoop.hbase.client.TableDescriptor;
import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
import org.apache.hadoop.hbase.util.Bytes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class HbaseService {

    @Autowired
    private HBaseAdmin admin;

    @Autowired
    private Connection connection;

    public void createTable(String name,String colFamily) throws IOException {
        TableName table = TableName.valueOf(name);
        if(admin.tableExists(table)) {
            System.out.println("table ["+name+"] exist.");
        }

        ColumnFamilyDescriptor cfd = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(colFamily))
                .setMaxVersions(1).build();

        TableDescriptor tableDes = TableDescriptorBuilder.newBuilder(table).setColumnFamily(cfd).build();
        admin.createTable(tableDes);

    }

    public void putData(String name,String colFamily,String rowKey,Map<String, String> data) throws IOException {
        TableName table = TableName.valueOf(name);
        if(admin.tableExists(table)) {
            Table t = connection.getTable(table);
            Put put = new Put(Bytes.toBytes(rowKey));
            for(Map.Entry<String, String> entry:data.entrySet()) {
                put.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(entry.getKey()), Bytes.toBytes(entry.getValue()));
            }
            t.put(put);
        }else {
            System.out.println("table ["+name+"] does not exist.");
        }
    }

    public void getData(String name) throws IOException{
        TableName table = TableName.valueOf(name);
        Table t = connection.getTable(table);
        ResultScanner rs = t.getScanner(new Scan());
        for(Result r:rs) {
            System.out.println("row:"+new String(r.getRow()));
            for(Cell cell:r.rawCells()) {
                System.out.println("colFamily:"+Bytes.toString(cell.getFamilyArray(),cell.getFamilyOffset(),cell.getFamilyLength())+""
                        +",qualifier:"+Bytes.toString(cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength())+
                        ",value:"+Bytes.toString(cell.getValueArray(),cell.getValueOffset(),cell.getValueLength()));
            }
        }
    }

}

test类

package com.hbase.hbaseapi;

import com.hbase.hbaseapi.service.HbaseService;
import org.apache.hbase.thirdparty.org.apache.commons.collections4.map.HashedMap;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.Map;

@SpringBootTest(classes = HbaseApiApplication.class)
@RunWith(SpringJUnit4ClassRunner.class)
class HbaseApiApplicationTests {

    @Autowired
    private HbaseService hbaseService;

    @Test
    void contextLoads() {
        try {
            // 创建表
            hbaseService.createTable("scr", "info");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void putData() {
        // 添加数据
        String name = "scr";
        String colFamily = "info";
        String rowKey = "2";
        Map<String, String> data = new HashedMap<>();
        data.put("name", "bb");
        data.put("email", "bb@qq.com");
        try {
            hbaseService.putData(name, colFamily, rowKey, data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // docker run -d -h hbase -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:1.3

    @Test
    public void getData() {
        // 获取表结构数据
        try {
            hbaseService.getData("scr");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

以上是我个人亲自实践出的 代码 效果 有意见 想法 觉得不好的地方 可以评论 谢谢!