forked from techgaun/dynamodb-copy-table
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamodb-copy-table.py
executable file
·97 lines (86 loc) · 2.94 KB
/
dynamodb-copy-table.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
#!/usr/bin/env python
from boto.dynamodb2.exceptions import ValidationException
from boto.dynamodb2.fields import HashKey, RangeKey
from boto.dynamodb2.layer1 import DynamoDBConnection
from boto.dynamodb2.table import Table
from boto.exception import JSONResponseError
from time import sleep
import sys
import os
if len(sys.argv) != 3:
print("Usage: %s <source_table_name> <destination_table_name>"% sys.argv[0])
sys.exit(1)
src_table = sys.argv[1]
dst_table = sys.argv[2]
region = os.getenv('AWS_DEFAULT_REGION', 'us-west-2')
# host = 'dynamodb.%s.amazonaws.com' % region
# ddbc = DynamoDBConnection(is_secure=False, region=region, host=host)
DynamoDBConnection.DefaultRegionName = region
ddbc = DynamoDBConnection()
# 1. Read and copy the target table to be copied
table_struct = None
try:
logs = Table(src_table, connection=ddbc)
table_struct = logs.describe()
except JSONResponseError:
print("Table %s does not exist" % src_table)
sys.exit(1)
print("*** Reading key schema from %s table" % src_table)
src = ddbc.describe_table(src_table)['Table']
hash_key = ''
range_key = ''
for schema in src['KeySchema']:
attr_name = schema['AttributeName']
key_type = schema['KeyType']
if key_type == 'HASH':
hash_key = attr_name
elif key_type == 'RANGE':
range_key = attr_name
# 2. Create the new table
table_struct = None
try:
new_logs = Table(dst_table,
connection=ddbc,
schema=[HashKey(hash_key),
RangeKey(range_key),
]
)
table_struct = new_logs.describe()
if 'DISABLE_CREATION' in os.environ:
print("Creation of new table is disabled. Skipping...")
else:
print("Table %s already exists" % dst_table)
sys.exit(0)
except JSONResponseError:
schema = [HashKey(hash_key)]
if range_key != '':
schema.append(RangeKey(range_key))
new_logs = Table.create(dst_table,
connection=ddbc,
schema=schema,
)
print("*** Waiting for the new table %s to become active" % dst_table)
sleep(5)
while ddbc.describe_table(dst_table)['Table']['TableStatus'] != 'ACTIVE':
sleep(3)
if 'DISABLE_DATACOPY' in os.environ:
print("Copying of data from source table is disabled. Exiting...")
sys.exit(0)
# 3. Add the items
for item in logs.scan():
new_item = {}
new_item[hash_key] = item[hash_key]
if range_key != '':
new_item[range_key] = item[range_key]
for f in item.keys():
if f in [hash_key, range_key]:
continue
new_item[f] = item[f]
try:
new_logs.use_boolean()
new_logs.put_item(new_item, overwrite=True)
except ValidationException:
print(dst_table, new_item)
except JSONResponseError:
print(ddbc.describe_table(dst_table)['Table']['TableStatus'])
print("We are done. Exiting...")