Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# -*- coding: utf-8 -*-
"""
Copyright 2015 Sho Shimauchi
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
hive_create_table_with_many_partitions.py
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Generate HQL to create table with many partitions.
usage: create_table_with_many_partitions.py [-n <num of partitions>] [-t <table name>] > [output.txt]
sample output:
CREATE TABLE IF NOT EXISTS test_table (id INT, name STRING) PARTITIONED BY (pid INT);
ALTER TABLE test_table ADD IF NOT EXISTS
PARTITION (pid = 1) PARTITION (pid = 2) ... ;
"""
import argparse
def parse_args():
""" Parse and return command line args """
parser = argparse.ArgumentParser()
parser.add_argument("-n", "--num_partitions", required=False, type=int, default=10)
parser.add_argument("-t", "--table_name", required=False, default="test_table")
return parser.parse_args()
def generate_hql(table_name="test_table", num_partitions=10):
s_create_table = "CREATE TABLE IF NOT EXISTS {0} (id INT, name STRING) PARTITIONED BY (pid INT);".format(table_name)
s_add_partitions_1 = "ALTER TABLE {0} ADD IF NOT EXISTS ".format(table_name)
l_partitions = []
for i in range(num_partitions):
l_partitions.append("PARTITION (pid = {0})".format(str(i)))
hql = s_create_table + s_add_partitions_1 + ' '.join(l_partitions) + ';'
return hql
if __name__ == '__main__':
args = parse_args()
print(generate_hql(table_name=args.table_name,
num_partitions=args.num_partitions
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.