编码大型numpy数组并将其保存到dynamodb

时间:2018-08-09 19:22:16

标签: json python-3.x amazon-web-services numpy amazon-dynamodb

我正在尝试使用Python构建lambda函数,并将dict输出保存到dynamodb数据库。 dict输出之一是浮点数numpy数组。可能有人提出过类似的问题,例如,在这里使用pickle.dumps或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'])
    }
)

0 个答案:

没有答案