forked from awslabs/amazon-redshift-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redshift_unload_copy.py
executable file
·177 lines (147 loc) · 8.61 KB
/
redshift_unload_copy.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
#!/usr/bin/env python
"""
Usage:
python redshift-unload-copy.py <config file> <region>
* Copyright 2014, Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
"""
import json
import sys
import logging
import util.log
from global_config import GlobalConfigParametersReader, config_parameters
from util.s3_utils import S3Helper, S3Details
from util.redshift_cluster import RedshiftCluster
from util.resources import ResourceFactory, TableResource, DBResource
from util.tasks import TaskManager, FailIfResourceDoesNotExistsTask, CreateIfTargetDoesNotExistTask, \
FailIfResourceClusterDoesNotExistsTask, UnloadDataToS3Task, CopyDataFromS3Task, CleanupS3StagingAreaTask, \
NoOperationTask
logger = util.log.setup_custom_logger('UnloadCopy')
logger.info('Starting the UnloadCopy Utility')
region = None
encryptionKeyID = 'alias/RedshiftUnloadCopyUtility'
def usage():
print("Redshift Unload/Copy Utility")
print("Exports data from a source Redshift database to S3 as an encrypted dataset, "
"and then imports into another Redshift Database")
print("")
print("Usage:")
print("python redshift_unload_copy.py <configuration> <region>")
print(" <configuration> Local Path or S3 Path to Configuration File on S3")
print(" <region> Region where Configuration File is stored (S3) "
"and where Master Keys and Data Exports are stored")
sys.exit(-1)
class ConfigHelper:
def __init__(self, config_path, s3_helper=None):
self.s3_helper = s3_helper
if config_path.startswith("s3://"):
if s3_helper is None:
raise Exception('No region set to get config file but it resides on S3')
self.config = s3_helper.get_json_config_as_dict(config_path)
else:
with open(config_path) as f:
self.config = json.load(f)
class UnloadCopyTool:
# noinspection PyDefaultArgument
def __init__(self,
config_file,
region_name,
global_config_values=GlobalConfigParametersReader().get_default_config_key_values()):
for key, value in global_config_values.items():
config_parameters[key] = value
self.region = region_name
self.s3_helper = S3Helper(self.region)
# load the configuration
self.config_helper = ConfigHelper(config_file, self.s3_helper)
self.task_manager = TaskManager()
self.barrier_after_all_cluster_pre_tests = NoOperationTask()
self.task_manager.add_task(self.barrier_after_all_cluster_pre_tests)
self.barrier_after_all_resource_pre_tests = NoOperationTask()
self.task_manager.add_task(self.barrier_after_all_resource_pre_tests)
src_config = self.config_helper.config['unloadSource']
dest_config = self.config_helper.config['copyTarget']
if(src_config['tableNames']):
src_tables = src_config['tableNames']
dest_tables = dest_config['tableNames']
logger.info("Migrating multiple tables")
if( not dest_tables or len(src_tables) != len(dest_tables) ):
logger.fatal("When migrating multiple tables 'tableNames' property must be configured in unloadSource and copyTarget, and be the same length")
raise NotImplementedError
for idx in range(0,len(src_tables)):
logger.info("Migrating table: " + src_tables[idx])
src_config['tableName'] = src_tables[idx]
dest_config['tableName'] = dest_tables[idx]
source = ResourceFactory.get_source_resource_from_config_helper(self.config_helper, self.region)
destination = ResourceFactory.get_target_resource_from_config_helper(self.config_helper, self.region)
self.add_src_dest_tasks(source,destination,global_config_values)
else:
# Migrating a single table
source = ResourceFactory.get_source_resource_from_config_helper(self.config_helper, self.region)
destination = ResourceFactory.get_target_resource_from_config_helper(self.config_helper, self.region)
self.add_src_dest_tasks(source,destination,global_config_values)
self.task_manager.run()
def add_src_dest_tasks(self,source,destination,global_config_values):
# TODO: Check whether both resources are of type table if that is not the case then perform other scenario's
if isinstance(source, TableResource):
if isinstance(destination, DBResource):
if not isinstance(destination, TableResource):
destination = ResourceFactory.get_table_resource_from_merging_2_resources(destination, source)
if global_config_values['tableName'] and global_config_values['tableName'] != 'None':
destination.set_table(global_config_values['tableName'])
self.add_table_migration(source, destination, global_config_values)
else:
logger.fatal('Destination should be a database resource')
raise NotImplementedError
pass
else:
# TODO: add additional scenario's
# For example if both resources are of type schema then create target schema and migrate all tables
logger.fatal('Source is not a Table, this type of unload-copy is currently not supported.')
raise NotImplementedError
def add_table_migration(self, source, destination, global_config_values):
if global_config_values['connectionPreTest']:
if not global_config_values['destinationTablePreTest']:
destination_cluster_pre_test = FailIfResourceClusterDoesNotExistsTask(resource=destination)
self.task_manager.add_task(destination_cluster_pre_test,
dependency_of=self.barrier_after_all_cluster_pre_tests)
if not global_config_values['sourceTablePreTest']:
source_cluster_pre_test = FailIfResourceClusterDoesNotExistsTask(resource=source)
self.task_manager.add_task(source_cluster_pre_test,
dependency_of=self.barrier_after_all_cluster_pre_tests)
if global_config_values['destinationTablePreTest']:
if global_config_values['destinationTableAutoCreate']:
destination_cluster_pre_test = FailIfResourceClusterDoesNotExistsTask(resource=destination)
self.task_manager.add_task(destination_cluster_pre_test,
dependency_of=self.barrier_after_all_cluster_pre_tests)
else:
destination_table_pre_test = FailIfResourceDoesNotExistsTask(destination)
self.task_manager.add_task(destination_table_pre_test,
dependency_of=self.barrier_after_all_resource_pre_tests,
dependencies=self.barrier_after_all_cluster_pre_tests)
if global_config_values['sourceTablePreTest']:
source_table_pre_test = FailIfResourceDoesNotExistsTask(source)
self.task_manager.add_task(source_table_pre_test,
dependency_of=self.barrier_after_all_resource_pre_tests,
dependencies=self.barrier_after_all_cluster_pre_tests)
if global_config_values['destinationTableAutoCreate']:
create_target = CreateIfTargetDoesNotExistTask(
source_resource=source,
target_resource=destination
)
self.task_manager.add_task(create_target,
dependency_of=self.barrier_after_all_resource_pre_tests,
dependencies=self.barrier_after_all_cluster_pre_tests)
s3_details = S3Details(self.config_helper, source, encryption_key_id=encryptionKeyID)
unload_data = UnloadDataToS3Task(source, s3_details)
self.task_manager.add_task(unload_data, dependencies=self.barrier_after_all_resource_pre_tests)
copy_data = CopyDataFromS3Task(destination, s3_details)
self.task_manager.add_task(copy_data, dependencies=unload_data)
s3_cleanup = CleanupS3StagingAreaTask(s3_details)
self.task_manager.add_task(s3_cleanup, dependencies=copy_data)
def main(args):
global region
global_config_reader = GlobalConfigParametersReader()
global_config_values = global_config_reader.get_config_key_values_updated_with_cli_args(args)
UnloadCopyTool(global_config_values['s3ConfigFile'], global_config_values['region'], global_config_values)
if __name__ == "__main__":
main(sys.argv)