/
BigQuery_Tool.py
80 lines (63 loc) · 2.02 KB
/
BigQuery_Tool.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 4 13:42:11 2015
@author: econ24
"""
import sys, json
import Google_Service_Builder, BigQuery_Configuration, BigQuery_Basics, BigQuery_Append
import DataUploader
def doHelp():
print \
'''
########################################
description: tool for executing basic bigQuery commands
usage: $ BigQuery_Tool.py [config_file_path] [command] [options]
########################################
'''
def main():
if len(sys.argv) <= 2:
doHelp()
return
elif len(sys.argv) >= 3:
configFile = sys.argv[1]
action = sys.argv[2].lower()
args = sys.argv[3:]
else:
return
if action == "configure":
BigQuery_Configuration.configureBigquery(configFile, args)
return
defaults = BigQuery_Configuration.loadConfig(configFile)
args, params = BigQuery_Configuration.loadParams(args)
for key in defaults:
if key not in params:
params[key] = defaults[key]
try:
bigquery = Google_Service_Builder.buildBigQuery(**params)
except Exception as e:
print e
print "failed to build bigquery"
return
if action == "list":
try:
BigQuery_Basics.listTables(bigquery, **params)
except Exception as e:
print e
print "failed to list tables in: %s.%s" % (params["projectId"], params["datasetId"])
elif action == "create":
try:
with open(args[0]) as f:
schema = json.load(f)
BigQuery_Basics.createTable(bigquery, schema, **params)
except Exception as e:
print e
print "failed to create table: %s" % params["tableId"]
elif action == "status":
BigQuery_Basics.getStatus(bigquery, params["projectId"], args[0])
elif action == "upload":
if not DataUploader.upload(args[0], params["bucket"]):
print "<DataLoader> Failed to upload file, exiting"
#end main
if __name__ == "__main__":
main()