Provisioning, HMS (3/3) / PostgreSQL 연동

Jeonghak Cho·2025년 3월 31일

Provisioning

목록 보기
19/44

프로비저닝 📗HMS, PostgreSQL 연결 - 도커 환경

환경변수로 DB_DRIVER=postgres 설정을 해주어야 기본 Derby 데이터베이스에서 postgresql 에 맞추어진 메타데이터를 자동으로 설치해 준다.

🏳️‍🌈 [궁금한점]

  • Derby 대신 PostgreSQL을 이용하는 방법

🔗[목차]

HMS와 외부 데이터베이스 연결

PostgreSQL 설치

네트워크를 mynetwork로 맞추어 hms, hs2와 통신이 가능하도록 한다.

docker run --name psql --network mynetwork -e POSTGRES_USER=hive -e POSTGRES_PASSWORD=hive -e POSTGRES_DB=hive_metastore -p 5432:5432 -d postgres:14

PostgreSQL 연결 확인

ubuntu@DESKTOP-SCOK45O:~/hive/db-derby-10.14.2.0-bin/lib$ d exec -it psql psql -U hive -d hive_metastore
psql (14.17 (Debian 14.17-1.pgdg120+1))
Type "help" for help.

hive_metastore=#

데이터베이스 연결 확인

hive_metastore=# \c hive_metastore
You are now connected to database "hive_metastore" as user "hive".

HMS 설치

  • DB 라이브러리 다운로드
wget https://jdbc.postgresql.org/download/postgresql-42.2.5.jar
  • 연결 속성 지정
    아래 속성 들이 로컬 ~/hive/hms 폴더의 hive-site.xml 에 추가되어야 한다.
    <property>
      <name>hive.metastore.uris</name>
      <value>thrift://localhost:9083</value>
      <description>URI for the Hive Metastore server</description>
    </property>
    <property>
      <name>javax.jdo.option.ConnectionURL</name>
      <value>jdbc:postgresql://psql:5432/hive_metastore</value>
      <description>JDBC connection URL for the Hive Metastore</description>
    </property>
    <property>
      <name>javax.jdo.option.ConnectionDriverName</name>
      <value>org.postgresql.Driver</value>
      <description>JDBC Driver for PostgreSQL</description>
    </property>
    <property>
      <name>javax.jdo.option.ConnectionUserName</name>
      <value>hive</value>
      <description>JDBC username for PostgreSQL</description>
    </property>
    <property>
      <name>javax.jdo.option.ConnectionPassword</name>
      <value>hive</value>
      <description>JDBC password for PostgreSQL</description>
    </property>
  • hms 서버 실행
    다운 받은 psql 라이브러리 마운트와 JDBC 연결 설정을 위한 hive-site.xml 마운트를 한다.환경변수로 DB_DRIVER=postgres 설정을 해주어야 기본 Derby 데이터베이스에서 postgresql 에 맞추어진 메타데이터를 자동으로 설치해 준다.
docker run -d -p 9083:9083 \
--env SERVICE_NAME=metastore \
--env DB_DRIVER=postgres \
--name hms \
-v ~/hive/db-derby-10.14.2.0-bin/lib:/opt/hive/derby/lib/ \
-v ~/hive/postgresql-42.2.5.jar:/opt/hive/lib/postgresql-42.2.5.jar \
-v ~/hive/hms/hive-site.xml:/opt/hive/conf/hive-site.xml \
--network mynetwork apache/hive:4.0.1
  • 메타데이터 확인
    PostgreSQL에 들어가서 테이블 들이 생성되어 있는 지 확인한다.
hive_metastore=# SELECT * FROM pg_catalog.pg_tables LIMIT 5;
 schemaname |    tablename     | tableowner | tablespace | hasindexes | hasrules | hastriggers | rowsecurity
------------+------------------+------------+------------+------------+----------+-------------+-------------
 public     | GLOBAL_PRIVS     | hive       |            | t          | f        | f           | f
 public     | NUCLEUS_TABLES   | hive       |            | t          | f        | f           | f
 pg_catalog | pg_statistic     | hive       |            | t          | f        | f           | f
 pg_catalog | pg_type          | hive       |            | t          | f        | f           | f
 public     | PARTITION_EVENTS | hive       |            | t          | f        | f           | f
(5 rows)
  • 현재 데이터베이스 목록 조회
hive_metastore=# SELECT current_database();
 current_database
------------------
 hive_metastore
(1 row)
  • 데이터베이스 목록 조회
hive_metastore=# \l
                                List of databases
      Name      | Owner | Encoding |  Collate   |   Ctype    | Access privileges
