docker run --name hbase --net=host -v /etc/hbase/conf:/hbase-2.1.3/conf -v /home/data/hbase:/hbase-data harisekhon/hbase
2.修改hbase-sit.xml配置的hbase.master.hostname,hbase.regionserver.hostname,否则开发环境无法连接到hbase。因为开发环境项目的hbase java client
配置hbase连接只配置的ZK地址,故开发环境是通过获取注册到ZK的hbase master,regionserver的真实地址连接的,故需要在ZK注册网络可通的IP地址。
[root@node1 ~]
hadoop-metrics2-hbase.properties hbase-env.sh hbase-site.xml regionservers
hbase-env.cmd hbase-policy.xml log4j.properties
<configuration>
<property>
<name>hbase.master.hostname</name>
<value>192.168.1.19</value>
</property>
<property>
<name>hbase.regionserver.hostname</name>
<value>192.168.1.19</value>
</property>
<property>
<name>hbase.cluster.distributed</name>
<value>true</value>
</property>
<property>
<name>hbase.rootdir</name>
<value>/hbase-data</value>
</property>
<property>
<name>hbase.zookeeper.quorum</name>
<value>localhost:2181</value>
</property>
</configuration>
3.进入docker容器执行命令(hbase shell)进入交互界面,创建一个测试表
HBase Shell
Use "help" to get list of supported commands.
Use "exit" to quit this interactive shell.
For Reference, please visit: http://hbase.apache.org/2.0/book.html
Version 2.1.3, rda5ec9e4c06c537213883cca8f3cc9a7c19daf67, Mon Feb 11 15:45:33 CST 2019
Took 0.0061 seconds
hbase(main):001:0> create 'person','info'
Created table person
Took 2.0373 seconds
=> Hbase::Table - person
hbase(main):002:0> list
TABLE
person
1 row(s)
Took 0.0622 seconds
=> ["person"]
4.本地java连接hbase进行CRUD;hbase的API不好用,建议做个再封装
package com.jwolf.bigdata;
import lombok.SneakyThrows;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Put;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
public class HbaseTest {
@SneakyThrows
public static void main(String[] args) {
org.apache.hadoop.conf.Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "192.168.1.19");
configuration.set("hbase.zookeeper.port", "2181");
configuration.set("hbase.zookeeper.znode", "/hbase");
configuration.set("hbase.client.keyvalue.maxsize", "1572864000");
configuration.set("hbase.regionserver.port", "1572864000");
long t = System.currentTimeMillis();
Connection conn = ConnectionFactory.createConnection(configuration);
System.out.println("连接耗时" + (System.currentTimeMillis() - t));
String tables = Arrays.stream(conn.getAdmin().listTableNames()).map(TableName::getNameAsString).collect(Collectors.joining());
System.out.println("已存在表:" + tables);
Map<String, String> cloumns = new HashMap<String, String>();
cloumns.put("name", "zzq");
cloumns.put("age", "22");
Put put = new Put("1".getBytes());
put.addColumn("info".getBytes(), "name".getBytes(), "jwolf".getBytes());
put.addColumn("info".getBytes(), "age".getBytes(), "18".getBytes());
conn.getTable(TableName.valueOf("person")).put(put);
System.out.println("增加成功");
}
}
|