Created
November 28, 2018 12:59
-
-
Save quentinhayot/b018515616e371ce1a065f533c092a29 to your computer and use it in GitHub Desktop.
Copy data from a dynamodb table to another
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python2 | |
# Forked from https://github.com/techgaun/dynamodb-copy-table | |
# USAGE: AWS_PROFILE=your_profile AWS_DEFAULT_REGION=your_region python dynamodb-copy-table.py "source_table" "destination_table" | |
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', 'eu-west-1') | |
DynamoDBConnection.DefaultRegionName = region | |
ddbc = DynamoDBConnection() | |
# Check if the source table exists | |
try: | |
logs = Table(src_table, connection=ddbc) | |
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 | |
new_logs = Table(dst_table, | |
connection=ddbc, | |
schema=[HashKey(hash_key), | |
RangeKey(range_key), | |
] | |
) | |
# Add the items | |
print '*** Copying 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...' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment