111

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?

mrsrinivas
  • 34,112
  • 13
  • 125
  • 125
Rolando
  • 58,640
  • 98
  • 266
  • 407

4 Answers4

239

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;
Lukas Vermeer
  • 5,920
  • 2
  • 16
  • 19
34

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

Vladislav Varslavans
  • 2,775
  • 4
  • 18
  • 33
Aditya
  • 2,385
  • 18
  • 25
  • 1
    how we could make sure that the buckets and storage format will going to be replicate into new table. – Indrajeet Gour Dec 19 '17 at 05:43
  • You should also append a semicolon after each statement so that the script can be executed by saying hive -f HiveTableDDL.txt. – Muton Apr 16 '18 at 11:02
  • This script is failing for me with the below error: Error while compiling statement: FAILED: ParseException line 1:18 cannot recognize input near '|' '|' in table name I am executing this script with beeline as HDP 3.0 does not support hive-shell access – Abhinav Mar 19 '19 at 20:00
  • @cfeduke this script gives the location of hive tables. how to skip location part inorder to run these ddls in another hive location ? alter on more than 1000 tables is not possible – user1 Jun 05 '20 at 16:59
2

Describe Formatted/Extended will show the data definition of the table in hive

hive> describe Formatted dbname.tablename;
Stu Thompson
  • 38,370
  • 19
  • 110
  • 156
user2637464
  • 2,366
  • 1
  • 14
  • 5
1
# !/bin/bash 
for DB in `beeline --showHeader=false --outputformat=tsv2 -e "show databases;"` 
do
for Tab in `beeline --showHeader=false --outputformat=tsv2 -e "use $DB; show tables;"` 
do 
beeline --showHeader=false --outputformat=tsv2 -e "show create table $DB.$Tab;" >$DB.$Tab.hql
done
done  
  • this code will give you all tables DDL from all databases – Abhishek Suryawanshi Aug 02 '22 at 06:56
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 03 '22 at 23:22