본문 바로가기

Hadoop/Hbase

HBase Client 프로그래밍 with JAVA

일반적으로 MySQL 데이터베이스를 사용하는 어플리케이션을 만들기 위해서는 또는 CRUD 연산을 하기 위해서 원격에서 해당 데이터베이스에 접근하기 위한 MySQL Client를 설치하거나 또는 해당 벤더에서 제공하는 JDBC를 사용하여야 합니다. 유명한 JDBC로는 오라클에서 제공하는 J connector 라는 것이 유명하죠~.

이처럼 HBase도 Client에서 HBase를 사용할 수 있게끔 Client용 프로그램을 제공합니다. HBase에 정보를 넣거나 가져올 때 사용하는 방법으로는 이 클라이언트 접속 프로그램을 포함하여 4가지 접근방법이 있습니다.

 

1. HBase Shell 이용

 가장 처음 hbase를 접하면 사용하는 방식일 것입니다. shell을 통해서는 테이블 및 스키마를 생성하거나, 수정할 때는 상관없지만, 대량의 데이터를 입력하기에는 불편하기 때문에 shell의 경우 HBase 개발자 보다는 운영자가 많이 사용합니다.

 $ hbase shell

 hbase(main):001:0> status
1 active master, 1 backup masters, 4 servers, 0 dead, 12.0000 average load

2. Apache HBase Client jar(JAVA) 이용

 Maven Repo을 참조하여 아카이빙 된 jar파일을 다운로드하여 사용하거나, pom.xml에 원하는 버전의 파일을 등록하면 프로젝트 빌드 시에 자동으로 설치됩니다.

 

3. Thrift Server

 만약 HBase 클러스터에 Thrift 서버가 설치되어 있다면, 다양한 언어를 이용해 Thrift 서버로 HBase를 사용할 수 있습니다.

 

4. REST API Server

 http의 GET, PUT, POST, DELETE 를 사용하여 클러스터에 데이터를 입력하고, 또는 get, scan을 할 수 있습니다.

 

오늘 알아볼 방식은 2번 방식입니다.

 

아래 내용은 HBase 클러스터가 설치된 후에 진행해야합니다.

저는 Cloudera의 CDH5를 통해 설치했고 HBase는 1.2.0, Hadoop은 2.6버전입니다.

 

Java Maven 프로젝트 생성

JDK는 Amazon Correctto의 1.8로 선택했습니다.

 

pom.xml 에 dependency 등록

클러스터의 HBase 버전을 잘 적어서 version 테그 안에 잘 적어줍니다.

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

 

HBase Connect

config.set() 함수에 첫 번째 인자로 주키퍼 쿼럼 정보를, 두 번째 인자로 Zookeeper의 IP 또는 FQDN(Fully Qualified Domain Name)을 적어 줍니다. 이 때 checkHBaseAvailable(confg)함수에서 exception이 발생하지 않으면 클러스터 접근에 성공한 것입니다.

 

왜 Zookeeper의 주소를 적어주는지 의문을 가지실 분이 계실텐데 HBase는 HMaster의 위치나 Node의 정보를 Zookeeper에서 관리합니다. 즉 HBase에서 Zookeeper는 클라이언트와의 End Point 역할을 하기 때문에 Connection에 필요합니다. 더 자세히 말씀드리면, Zookeeper에 저장된 (-ROOT-)리전이 위치한 리전서버를 찾고 이 (-ROOT-)리전을 토를 통해 row-key를 찾아갑니다. 해당 내용은 다른 포스팅에서 자세히 다루겠습니다.

HBaseConfiguration config = new HBaseConfiguration();
config.set("hbase.zookeeper.quorum", "IP or FQDN");  
HBaseAdmin.checkHBaseAvailable(config);

 

PUT

HTable table = new HTable(config, "testtable");
Put put = new Put(Bytes.toBytes("row1")); 
put.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"),
                            Bytes.toBytes("val1")); 
put.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual2"),
                            Bytes.toBytes("val2"));
table.put(put);

 

GET

HTable table = new HTable(config, "testtable");
Get get = new Get(Bytes.toBytes("row1"));
get.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1")); 
Result result = table.get(get); 
byte[] val = result.getValue(Bytes.toBytes("colfam1"),
Bytes.toBytes("qual1"));
System.out.println("Value: " + Bytes.toString(val)); 
table.close();

 

SCAN

HTable table = new HTable(config, "testtable");
System.out.println("Scanning table #1...");
Scan scan1 = new Scan();
ResultScanner scanner1 = table.getScanner(scan1); 
for (Result res : scanner1) {
  System.out.println(res); 
}
scanner1.close();

DELETE

HTable table = new HTable(config, "testtable");
Delete delete = new Delete(Bytes.toBytes("row1"));
delete.setTimestamp(1); 
delete.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1")); 
delete.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual3"), 3); 
delete.addColumns(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"))
delete.addColumns(Bytes.toBytes("colfam1"), Bytes.toBytes("qual3"), 2); 
delete.addFamily(Bytes.toBytes("colfam1"));
delete.addFamily(Bytes.toBytes("colfam1"), 3); 
table.delete(delete); 
table.close();

 

HBase가  HDFS나 Zookeeper와 dependency가 있어 초기 클러스터 구축이 힘든 편이고 row-key 설계와 관련된 지식이 필요하기에 초반 Learining Curve가 높은 편이라고 합니다. 그러나 자동 Sharding과 HDFS를 사용하기에 높은 가용성 그리고 노드 추가를 통한 높은 scale out 성능을 고려한다면 좋은 옵션이라고 생각합니다. HBase에 자료가 많이 부족하지만, HBase 관련 커뮤니티가 활성화 되어서 다시 예전의 HBase의 명성을 되찾았으면 좋겠습니다.😄

 

 

참고: https://github.com/larsgeorge/hbase-book

'Hadoop > Hbase' 카테고리의 다른 글

HBase Table 관리  (0) 2022.05.02