Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting None values into DynamoDB using Boto

I'm new to Boto, and I'm trying to use it to insert a Python dictionary into Amazon DynamoDB. I must be missing something, because the "dynamizer" (encoder) does not seem to support None values. This is a problem because the source data has tons of nulls in it. I could go through each row and delete out all of the key/value items where the value is None, but somehow I feel like a package as sophisticated as Boto should take care of that for me. I'm simply trying to insert one row like so:

conn = DynamoDBConnection(region=RegionInfo(endpoint="dynamodb.us-west-2.amazonaws.com"))
dest = Table('d_company', connection=conn)
data = {"company_id":99999, "company_name":None}
dest.put_item(data)

...and this gives me the error:

Error
Traceback (most recent call last):
  File "TestDynamoDB.py", line 37, in testPutIntoDynamoDB
    dest.put_item(data)
  File "C:\Python27\lib\site-packages\boto\dynamodb2\table.py", line 452, in put_item
    return item.save(overwrite=overwrite)
  File "C:\Python27\lib\site-packages\boto\dynamodb2\items.py", line 362, in save
    final_data = self.prepare_full()
  File "C:\Python27\lib\site-packages\boto\dynamodb2\items.py", line 265, in prepare_full
    final_data[key] = self._dynamizer.encode(value)
  File "C:\Python27\lib\site-packages\boto\dynamodb\types.py", line 228, in encode
    dynamodb_type = self._get_dynamodb_type(attr)
  File "C:\Python27\lib\site-packages\boto\dynamodb\types.py", line 220, in _get_dynamodb_type
    return get_dynamodb_type(attr)
  File "C:\Python27\lib\site-packages\boto\dynamodb\types.py", line 110, in get_dynamodb_type
    raise TypeError(msg)
TypeError: Unsupported type "<type 'NoneType'>" for value "None"

What am I doing wrong?

like image 850
futilerebel Avatar asked May 22 '13 18:05

futilerebel


People also ask

Does DynamoDB accept null values?

Dynamodb can't accept a key with an explicitly NULL value.

Does DynamoDB accept empty string?

Amazon DynamoDB now supports empty values for non-key String and Binary attributes in DynamoDB tables. Empty value support gives you greater flexibility to use attributes for a broader set of use cases without having to transform such attributes before sending them to DynamoDB.

Can DynamoDB hash key null?

Can the DynamoDB sort key be null? DynamoDB does not support null for sort key.


1 Answers

You are not doing anything wrong. While boto is sophisticated indeed, you have to remember, that it does not have the knowledge of your business logic.

For example, there are at least a few ways someone can think of saving None to DynamoDB database:

  • as "None" string
  • as empty string
  • don't save at all - remember, it is NoSQL database, attributes are not required

The best way to determine it - is your code. If your data could be None, don't add it to the dictionary.

like image 130
kukido Avatar answered Sep 24 '22 06:09

kukido