-
Notifications
You must be signed in to change notification settings - Fork 3
/
Tableau TDE API with Teradata Connection.py
277 lines (235 loc) · 10.3 KB
/
Tableau TDE API with Teradata Connection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import dataextract as tde
import pyodbc as odbc
import getpass, os, datetime, csv, time, sys, getopt, subprocess
global dir
global tp
global ts
global td
global outputfile
global fiscalWeek
global startTime
def runQuery():
startTime = time.clock()
# Get Teradata User Information
uid = raw_input("Enter UID for Teradata: ")
pwd = getpass.getpass()
# Set Teradata Connection
print "Connecting to Teradata..."
conn = odbc.connect('DRIVER={TERADATA};DBCNAME=<DBCNAME;UID='+uid+';PWD='+pwd+';QUIETMODE=YES;')
# Create cursor for SELECT Query
curs = conn.cursor()
# Display Progress
print "Executing Query..."
# Set Query
curs.execute("""SELECT * FROM FOOBAR""")
## File Loop for CSV Adjustment / Moves
#fileDir = dir
#for root, dirs, filenames in os.walk(fileDir):
# for file in filenames:
# if ".csv" in file:
# os.rename(file, file.replace(".csv","<week/date>.csv",1))
# Create new CSV File for Consumption
csvName = outputfile
try:
csvFile = open(csvName+'.csv','wb')
except:
os.remove(csvName+'.csv')
csvFile = open(csvName+'.csv','wb')
# Push records into CSV file
print "Writting to CSV file for Consumption"
print "%i records returned" % (curs.rowcount)
writer = csv.writer(csvFile)
writer.writerow([i[0] for i in curs.description])
writer.writerows(curs)
# Close Connection
conn.close()
csvFile.close()
# TDE File Creation
# TDE Functions
def add_tde_col(colnum, row, val, t):
dateformat = '%Y-%m-%d'
datetimeformat = '%Y-%m-%d %H:%M:%S'
if t == tdeTypes['INTEGER']:
try:
convert = int(val)
row.setInteger(colnum, convert)
except ValueError:
row.setNull(colnum)
elif t == tdeTypes['DOUBLE']:
try:
convert = float(val)
row.setDouble(colnum, convert)
except ValueError:
row.setNull(colnum)
elif t == tdeTypes['BOOLEAN']:
try:
convert = int(val)
if convert > -1 and convert <= 1:
row.setBoolean(colnum, convert)
else:
row.setNull(colnum)
except ValueError:
row.setNull(colnum)
elif t == tdeTypes['DATE']:
try:
d = datetime.datetime.strptime(val, dateformat)
row.setDate(colnum, d.year, d.month, d.day)
except ValueError:
row.setNull(colnum)
elif t == tdeTypes['DATETIME']:
try:
d = datetime.datetime.strptime(val, datetimeformat)
row.setDateTime(colnum, d.year, d.month, d.day, d.hour, d.minute, d.second, d.microsecond)
except ValueError:
row.setNull(colnum)
elif t == tdeTypes['CHAR_STRING']:
row.setCharString(colnum, val)
elif t == tdeTypes['UNICODE_STRING']:
row.setString(colnum, val)
elif t == tdeTypes['DURATION']:
print "Don't know what to do here. ^_^"
else:
print 'Something is missing here.'
row.setNull(colnum)
# Tableau DataTypes Dictionary Creation
tdeTypes = {'INTEGER': 7, 'DOUBLE': 10, 'BOOLEAN': 11, 'DATE': 12, 'DATETIME': 13, 'DURATION': 14, 'CHAR_STRING': 15, 'UNICODE_STRING': 16}
def createTDEFile():
# This part is unique for each extract.
# Will need to modify..
# TODO: Make this more dynamic
################################################################################################
################################################################################################
csvSchema = []
csvSchema.append({'<COLUMNNAME>': tdeTypes['INTEGER']})
################################################################################################
################################################################################################
# Try to create the TDE File
try:
tdeFile = tde.Extract(outputfile+'.tde')
except:
os.remove(outputfile+'.tde')
tdeFile = tde.Extract(outputfile+'.tde')
# Open newly created CSV File
csvFile = open(outputfile+'.csv',"r")
reader = csv.reader(csvFile)
print 'Reading records from %s' % (outputfile+'.csv')
# Create TDE Table definition
tdeTableDef = tde.TableDefinition()
# Build TDE Table Definition from csv schema above
print 'Defined table schema:'
for index, item in enumerate(csvSchema):
for k, v in item.items():
print 'Column %i: %s <%s>' % (index, k, tdeTypes.keys()[tdeTypes.values().index(v)])
tdeTableDef.addColumn(k, v)
# Add table to TDE File
tdeTable = tdeFile.addTable("Extract", tdeTableDef)
# Iterate through rows and columns of csv and adding them to TDE File
print 'Writing records to %s' % (csvName+'.tde')
rownum = 0
for row in reader:
if rownum == 0:
header = row
else:
colnum = 0
tdeRow = tde.Row(tdeTableDef)
for col in row:
if colnum+1 > len(csvSchema):
print 'Something is missing here.'
break
add_tde_col(colnum, tdeRow, row[colnum], csvSchema[colnum].values()[0])
colnum += 1
tdeTable.insert(tdeRow)
tdeRow.close()
rownum += 1
print '%i rows added in total in %f seconds' % (rownum-1, time.clock()-startTime)
print 'Closing TDE and CSV File...'
tdeFile.close()
csvFile.close()
def RunUpdateProcess():
subprocess.check_output("C:\Program Files (x86)\Tableau\Tableau 8.0\bin\tableau addfiletoextract --file '"+ dir + "\\" + outputfile + '.csv' + "' --server "+ ts +" --project '"+ tp +"' --datasource '"+ td +"' --username <user> --password <pass>",
stderror = subprocess.STDOUT,
shell=False)
print stderror
def main(argv):
dir = ''
tp = ''
ts = ''
td = ''
outputfile = ''
fiscalWeek = ''
try:
opts, args = getopt.getopt(argv, "hv:d:tp:ts:td:v:fw:o:",["help","directory=","tableau-project=","tableau-server=","tableau-datasource=","version=","fiscalweek","output-file="])
except getopt.GetoptError:
print 'Tableau Data Extract File Generation from Teradata using SQL Query'
print ''
print 'USAGE: file -d <directory> -tp <projectname>'
print ' -ts <server-url> -td <datasourcename>'
print ' -o <datastorefilename>'
print '-h, --help Returns this help file'
print '-v, --version Version information'
print '-d, --directory Directory of file store'
print '-tp, --tableau-project Tableau Project Name'
print '-ts, --tableau-server Tableau Server Address (http)'
print '-td, --tableau-datasource Tableau Data Source Name'
print '-o, --output-file Filename to store data as.'
print ' This file will be the naem of'
print ' the file stored on the local'
print ' system.'
print '-fw, --fiscalweek Week for query to be ran for'
sys.exit(2)
for opt, arg in opts:
if opt in("-h","--help"):
print 'Tableau Data Extract File Generation from Teradata using SQL Query'
print ''
print 'USAGE: file -d <directory> -tp <projectname>'
print ' -ts <server-url> -td <datasourcename>'
print ' -o <datastorefilename>'
print '-h, --help Returns this help file'
print '-v, --version Version information'
print '-d, --directory Directory of file store'
print '-tp, --tableau-project Tableau Project Name'
print '-ts, --tableau-server Tableau Server Address (http)'
print '-td, --tableau-datasource Tableau Data Source Name'
print '-o, --output-file Filename to store data as.'
print ' This file will be the naem of'
print ' the file stored on the local'
print ' system.'
print '-fw, --fiscalweek Week for query to be ran for'
sys.exit()
elif opt in ("-d","--directory"):
dir = arg
elif opt in ("tp","--tableau-project"):
tp = arg
elif opt in ("ts","--tableau-server"):
ts = arg
elif opt in ("td","--tableau-datasource"):
td = arg
elif opt in ("-o","--output-file"):
outputfile = arg
elif opt in ("-fw","--fiscalweek"):
fiscalWeek = arg
if dir == '' or tp == '' or ts == '' or td == '' or outputfile == '' or fiscalWeek == '':
print 'Tableau Data Extract File Generation from Teradata using SQL Query'
print ''
print 'USAGE: file -d <directory> -tp <projectname>'
print ' -ts <server-url> -td <datasourcename>'
print ' -o <datastorefilename>'
print '-h, --help Returns this help file'
print '-v, --version Version information'
print '-d, --directory Directory of file store'
print '-tp, --tableau-project Tableau Project Name'
print '-ts, --tableau-server Tableau Server Address (http)'
print '-td, --tableau-datasource Tableau Data Source Name'
print '-o, --output-file Filename to store data as.'
print ' This file will be the naem of'
print ' the file stored on the local'
print ' system.'
print '-fw, --fiscalweek Week for query to be ran for'
sys.exit()
else:
runQuery()
createTDEFile()
RunUpdateProcess()
# Main Load function
if __name__ == "__main__":
main(sys.argv[1:])