Skip to content

Instantly share code, notes, and snippets.

@olibob
Created June 19, 2017 14:11
Show Gist options
  • Save olibob/6039656c54594282d88841481794eb22 to your computer and use it in GitHub Desktop.
Save olibob/6039656c54594282d88841481794eb22 to your computer and use it in GitHub Desktop.
import boto3
import json
import decimal
from boto3.dynamodb.conditions import Key, Attr
from botocore.exceptions import ClientError
# Helper class to convert a DynamoDB item to JSON.
class DecimalEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, decimal.Decimal):
if o % 1 > 0:
return float(o)
else:
return int(o)
return super(DecimalEncoder, self).default(o)
dynamodb = boto3.resource("dynamodb", endpoint_url="https://dynamodb.eu-central-1.amazonaws.com")
table = dynamodb.Table('NetdinoData')
DeviceId = "Netduino1"
UpdateTime = "06/18/2017 00:00:00"
try:
# response = table.query(
# KeyConditionExpression = Key('year').eq(2016)
# )
response = table.query(
KeyConditionExpression = Key('DeviceId').eq(DeviceId) & Key('UpdateTime').gte(UpdateTime),
FilterExpression = Attr('payload.Humidity_1').gt(12)
)
except ClientError as e:
print(e.response['Error']['Message'])
else:
item = response['Items']
print("GetItem succeeded:")
print(json.dumps(item, indent=4, cls=DecimalEncoder))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment