Created
February 1, 2019 12:11
-
-
Save utkarshmalik211/1908d64b0f4e0c924a17c64363215b74 to your computer and use it in GitHub Desktop.
Python script to extract all dynamoDB data to a .json file using boto3 .
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
import boto3 | |
from boto3.dynamodb.conditions import Key, Key | |
import decimal | |
import json | |
AccessKey= "xxxxxxxxxxxxxxxxxxxxx" #aws access keys | |
SecretKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" | |
session = boto3.Session( | |
aws_access_key_id=AccessKey, | |
aws_secret_access_key=SecretKey, | |
region_name="us-west-2" | |
) | |
dynamodb = session.resource('dynamodb') | |
valuesToSearch = ['sample data', 'sample data'] | |
# paginator = dynamodb.get_paginator('scan') | |
table = dynamodb.Table('tableName') | |
def getData(partitionKeyValue): | |
data =[] | |
response = table.query( | |
KeyConditionExpression=Key("partitionKey").eq(partitionKeyValue) & Key('timestamp').between("2018-01-01 00:00:00","2019-01-31 00:00:00") #timestamp is sort key | |
) | |
data.extend(response['Items']) | |
while 'LastEvaluatedKey' in response: | |
response = table.query( | |
KeyConditionExpression=Key("partitionKey").eq(partitionKeyValue) & Key('timestamp').between("2018-01-01 00:00:00","2019-01-31 00:00:00") | |
ExclusiveStartKey=response['LastEvaluatedKey'] | |
) | |
data.extend(response['Items']) | |
with open(f"folderName/{deviceId}.json","w+") as file1: | |
file1.write(json.dumps(data)) | |
data = [] | |
for i in valuesToSearch: | |
getData(i) | |
print("Done for ",i) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment