-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprestodemo.py
67 lines (55 loc) · 2.01 KB
/
prestodemo.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
#!/usr/bin/env python3
import argparse
import configparser
import logging
import pandas
import prestodb
import sys
logging.basicConfig(stream=sys.stdout,
format="%(asctime)s - %(levelname)s - %(message)s")
def parse_command_line():
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--config",
help="Configuration in INI file format (default: %(default)s)",
default="prestodemo.ini", metavar="FILE")
parser.add_argument("-l", "--log",
help="Logging level: %(choices)s (default: %(default)s)",
choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
default="INFO", metavar="LEVEL")
args = parser.parse_args()
logging.getLogger().setLevel(getattr(logging, args.log, None))
logging.debug("Parsed command line arguments: %s" %args)
return read_config(args.config)
def read_config(configfile):
logging.debug("Reading configuration file: %s" %configfile)
config = configparser.ConfigParser()
config.read(configfile)
return config
def main():
config = parse_command_line()
try:
conn=prestodb.dbapi.connect(
host=config["DB"]["host"],
port=config["DB"]["port"],
user=config["Creds"]["user"],
catalog=config["DB"]["catalog"],
schema=config["DB"]["schema"],
http_scheme=config["DB"]["http_scheme"],
auth=prestodb.auth.BasicAuthentication(
config["Creds"]["user"], config["Creds"]["password"]),
)
cur = conn.cursor()
logging.info("Executing query: %s" %config["DB"]["query"])
cur.execute(config["DB"]["query"])
results = cur.fetchall()
cols = [col[0] for col in cur.description]
data = pandas.DataFrame(results, columns=cols)
logging.info("Results: %s Rows, %s Cols" %(data.shape[0], data.shape[1]))
except Exception as e:
logging.error("Error: %s" %e)
finally:
cur.cancel()
conn.close()
return
if __name__ == "__main__":
main()