0

I am a novice in Unix and need some help. I have a excel file in the below format.

Table,Column,Datatype,Inputformat

TableA,col1,int,TEXTFILE

TableA,col2,string,TEXTFILE

TableA,col3,float,TEXTFILE

TableA,col4,int,TEXTFILE

TableB,col1,string,TEXTFILE

TableB,col2,int,TEXTFILE

TableB,col3,int,TEXTFILE

Likewise I have records for 100 tables.

I need to create a ddl statement for hive table creation for all those 100 tables using unix.

e.g:

create table TableA(col1 int ,col2 string,col3 float,col4 int) STORED AS TEXTFILE;

create table TableB(col1 string ,col2 int,col3 int) STORED AS TEXTFILE;

Can you please help me with the approach.

Thanks,

0

1 Answer 1

1

You may prepare an awk script

awk  -F ',' '{ 
                 a[$1] = a[$1] " " $2 " " $3 ","; #read the column/dtype into array
                 b[$1] = $4 ;                     #read the file format
         }END{
              for (i in a ) #loop through the concatenated string
              { gsub(/,$/, ")" ,a[i] );           #replace last comma with ")"
                      print "CREATE TABLE " i " (" a[i] " STORED AS " b[i] ;
                  } 
                }' filename
Sign up to request clarification or add additional context in comments.

2 Comments

NayakWhat if I want to give the Inputformat dynamically? e.g: Table,Column,Datatype,Inputformat TableA,col1,int,TEXTFILE TableA,col2,string,TEXTFILE TableA,col3,float,TEXTFILE TableA,col4,int,TEXTFILE TableB,col1,string,SEQUENCEFILE TableB,col2,int,SEQUENCEFILE TableB,col3,int,SEQUENCEFILE
@SumitD : I've edited the script to handle that now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.