환경변수로 DB_DRIVER=postgres 설정을 해주어야 기본 Derby 데이터베이스에서 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
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".
wget https://jdbc.postgresql.org/download/postgresql-42.2.5.jar
<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>
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
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)
<property>
<name>hive.metastore.uris</name>
<value>thrift://hms:9083</value>
</property>
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
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)
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 &
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