import boto3 from boto3.dynamodb.conditions import Key import json DYNAMODB_TABLE_NAME = 'prod_feed_ingestion_status' feed_names = { 'spotify_aggregated_streams': 'spotify_theorchard_aggregated_streams', 'spotify_streams': 'spotify_theorchard_streams', 'spotify_sub_30_sec_streams': 'spotify_theorchard_sub_30_sec_streams', 'spotify_tracks': 'spotify_theorchard_tracks', 'spotify_users': 'spotify_theorchard_users', } def main(): dynamodb = boto3.resource('dynamodb', region_name='us-east-1') dynamodb_table = dynamodb.Table(DYNAMODB_TABLE_NAME) for feed_name, new_feed_name in feed_names.items(): response = dynamodb_table.query( KeyConditionExpression=Key('feed_name').eq(feed_name)) items = response['Items'] # save statuses in file with open('{}_statuses.json'.format(feed_name), 'w') as outfile: json.dump(items, outfile) with dynamodb_table.batch_writer() as batch: for item in items: # insert new item new_item = dict(item) new_item['feed_name'] = new_feed_name batch.put_item(Item=new_item) print('Inserting: {}'.format(new_item)) # delete old item batch.delete_item( Key={ 'feed_name': item['feed_name'], 'date': item['date']}) if __name__ == '__main__': main()