Back
Close

Computing with Data

elgeish
578.2K views

Amazon DynamoDB - Part VI

NoSQL databases are a great fit for unstructured data, like in the following example:

from pprint import pprint

import boto3
from boto3.dynamodb.conditions import Key

dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('reminder')
with table.batch_writer() as batch:
  batch.put_item(
    Item={
      'timestamp': 1511652070,
      'userID': '[email protected]',
      'ttl': 1511738470,
      'checklist': ['milk', 'eggs', 'bread']
    }
  )
  batch.put_item(
    Item={
      'timestamp': 1511652070,
      'userID': '[email protected]',
      'ttl': 1511738470,
      'actions': [{
        'callback': 'example.com/callback',
        'payload': {'text': 'flash office lights'},
        'method': 'POST'
      }]
    }
  )

response = table.query(
  KeyConditionExpression=Key('timestamp').eq(1511652070)
)
pprint(response['Items'])
## [{u'checklist': [u'milk', u'eggs', u'bread'],
##   u'timestamp': Decimal('1511652070'),
##   u'ttl': Decimal('1511738470'),
##   u'userID': u'[email protected]'},
## {u'actions': [{u'callback': u'example.com/callback',
##     u'method': u'POST',
##     u'payload': {u'text': u'flash office lights'}}],
##   u'timestamp': Decimal('1511652070'),
##   u'ttl': Decimal('1511738470'),
##   u'userID': u'[email protected]'}]
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io