I am trying to build a lambda function with Python, and save the dict outputs to dynamodb database. One of the dict outputs is floating number numpy array. Similar questions probably have been asked, for example, one here using pickle.dumps or numpy.save (How can I serialize a numpy array while preserving matrix dimensions?).
import boto3
import numpy as np
import pickle
# Dynamodb table example with primary keys first_name and last_name
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('users')
# Dict output example
output = {
'first_name': 'David',
'last_name': 'Adams',
'rate', np.random.rand(100000)
}
# Put dict output directly as item to dynamodb
# it will fail due to the data type dynamodb supports
table.put_item(
Item = output
)
# OR convert array to string, but the numeric array will be truncated
table.put_item(
Item = {
'first_name': output['first_name'],
'last_name': output['last_name'],
'rate': str(output['rate'])
}
)
# OR using pickle.dumps to convert numeric array to bytes.
# However, it will fail when the array length is too big, since dynamodb item has a limit of 400kb
table.put_item(
Item = {
'first_name': output['first_name'],
'last_name': output['last_name'],
'rate': pickle.dumps(output['rate'])
}
)