CDH 환경에서 Kerberos 보안이 활성화된 HBase에 Java 애플리케이션으로 접속하려면 몇 가지 설정 단계가 필요하다.
필요 구성 요소 준비
먼저 Cloudera Manager 콘솔에서 HBase 관련 설정 파일들을 다운로드하여 로컬 프로젝트에 배치해야 한다. 이 파일들은 일반적으로 src/main/resources 디렉토리에 저장한다.
Kerberos 인증을 위해 다음 두 파일도 준비해야 한다:
- Kerberos 클라이언트 설정 파일 (
krb5.conf) - HBase 서비스용 keytab 파일
이들 역시 리소스 디렉토리에 위치시킨다.
네트워크 및 호스트 설정
로컬 개발 환경의 /etc/hosts 파일에 CDH 클러스터 노드들의 호스트 정보를 추가 등록한다.
Maven 의존성 구성
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
Java 코드 구현 예제
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.security.UserGroupInformation;
import java.io.IOException;
public class SecureHBaseConnector {
private static final Configuration config = initializeConfig();
private static Configuration initializeConfig() {
String projectRoot = System.getProperty("user.dir");
String keytabPath = projectRoot + "/src/main/resources/service.keytab";
String krbConfPath = projectRoot + "/src/main/resources/krb5.conf";
System.setProperty("java.security.krb5.conf", krbConfPath);
Configuration hbaseConfig = HBaseConfiguration.create();
hbaseConfig.set("hadoop.security.authentication", "kerberos");
hbaseConfig.set("hbase.security.authentication", "kerberos");
hbaseConfig.set("keytab.file", keytabPath);
hbaseConfig.set("kerberos.principal", "hbase-service@example.realm");
UserGroupInformation.setConfiguration(hbaseConfig);
try {
UserGroupInformation.loginUserFromKeytab("hbase-service@example.realm", keytabPath);
} catch (IOException exception) {
throw new RuntimeException("Failed to authenticate via Kerberos", exception);
}
return hbaseConfig;
}
public static void queryTableData(String targetTable) {
// 테이블 조회 로직 구현
System.out.println("Accessing table: " + targetTable);
}
public static void main(String[] args) {
queryTableData("customer_data");
}
}
실행 결과 확인
프로그램 실행 시 아래와 유사한 성공 메시지를 확인할 수 있다:
INFO security.UserGroupInformation: Login successful for user hbase-service@example.realm using keytab file [PATH]/service.keytab