----------------+-------+----------+------------+------------+-------------------
 hive_metastore | hive  | UTF8     | en_US.utf8 | en_US.utf8 |
 postgres       | hive  | UTF8     | en_US.utf8 | en_US.utf8 |
 template0      | hive  | UTF8     | en_US.utf8 | en_US.utf8 | =c/hive          +
                |       |          |            |            | hive=CTc/hive
 template1      | hive  | UTF8     | en_US.utf8 | en_US.utf8 | =c/hive          +
                |       |          |            |            | hive=CTc/hive
(4 rows)

HS2 서버 설치

  • 연결 속성 지정
    아래 속성 들이 ~/hive/hs2 폴더의 hive-site.xml 에 추가되어야 한다.
 <property>
    <name>hive.metastore.uris</name>
    <value>thrift://hms:9083</value>
 </property>
  • hs2 서버 실행
docker run -d -p 10000:10000 -p 10002:10002 --env SERVICE_NAME=hiveserver2 \
--env SERVICE_OPTS="-Dhive.metastore.uris=thrift://hms:9083" \
--env IS_RESUME="true" \
-v ~/hive/hs2/hive-site.xml:/opt/hive/conf/hive-site.xml \
--network mynetwork \
--name hs2 apache/hive:4.0.1

데이터 생성

beeline 접속

docker exec -it hs2 beeline -u jdbc:hive2://localhost:10000

데이터베이스 조회

0: jdbc:hive2://localhost:10000> show databases;
INFO  : Completed executing command(queryId=hive_20250330004838_eb05d42f-6eac-468b-be74-fb86ee7328e1); Time taken: 0.082 seconds
+----------------+
| database_name  |
+----------------+
| default        |
+----------------+
1 row selected (1.465 seconds)

테이블 생성

1 row selected (1.496 seconds)
0: jdbc:hive2://localhost:10000> create database mydb;

0: jdbc:hive2://localhost:10000> show databases;
+----------------+
| database_name  |
+----------------+
| default        |
| mydb           |
+----------------+
2 rows selected (0.027 seconds)

0: jdbc:hive2://localhost:10000> create table employee (id int, name string, age int);

데이터 삽입

0: jdbc:hive2://localhost:10000> insert into employee(name,age) values ("alex",10);
----------------------------------------------------------------------------------------------
        VERTICES      MODE        STATUS  TOTAL  COMPLETED  RUNNING  PENDING  FAILED  KILLED
----------------------------------------------------------------------------------------------
Map 1 .......... container     SUCCEEDED      1          1        0        0       0       0
Reducer 2 ...... container     SUCCEEDED      1          1        0        0       0       0
----------------------------------------------------------------------------------------------
VERTICES: 02/02  [==========================>>] 100%  ELAPSED TIME: 1.23 s
----------------------------------------------------------------------------------------------

데이터 조회

0: jdbc:hive2://localhost:10000> SELECT * FROM EMPLOYEE;
+--------------+----------------+---------------+
| employee.id  | employee.name  | employee.age  |
+--------------+----------------+---------------+
| NULL         | alex           | 10            |
+--------------+----------------+---------------+
1 row selected (0.183 seconds)

메타 저장소 ( POSTGRESQL )

데이터베이스 목록

hive_metastore=# \l
                                List of databases
      Name      | Owner | Encoding |  Collate   |   Ctype    | Access privileges
----------------+-------+----------+------------+------------+-------------------
 hive_metastore | hive  | UTF8     | en_US.utf8 | en_US.utf8 |
 postgres       | hive  | UTF8     | en_US.utf8 | en_US.utf8 |
 template0      | hive  | UTF8     | en_US.utf8 | en_US.utf8 | =c/hive          +
                |       |          |            |            | hive=CTc/hive
 template1      | hive  | UTF8     | en_US.utf8 | en_US.utf8 | =c/hive          +
                |       |          |            |            | hive=CTc/hive
(4 rows)

schematool -dbType postgres -initSchema --verbose
hive --service metastore &

hive 사용자 권한 확인

SELECT grantee, table_schema, table_name, privilege_type
FROM information_schema.role_table_grants
WHERE table_schema = 'public'
AND table_name = 'TBLS'
AND grantee = 'hive'; 

현재 사용자 확인

hive_metastore=# SELECT current_user;
hive

생성 테이블 확인

SELECT * FROM public."TBLS";

      1 |  1743401238 |     1 |                0 | hive  | USER       |         0 |     1 | employee | EXTERNAL_TABLE |                    |                    | f                  |        0

0개의 댓글