如何在 Amazon EMR 上将 PostgreSQL 数据库用作 Hive 外部元数据仓?
上次更新时间:2019 年 10 月 10 日
如何在 Amazon EMR 上,使用 Amazon Relational Database Service (Amazon RDS) 将 PostgreSQL 数据库实例用作 Apache Hive 的外部元数据仓?
解决方法
在开始之前,请注意以下几点:
此解决方案假定您已经有一个活跃的 PostgreSQL 数据库。 如果您使用的是 Amazon EMR 5.7 版或更低版本,请 下载 PostgreSQL JDBC 驱动程序。然后,将其添加到 Hive 库路径 (/usr/lib/hive/lib)。Amazon EMR 版本 5.8.0 及更高版本会在 Hive 库路径中附带 PostgreSQL JDBC 驱动程序。要将 PostgreSQL 数据库实例配置为 Hive 的外部元数据仓:
1.创建一个 PostgreSQL 数据库实例。
2.修改数据库实例安全组,以允许在端口 5432 上连接数据库 和 ElasticMapReduce-master 安全组。有关更多信息,请参阅 VPC 安全组。
3.启动一个没有外部元数据仓的 Amazon EMR 集群(仅默认的 MySQL 数据库)。
4. 使用 SSH 连接到主节点。
5.替换 Hive 配置的以下属性。替换示例中的以下值:
mypostgresql.c6c8mwvfdgv0.us-west-2.rds.amazonaws.com:数据库实例的终端节点
mypgdb:PostegreSQL 数据库名称
database_username:数据库实例用户名
database_password:数据库实例密码
$ sudo vi /etc/hive/conf/hive-site.xml
<property>
<name>javax.jdo.option.ConnectionURL</name>
<value>jdbc:postgresql://mypostgresql.c6c8mwvfdgv0.us-west-2.rds.amazonaws.com:5432/mypgdb</value>
<description>PostgreSQL JDBC driver connection URL</description>
</property>
<property>
<name>javax.jdo.option.ConnectionDriverName</name>
<value>org.postgresql.Driver</value>
<description>PostgreSQL metastore driver class name</description>
</property>
<property>
<name>javax.jdo.option.ConnectionUserName</name>
<value>database_username</value>
<description>the username for the DB instance</description>
</property>
<property>
<name>javax.jdo.option.ConnectionPassword</name>
<value>database_password</value>
<description>the password for the DB instance</description>
</property>
6.运行以下命令,创建 PostgreSQL 架构:
$ cd /usr/lib/hive/bin/
$ ./schematool -dbType postgres -initSchema
Metastore connection URL: jdbc:postgresql://mypostgresql.c6c8mwvfdgv0.us-west-2.rds.amazonaws.com:5432/mypgdb
Metastore Connection Driver : org.postgresql.Driver
Metastore connection User: test
Starting metastore schema initialization to 2.3.0
Initialization script hive-schema-2.3.0.postgres.sql
Initialization script completed
schemaTool completed
7.关闭并启动 Hive 服务,以使新设置生效:
$ sudo initctl list |grep -i hive
hive-server2 start/running, process 11818
hive-hcatalog-server start/running, process 12708
$ sudo stop hive-server2
hive-server2 stop/waiting
$ sudo stop hive-hcatalog-server
hive-hcatalog-server stop/waiting
$ sudo start hive-server2
hive-server2 start/running, process 18798
$ sudo start hive-hcatalog-server
hive-hcatalog-server start/running, process 19614
8.登录 Hive shell 并创建 Hive 表。在以下示例中,表将被命名为“test_postgres”。
$ hive
Logging initialized using configuration in file:/etc/hive/conf.dist/hive-log4j2.properties Async: true
hive> show databases;
OK
default
Time taken: 0.569 seconds, Fetched: 1 row(s)
hive> create table test_postgres(a int,b int);
OK
Time taken: 0.708 seconds
9.安装 PostgreSQL:
$ sudo yum install postgresql
10.使用命令行连接 PostgreSQL 数据库实例。替换示例中的以下值:
mypostgresql.c6c8mwvfdgv0.us-west-2.rds.amazonaws.com:数据库实例的终端节点
mypgdb:PostegreSQL 数据库名称
database_username:数据库实例用户名
$ psql --host=mypostgresql.c6c8mwvfdgv0.us-west-2.rds.amazonaws.com --port=5432 --username=database_username --password --dbname=mypgdb
11. 弹出提示时,输入数据库实例密码。
12.运行以下命令,确认您可以访问之前创建的 Hive 表:
$ select * from "TBLS";
TBL_ID | CREATE_TIME | DB_ID | LAST_ACCESS_TIME | OWNER | RETENTION | SD_ID | TBL_NAME | TBL_TYPE | VIEW_EXPANDED_TEXT | VIEW_ORIGINAL_TEXT | IS_REWRITE_ENABLED
--------+-------------+-------+------------------+--------+-----------+-------+---------------+---------------+--------------------+--------------------+--------------------
1 | 1555014961 | 1 | 0 | hadoop | 0 | 1 | test_postgres | MANAGED_TABLE | | | f
(1 row)
现在,您的 Amazon EMR 集群正在将 PostgreSQL 数据库用作 Hive 的外部元数据仓。