Last active
July 6, 2016 04:17
-
-
Save pilgrim2go/89df3a71ba52024519547f45a436d8c7 to your computer and use it in GitHub Desktop.
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 | |
# From https://gist.github.com/iomz/9774415 | |
# Adapt to use boto3 | |
import boto3 | |
import botocore | |
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') | |
dynamodb = boto3.resource('dynamodb',region_name=region) | |
# 1. Read and copy the target table to be copied | |
table_struct = None | |
try: | |
logs = dynamodb.Table(src_table) | |
table_struct = logs.key_schema | |
except botocore.exceptions.ClientError as e: | |
print "Table %s does not exist" % src_table | |
sys.exit(1) | |
print '*** Reading key schema from %s table' % src_table | |
hash_key = '' | |
range_key = '' | |
for schema in logs.key_schema: | |
attr_name = schema['AttributeName'] | |
key_type = schema['KeyType'] | |
if key_type == 'HASH': | |
hash_key = attr_name | |
elif key_type == 'RANGE': | |
range_key = attr_name | |
hash_key_type = '' | |
range_key_type = '' | |
for attr in logs.attribute_definitions: | |
attr_name = attr['AttributeName'] | |
attr_type = attr['AttributeType'] | |
if attr_name == hash_key: | |
hash_key_type = attr_type | |
elif attr_name == range_key: | |
range_key_type = attr_type | |
# 2. Create the new table | |
new_logs = None | |
try: | |
new_logs = dynamodb.Table(dst_table) | |
new_logs.table_status | |
print 'Table %s already exists' % dst_table | |
# sys.exit(0) | |
except botocore.exceptions.ClientError: | |
new_logs = dynamodb.create_table( | |
TableName=dst_table, | |
KeySchema=[ | |
{ | |
'AttributeName': hash_key, | |
'KeyType': 'HASH' #Partition key | |
}, | |
{ | |
'AttributeName': range_key, | |
'KeyType': 'RANGE' #Sort key | |
} | |
], | |
AttributeDefinitions=[ | |
{ | |
'AttributeName': hash_key, | |
'AttributeType': hash_key_type | |
}, | |
{ | |
'AttributeName': range_key, | |
'AttributeType': range_key_type | |
}, | |
] | |
) | |
print '*** Waiting for the new table %s to become active' % dst_table | |
sleep(5) | |
while new_logs.table_status != '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 | |
response = logs.scan() | |
for i in response['Items']: | |
try: | |
response = new_logs.put_item( | |
Item=i | |
) | |
except Exception as e: | |
print(e) | |
print 'We are done. Exiting...' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment