Skip to content

Instantly share code, notes, and snippets.

@saisgit
Last active April 28, 2020 14:48
Show Gist options
  • Select an option

  • Save saisgit/eea76e4b50054bb301f060418f396d05 to your computer and use it in GitHub Desktop.

Select an option

Save saisgit/eea76e4b50054bb301f060418f396d05 to your computer and use it in GitHub Desktop.
#Essential Libraries
import os
import re
import sys
import ConfigParser
#Create File Method. Take 2 parameters:1)Filename 2)Data
def createFile(fName,data):
file = open(fName,'w+')
file.write(data)
file.close()
return "HQL Created"
inputFile = sys.argv[1] #From which file the column names are to be extracted.
tableName = sys.argv[2]
f = open(inputFile,'r')
print f.name
line = f.readline() #Retrieves the first line of the input file
header = re.split('\|',line) #Splits the line based on delimiter and adds it to the header list
f.close()
statement_prefix = "CREATE TABLE dbName." + tableName + "("
for i in range(len(header)):
statement_prefix = statement_prefix + ('\n{} STRING,').format(header[i].upper().strip(),str(header))
#Appends every element from list to the file with datatype String
statement_suffix = "\n" + "COMMENT 'This is an example table'" + "\n" + "ROW FORMAT DELIMITED" + "\n" + "FIELDS TERMINATED BY '|'" + "\n" + "STORED AS TEXTFILE" + "\n" + "LOCATION '/etlhive';"
statement_hql = statement_prefix[:-1] + "\n)" + statement_suffix #Merge all the elements into one string
createFile(tableName + ".hql",statement_hql) #Call the createFile method to create the hql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment