HBaseをローカル分散モードで立ち上げてJavaから繋ぐまで

好き勝手やるためにローカル分散モードで立ち上げてJavaのHBase Clientから繋ぐまでやる。

  • HBaseローカル分散モードでコンテナ起動

GitHub - big-data-europe/docker-hbase

> git clone https://github.com/big-data-europe/docker-hbase.git
> docker-compose -f docker-compose-distributed-local.yml up -d
  • HBaseマスタのコンテナにログイン。使うテーブルを適当に作る。
> docker-compose -f docker-compose-distributed-local.yml exec hbase-master bash
> root@hbase-master:/# hbase shell

hbase(main):001:0> list
TABLE                                                                                
0 row(s) in 0.1540 seconds

=> []
hbase(main):002:0> create 'source_table', {NAME=>'e',VERSIONS=>10}
0 row(s) in 1.3220 seconds

=> Hbase::Table - source_table

hbase(main):005:0* scan 'source_table'
ROW                    COLUMN+CELL                                                   
0 row(s) in 0.0860 seconds
  • Gradleのdependenciesに追加。HBaseのバージョンが1.2.6なのでそれに準ずるバージョンを指定。
dependencies {
    ...
    compile group: 'org.apache.hbase', name: 'hbase-client', version: '1.2.6'
    compile group: 'org.apache.hbase', name: 'hbase-server', version: '1.2.6'
}
  • docker-composeの外側からHBaseに接続して適当なデータをPutするコード。
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;

import com.google.protobuf.ServiceException;

public final class ExampleReadWrite {

    public static void main(String[] args) throws IOException {
        final var config = HBaseConfiguration.create();
        config.set("hbase.zookeeper.quorum", "localhost");
        config.set("hbase.zookeeper.property.clientPort","2181");

        try (final var conn = ConnectionFactory.createConnection(config)) {
            HBaseAdmin.checkHBaseAvailable(config);
            final var sourceTableName = Bytes.toBytes("source_table");

            final var put = new Put(Bytes.toBytes("z"));
            put.addColumn(Bytes.toBytes("e"), Bytes.toBytes("c"), Bytes.toBytes(20));
            put.addColumn(Bytes.toBytes("e"), Bytes.toBytes("c"), Bytes.toBytes(10));

            final var sourceTable = conn.getTable(TableName.valueOf(sourceTableName));
            sourceTable.put(put);

            sourceTable.close();
        } catch (IOException | ServiceException e) {
            System.out.println(e);
        }
    }

}
# for docker-hbase
127.0.0.1       hbase-master
127.0.0.1       hbase-region
  • また別のエラー。org.apache.hadoop.hbase.MasterNotRunningException: com.google.protobuf.ServiceException: java.net.ConnectException: Connection refusedがでる。ローカルから接続する際にdocker-compose-distributed-local.ymlport設定が足りていないので以下のように編集。(16000, 16020を追加する)
...
  hbase-master:
    image: bde2020/hbase-master:1.0.0-hbase1.2.6
    container_name: hbase-master
    hostname: hbase-master
    env_file:
      - ./hbase-distributed-local.env
    environment:
      SERVICE_PRECONDITION: "namenode:50070 datanode:50075 zoo:2181"
    ports:
      - 16000:16000
      - 16010:16010
...
  hbase-region:
    image: bde2020/hbase-regionserver:1.0.0-hbase1.2.6
    container_name: hbase-regionserver
    hostname: hbase-regionserver
    env_file:
      - ./hbase-distributed-local.env
    environment:
      HBASE_CONF_hbase_regionserver_hostname: hbase-region
      SERVICE_PRECONDITION: "namenode:50070 datanode:50075 zoo:2181 hbase-master:16010"
    ports:
      - 16020:16020
      - 16030:16030
  • コードを実行するとようやく最後までrunされる。
hbase(main):006:0> scan 'source_table'
ROW                    COLUMN+CELL                                                   
 z                     column=e:c, timestamp=1582633454479, value=\x00\x00\x00\x0A   
1 row(s) in 0.0260 seconds