Come posso utilizzare un database PostgreSQL come metastore esterno per Hive in Amazon EMR?
Desidero utilizzare un'istanza database Amazon Relational Database Service (Amazon RDS) per PostgreSQL come metastore esterno per Apache Hive in Amazon EMR.
Risoluzione
Prima di iniziare, tieni presente quanto segue:
- Questa soluzione presuppone che tu abbia già un database PostgreSQL attivo.
- Se utilizzi Amazon EMR versione 5.7 o precedente, scarica prima il driver JDBC PostgreSQL dal sito web pgJDBC. Quindi aggiungilo al percorso della libreria Hive (/usr/lib/hive/lib). Le versioni 5.8.0 e successive di Amazon EMR includono il driver JDBC PostgreSQL nel percorso della libreria Hive.
Configura un'istanza database PostgreSQL come metastore esterno per Hive
Completa i seguenti passaggi:
-
Crea un'istanza database Amazon RDS per PostgreSQL e il database.
Nota: puoi creare il database mentre crei l'istanza database dalla console Amazon Aurora e RDS. Puoi specificare il nome del database nel campo Nome database iniziale in Configurazione aggiuntiva. Oppure puoi connettere l'istanza database PostgreSQL e successivamente creare il database. -
Per consentire le connessioni sulla porta 5432 tra il database e il gruppo di sicurezza ElasticMapReduce-master, modifica il gruppo di sicurezza dell'istanza database. Per ulteriori informazioni, consulta Panoramica dei gruppi di sicurezza VPC.
-
Avvia un cluster Amazon EMR senza un metastore esterno. In questo caso, Amazon EMR utilizza il database MySQL predefinito.
-
Aggiorna la configurazione di Hive.
Di seguito è riportato un esempio di configurazione di Hive. Sostituisci i seguenti valori con i tuoi:
mypostgresql.testabcd1111.us-west-2.rds.amazonaws.com con l'endpoint della tua istanza database
mypgdb con il nome del tuo database PostgreSQL
database_username con il nome utente dell'istanza database
database_password con la password dell'istanza database[hadoop@ip-X-X-X-X ~]$ sudo vi /etc/hive/conf/hive-site.xml <property> <name>javax.jdo.option.ConnectionURL</name> <value>jdbc:postgresql://mypostgresql.testabcd1111.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> -
Per creare lo schema di PostgreSQL, esegui questi comandi:
[hadoop@ip-X-X-X-X ~]$ cd /usr/lib/hive/bin/[hadoop@ip-X-X-X-X bin]$ ./schematool -dbType postgres -initSchema Metastore connection URL: jdbc:postgresql://mypostgresql.testabcd1111.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 -
Per applicare gli aggiornamenti e le modifiche, arresta e avvia i servizi Hive.
[hadoop@ip-X-X-X-X bin]$ sudo initctl list |grep -i hivehive-server2 start/running, process 11818 hive-hcatalog-server start/running, process 12708 [hadoop@ip-X-X-X-X9 bin]$ sudo stop hive-server2 hive-server2 stop/waiting [hadoop@ip-X-X-X-X bin]$ sudo stop hive-hcatalog-server hive-hcatalog-server stop/waiting [hadoop@ip-X-X-X-X bin]$ sudo start hive-server2 hive-server2 start/running, process 18798 [hadoop@ip-X-X-X-X bin]$ sudo start hive-hcatalog-server hive-hcatalog-server start/running, process 19614Nota: per automatizzare i passaggi da 5 a 7, esegui questo script bash (hive_postgres_emr_step.sh) come processo per fasi nel cluster EMR.
## Automated Bash script to update the hive-site.xml and restart Hive ## Parameters rds_db_instance_endpoint='<rds_db_instance_endpoint>' rds_db_instance_port='<rds_db_instance_port>' rds_db_name='<rds_db_name>' rds_db_instance_username='<rds_db_instance_username>' rds_db_instance_password='<rds_db_instance_username>' ############################# Copying the original hive-site.xml sudo cp /etc/hive/conf/hive-site.xml /tmp/hive-site.xml ############################# Changing the JDBC URL old_jdbc=`grep "javax.jdo.option.ConnectionURL" -A +3 -B 1 /tmp/hive-site.xml | grep "<value>" | xargs` sudo sed -i "s|$old_jdbc|<value>jdbc:postgresql://$rds_db_instance_endpoint:$rds_db_instance_port/$rds_db_name</value>|g" /tmp/hive-site.xml ############################# Changing the Driver name old_driver_name=`grep "javax.jdo.option.ConnectionDriverName" -A +3 -B 1 /tmp/hive-site.xml | grep "<value>" | xargs` sudo sed -i "s|$old_driver_name|<value>org.postgresql.Driver</value>|g" /tmp/hive-site.xml ############################# Changing the database user old_db_username=`grep "javax.jdo.option.ConnectionUserName" -A +3 -B 1 /tmp/hive-site.xml | grep "<value>" | xargs` sudo sed -i "s|$old_db_username|<value>$rds_db_instance_username</value>|g" /tmp/hive-site.xml ############################# Changing the database password and description connection_password=`grep "javax.jdo.option.ConnectionPassword" -A +3 -B 1 /tmp/hive-site.xml | grep "<value>" | xargs` sudo sed -i "s|$connection_password|<value>$rds_db_instance_password</value>|g" /tmp/hive-site.xml old_password_description=`grep "javax.jdo.option.ConnectionPassword" -A +3 -B 1 /tmp/hive-site.xml | grep "<description>" | xargs` new_password_description='<description>the password for the DB instance</description>'sudo sed -i "s|$password_description|$new_password_description|g" /tmp/hive-site.xml ############################# Moving hive-site to backup sudo mv /etc/hive/conf/hive-site.xml /etc/hive/conf/hive-site.xml_bkup sudo mv /tmp/hive-site.xml /etc/hive/conf/hive-site.xml ############################# Init Schema for Postgres /usr/lib/hive/bin/schematool -dbType postgres -initSchema ############################# Restart Hive ## Check Amazon Linux version and restart Hive OS_version=`uname -r` if [[ "$OS_version" == *"amzn2"* ]]; then echo "Amazon Linux 2 instance, restarting Hive..." sudo systemctl stop hive-server2 sudo systemctl stop hive-hcatalog-server sudo systemctl start hive-server2 sudo systemctl start hive-hcatalog-server elif [[ "$OS_version" == *"amzn1"* ]]; then echo "Amazon Linux 1 instance, restarting Hive" sudo stop hive-server2 sudo stop hive-hcatalog-server sudo start hive-server2 sudo start hive-hcatalog-server else echo "ERROR: OS version different from AL1 or AL2." fi echo "--------------------COMPLETED--------------------"Assicurati di sostituire i seguenti valori nello script:
- rds_db_instance_endpoint con l'endpoint della tua istanza database
- rds_db_instance_port con la porta della tua istanza database
- rds_db_name con il nome del database PostgreSQL
- rds_db_instance_username con il nome utente dell'istanza database
- rds_db_instance_password con la password dell'istanza database
Carica lo script in Amazon S3
Puoi eseguire lo script come processo per fasi tramite la console Amazon EMR, l'Interfaccia della linea di comando AWS (AWS CLI) o l'API. Per eseguire lo script utilizzando la console Amazon EMR, procedi come segue:
-
Apri la console Amazon EMR.
-
Nella pagina Elenco dei cluster , seleziona il collegamento al cluster.
-
Nella pagina Dettagli del cluster, scegli la scheda Fasi.
-
Nella scheda Fasi, scegli Aggiungi fase.
-
Nella finestra di dialogo Aggiungi fase, mantieni i valori predefiniti per Tipo di fase e Nome.
-
Per Posizione JAR, inserisci quanto segue:
command-runner.jar -
Per Argomenti, inserisci quanto segue:
bash -c "aws s3 cp s3://example_bucket/script/hive_postgres_emr_step.sh .; chmod +x hive_postgres_emr_step.sh; ./hive_postgres_emr_step.sh"Sostituisci la posizione S3 nel comando con la posizione in cui hai archiviato lo script.
-
Scegli Aggiungi per esegui il processo per fasi.
Verifica gli aggiornamenti della configurazione di Hive
Completa i seguenti passaggi:
-
Accedi alla shell Hive e crea una tabella Hive.
Nota: sostituisci test_postgres nell'esempio seguente con il nome della tua tabella Hive.[hadoop@ip-X-X-X-X bin]$ 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 -
Per installare PostgreSQL, esegui questo comando:
[hadoop@ip-X-X-X-X bin]$ sudo yum install postgresql -
Connettiti all'istanza database PostgreSQL utilizzando la riga di comando.
Sostituisci i seguenti valori nel comando:
mypostgresql.testabcd1111.us-west-2.rds.amazonaws.com con l'endpoint della tua istanza database
mypgdb con il nome del tuo database PostgreSQL
database_username con il nome utente dell'istanza database[hadoop@ip-X-X-X-X bin]$ psql --host=mypostgresql.testabcd1111.us-west-2.rds.amazonaws.com --port=5432 --username=database_username --password --dbname=mypgdb -
Quando richiesto, inserisci la password dell'istanza database.
-
Per verificare di poter accedere alla tabella Hive che hai creato in precedenza, esegui questo comando:
mypgdb=> 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)
Informazioni correlate
Configurazione di un metastore esterno per Hive
Connessione a un'istanza database che esegue il motore di database PostgreSQL
- Argomenti
- Analytics
- Tag
- Amazon EMR
- Lingua
- Italiano
