Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get/generate the create statement for an existing hive table?

Tags:

sql

hive

hiveql

Assuming you have "table" already in Hive, is there a quick way like other databases to be able to get the "CREATE" statement for that table?

like image 860
Rolando Avatar asked Aug 08 '13 19:08

Rolando


People also ask

Which Hive command is used to create internal table for worker?

hive> CREATE TABLE IF NOT EXISTS employee ( eid int, name String, salary String, destination String) COMMENT 'Employee details' ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n' STORED AS TEXTFILE; If you add the option IF NOT EXISTS, Hive ignores the statement in case the table already exists.


2 Answers

Steps to generate Create table DDLs for all the tables in the Hive database and export into text file to run later:

step 1)
create a .sh file with the below content, say hive_table_ddl.sh

#!/bin/bash rm -f tableNames.txt rm -f HiveTableDDL.txt hive -e "use $1; show tables;" > tableNames.txt   wait cat tableNames.txt |while read LINE    do    hive -e "use $1;show create table $LINE;" >>HiveTableDDL.txt    echo  -e "\n" >> HiveTableDDL.txt    done rm -f tableNames.txt echo "Table DDL generated" 

step 2)

Run the above shell script by passing 'db name' as paramanter

>bash hive_table_dd.sh <<databasename>> 

output :

All the create table statements of your DB will be written into the HiveTableDDL.txt

like image 27
Aditya Avatar answered Sep 22 '22 07:09

Aditya


As of Hive 0.10 this patch-967 implements SHOW CREATE TABLE which "shows the CREATE TABLE statement that creates a given table, or the CREATE VIEW statement that creates a given view."

Usage:

SHOW CREATE TABLE myTable; 
like image 133
Lukas Vermeer Avatar answered Sep 25 '22 07:09

Lukas Vermeer