{"id":1138,"date":"2017-10-14T10:23:20","date_gmt":"2017-10-14T15:23:20","guid":{"rendered":"http:\/\/blog.ls-al.com\/?p=1138"},"modified":"2017-10-14T10:23:20","modified_gmt":"2017-10-14T15:23:20","slug":"dynamodbtest","status":"publish","type":"post","link":"https:\/\/blog.ls-al.com\/dynamodbtest\/","title":{"rendered":"DynamoDB Test"},"content":{"rendered":"

Boto3 and AWS DynamoDB usage...<\/p>\n

http:\/\/boto3.readthedocs.io\/en\/latest\/reference\/services\/dynamodb.html<\/p>\n

\r\n$ cat dynamodbTest.py \r\nimport boto3\r\n\r\n#dynamodb = boto3.resource('dynamodb')\r\n# Hard coding strings as credentials, not recommended. Use configs or env variables AWS_ACCESS_KEY, AWS_SECRET_KEY\r\ndynamodb = boto3.resource(\r\n    'dynamodb',\r\n    aws_access_key_id='KEY_ID_REMOVED',\r\n    aws_secret_access_key='ACCESS_KEY_REMOVED',\r\n    region_name = 'us-east-1'\r\n)\r\n\r\ndef create_table(tableName):\r\n  table = dynamodb.create_table(\r\n    TableName=tableName,\r\n    KeySchema=[\r\n        {\r\n            'AttributeName': 'username', \r\n            'KeyType': 'HASH'\r\n        },\r\n        {\r\n            'AttributeName': 'last_name', \r\n            'KeyType': 'RANGE'\r\n        }\r\n    ], \r\n    AttributeDefinitions=[\r\n        {\r\n            'AttributeName': 'username', \r\n            'AttributeType': 'S'\r\n        }, \r\n        {\r\n            'AttributeName': 'last_name', \r\n            'AttributeType': 'S'\r\n        }, \r\n    ], \r\n    ProvisionedThroughput={\r\n        'ReadCapacityUnits': 1, \r\n        'WriteCapacityUnits': 1\r\n    }\r\n  )\r\n\r\n  table.meta.client.get_waiter('table_exists').wait(TableName=tableName)\r\n  print 'Table item count: {}'.format(table.item_count)\r\n\r\ndef delete_table(tableName):\r\n  table = dynamodb.Table(tableName)\r\n  table.delete()\r\n\r\ndef put_item(tableName):\r\n  table = dynamodb.Table(tableName)\r\n\r\n  response = table.put_item(\r\n   Item={\r\n        'username': 'jdoe',\r\n        'first_name': 'jane',\r\n        'last_name': 'doe',\r\n        'age': 20,\r\n        'account_type': 'librarian',\r\n    }\r\n  )\r\n\r\n  print response\r\n\r\ndef get_item(tableName):\r\n  table = dynamodb.Table(tableName)\r\n\r\n  response = table.get_item(\r\n   Key={\r\n        'username': 'jdoe',\r\n        'last_name': 'doe'\r\n    }\r\n  )\r\n\r\n  item = response['Item']\r\n  name = item['first_name']\r\n\r\n  print(item)\r\n  print("Hello, {}" .format(name))\r\n\r\ndef update_item(tableName):\r\n  table = dynamodb.Table(tableName)\r\n\r\n  table.update_item(\r\n    Key={\r\n        'username': 'jdoe',\r\n        'last_name': 'doe'\r\n    },\r\n    UpdateExpression='SET age = :val1',\r\n    ExpressionAttributeValues={\r\n        ':val1': 23\r\n    }\r\n  )\r\n\r\ndef delete_item(tableName):\r\n  table = dynamodb.Table(tableName)\r\n\r\n  table.delete_item(\r\n    Key={\r\n        'username': 'jdoe',\r\n        'last_name': 'doe'\r\n    }\r\n  )\r\n\r\ndef batch_write(tableName):\r\n  table = dynamodb.Table(tableName)\r\n\r\n  with table.batch_writer() as batch:\r\n    batch.put_item(\r\n        Item={\r\n            'account_type': 'end_user',\r\n            'username': 'bbob',\r\n            'first_name': 'billy',\r\n            'last_name': 'bob',\r\n            'age': 20,\r\n            'address': {\r\n                'road': '1 fake street',\r\n                'city': 'Houston',\r\n                'state': 'TX',\r\n                'country': 'USA'\r\n            }\r\n        }\r\n    )\r\n    batch.put_item(\r\n        Item={\r\n            'account_type': 'librarian',\r\n            'username': 'user1',\r\n            'first_name': 'user1 first name',\r\n            'last_name': 'user1 last name',\r\n            'age': 20,\r\n            'address': {\r\n                'road': '10 fake street',\r\n                'city': 'Dallas',\r\n                'state': 'TX',\r\n                'country': 'USA'\r\n            }\r\n        }\r\n    )\r\n    batch.put_item(\r\n        Item={\r\n            'account_type': 'end_user',\r\n            'username': 'user2',\r\n            'first_name': 'user2 first name',\r\n            'last_name': 'user2 last name',\r\n            'age': 23,\r\n            'address': {\r\n                'road': '12 fake street',\r\n                'city': 'Austin',\r\n                'province': 'TX',\r\n                'state': 'USA'\r\n            }\r\n        }\r\n    )\r\n\r\ndef create_multiple_items(tableName,itemCount):\r\n\r\n  table = dynamodb.Table(tableName)\r\n\r\n  with table.batch_writer() as batch:\r\n    for i in range(itemCount):\r\n        batch.put_item(\r\n            Item={\r\n                'account_type': 'anonymous',\r\n                'username': 'user-' + str(i),\r\n                'first_name': 'unknown',\r\n                'last_name': 'unknown'\r\n            }\r\n        )\r\n\r\n\r\ndef query(tableName):\r\n  from boto3.dynamodb.conditions import Key, Attr\r\n  table = dynamodb.Table(tableName)\r\n\r\n  response = table.query(\r\n    KeyConditionExpression=Key('username').eq('user2')\r\n  )\r\n\r\n  items = response['Items']\r\n  print(items)\r\n\r\ndef scan(tableName):\r\n  from boto3.dynamodb.conditions import Key, Attr\r\n\r\n  table = dynamodb.Table(tableName)\r\n\r\n  response = table.scan(\r\n    FilterExpression=Attr('age').gt(23)\r\n  )\r\n\r\n  items = response['Items']\r\n  print(items)\r\n\r\n  len(items)\r\n  for x in range(len(items)): \r\n    items[x]['username']\r\n\r\ndef query_filter(tableName):\r\n  from boto3.dynamodb.conditions import Key, Attr\r\n\r\n  table = dynamodb.Table(tableName)\r\n\r\n  response = table.scan(\r\n    FilterExpression=Attr('first_name').begins_with('r') & Attr('account_type').eq('librarian')\r\n  )\r\n\r\n  items = response['Items']\r\n  print(items)\r\n\r\n\r\n# Comment\/uncomment below to play with the different functions\r\n#create_table('staff')\r\n\r\n#put_item('staff')\r\n#get_item('staff')\r\n#update_item('staff')\r\n#delete_item('staff')\r\n\r\n#batch_write('staff')\r\n\r\n#create_multiple_items('staff', 100)\r\n\r\n#query('staff')\r\n#scan('staff')\r\n#query_filter('staff')\r\n\r\n#delete_table('staff')\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

Boto3 and AWS DynamoDB usage… http:\/\/boto3.readthedocs.io\/en\/latest\/reference\/services\/dynamodb.html $ cat dynamodbTest.py import boto3 #dynamodb = boto3.resource(‘dynamodb’) # Hard coding strings as credentials,<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[91,13],"tags":[],"class_list":["post-1138","post","type-post","status-publish","format-standard","hentry","category-dynamodb","category-python"],"_links":{"self":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts\/1138","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/comments?post=1138"}],"version-history":[{"count":0,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts\/1138\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/media?parent=1138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/categories?post=1138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/tags?post=1138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}