-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_table.py
More file actions
executable file
·60 lines (52 loc) · 2.18 KB
/
create_table.py
File metadata and controls
executable file
·60 lines (52 loc) · 2.18 KB
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
#!/usr/bin/env python3
import argparse
from os.path import dirname, join
from socket import getfqdn
from presto import PrestoCliProxy
parser = argparse.ArgumentParser()
parser.add_argument('-P', '--presto-cmd', action='store',
default=join(dirname(__file__), 'presto.sh'),
help='Path to the script that runs the Presto CLI.')
parser.add_argument('-S', '--presto-server', action='store',
default=getfqdn() + ':8080',
help='URL as <host>:<port> of the Presto server.')
parser.add_argument('-C', '--presto-catalogue', action='store', default='hive',
help='Default catalogue to use in Presto.')
parser.add_argument('--presto-schema', action='store', default='default',
help='Default schema to use in Presto.')
parser.add_argument('-T', '--table-name',
help='Name of the table that should be created.')
parser.add_argument('-L', '--location',
help='Location of the external files (on HDFS or S3).')
parser.add_argument('-V', '--view-name',
help='Name of the view that should be created.')
parser.add_argument('--variant', default='native',
help='Variant of the tables to create (native or shredded).')
args = parser.parse_args()
# Assemble paths to SQL files
base_dir = dirname(__file__)
create_table_file = join(base_dir, 'create_table_{}.sql'.format(args.variant))
create_view_file = join(base_dir, 'create_view_{}.sql'.format(args.variant))
# Create Presto client
presto = PrestoCliProxy(args.presto_cmd, args.presto_server,
args.presto_catalogue, args.presto_schema)
# Delete table if exists
presto.run('DROP TABLE IF EXISTS {};'.format(args.table_name))
# Create new table
with open(create_table_file, 'r') as f:
query = f.read()
query = query.format(
table_name=args.table_name,
location=args.location,
)
presto.run(query)
# Create view
if args.variant in ['shredded']:
# Create or replace view
with open(create_view_file, 'r') as f:
query = f.read()
query = query.format(
table_name=args.table_name,
view_name=args.view_name,
)
presto.run(query)