irpas技术客

docker搭建hbase环境_luffy5459_docker hbase

网络投稿 1061

? ? hbase是hadoop生态中的一员,最早搭建hbase,需要先安装hadoop,然后安装zookeeper,再安装hbase。现在通过docker可以直接安装hbase,而且容器中并不需要hadoop。

? ? 安装很简单。直接拉取镜像,然后运行即可。

docker run -d --name hbase -p 2181:2181 -p 16010:16010 -p 16020:16020 -p 16030:16030 harisekhon/hbase

? ? 这里注意一点,对于端口映射,hbase用到了很多端口,有的地方直接使用-P参数,而不是用-p做端口一一映射。?

docker run -d --name hbase -P harisekhon/hbase

? ? 如果通过-P参数,会将容器内部所有监听的端口都映射为随机端口,我们可以看看容器状态:

[root@docker ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 748f76bf11f3 harisekhon/hbase "/entrypoint.sh" 31 minutes ago Up 31 minutes 0.0.0.0:32776->2181/tcp, 0.0.0.0:32775->8080/tcp, 0.0.0.0:32774->8085/tcp, 0.0.0.0:32773->9090/tcp, 0.0.0.0:32772->9095/tcp, 0.0.0.0:32771->16000/tcp, 0.0.0.0:32770->16010/tcp, 0.0.0.0:32769->16201/tcp, 0.0.0.0:32768->16301/tcp hbase

? ? 其中,16010端口是hbase提供的webui界面暴露的端口,映射为了32770,我们可以通过虚拟机地址,以及这个端口访问hbase webui。

? ? ?到了这里,通过web界面能够看到hbase状态,然后,可以进入容器,运行hbase shell,执行建表,添加数据,扫描数据都没有问题。但是如果外部程序,比如java需要操作hbase,就非常麻烦。

? ? 所以,这里不建议直接使用-P(大写)的方式启动hbase。

? ? 推荐的启动方式:docker run -d --name hbase -p 2181:2181 -p 16010:16010 -p 16020:16020 -p 16030:16030 harisekhon/hbase

? ? 不推荐的启动方式:docker run -d --name -P harisekhon/hbase

? ? springboot与hbase整合

? ? 1、通过docker搭建hbase环境虽然简单,但是有个问题,就是它的主机映射直接使用的是容器ID,所以,我们外部java程序如果需要连接hbase,需要对容器ID添加到本机的hosts列表中。

192.168.61.150 docker dd13ff2dca8e

? ? 2、引入hbase依赖库,这里,只需要额外引入hbase-client依赖即可。

<dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>2.4.3</version> </dependency>

? ?3、配置文件application.properties

hbase.config.hbase.zookeeper.quorum=192.168.61.150 hbase.config.hbase.zookeeper.property.clientPort=2181

? ?4、配置文件对应Java类,HbaseProperties.java

package com.xx.hbase.config; import java.util.Map; import org.springframework.boot.context.properties.ConfigurationProperties; @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; } }

? ? 5、HbaseConfig.java

package com.xx.hbase.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 HbaseConfig { private final HbaseProperties props; public HbaseConfig(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(); } }

? ? 6、HbaseService.java

package com.xx.hbase.config; 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())); } } } }

? ? 7、springboot启动类

package com.xx.hbase; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class HbaseApp { public static void main(String[] args) { SpringApplication.run(HbaseApp.class, args); } }

? ? 8、hbase单元测试类

package com.xx.mybatis; import java.io.IOException; import java.util.Map; import org.apache.hbase.thirdparty.org.apache.commons.collections4.map.HashedMap; import org.junit.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 com.xx.hbase.HbaseApp; import com.xx.hbase.config.HbaseService; @SpringBootTest(classes = HbaseApp.class) @RunWith(SpringJUnit4ClassRunner.class) public class HbaseTest { @Autowired private HbaseService hbaseService; @Test public void createTable() { try { hbaseService.createTable("stu", "info"); } catch (IOException e) { e.printStackTrace(); } } @Test public void putData() { String name = "stu"; String colFamily = "info"; String rowKey = "2"; Map<String, String> data = new HashedMap<>(); data.put("name", "bbb"); data.put("email", "bb@126.com"); try { hbaseService.putData(name, colFamily, rowKey, data); } catch (IOException e) { e.printStackTrace(); } } @Test public void getData() { try { hbaseService.getData("stu"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

? ? 这里单元测试类,没有使用断言,只是调用了一下建表,插入数据,查询数据三个方法,证明java调用hbase接口操作hbase是没有问题的。?

? ? 9、在docker容器中操作hbase

? ? 总结:

? ? ? ? ?1、启动容器,不要使用-P随机端口映射,这样对后续java外部程序接入hbase造成很大麻烦。

? ? ? ? ?2、docker启动的hbase,使用了容器ID作为host,所以如果是外部程序要接入hbase,需要在本机hosts中加入容器ID对应的主机映射。?


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #Docker #HBase #安装很简单 #直接拉取镜像然后运行即可