Getting started with Dynamo from Python boto

at first

I wanted to access Dynamo DB using Boto (Python SDK of AWS), but I couldn't find the Sample Code that was very helpful in the elementary part, so I put the created Code behind the leaflet. Put.

Code to access Dynamo and do get_item, query_2, scan

It assumes the following
#!/usr/bin/env python
# coding: utf-8

import boto.dynamodb2
from boto.dynamodb2.table import Table

#AWS Keys for Dynamo
aws_access_key_id = 'AKIASJOE73SDGDTDA'
aws_secret_access_key = 'BuAHZv01Us0pZTbe87987JOIeuoeaM3MO'
aws_region = 'ap-northeast-1'

def main():
  conn = boto.dynamodb2.connect_to_region(aws_region, aws_access_key_id=aws_access_key_id,
                       aws_secret_access_key=aws_secret_access_key)

  requests = Table('requests', connection=conn)

  # show # of items
  print requests.count()


  # 'get_key_fields' example to show hash/range-key info for this table
  print requests.get_key_fields()
  

  # 'get_item' example to show all attribute of a item which has particular userID/created_date
  item = requests.get_item(userID='USER-ID-0123-EDSMQNAK', created_date=143918255309)
  for field, val in item.items():
    print "%s: %s" % (field, val)


  # 'query_2' example to query items with particular userID, and show all attributes for each item
  # need to set 'userID__eq', not 'userID' 
  query_item = requests.query_2(userID__eq='USER-ID-0123-EDSMQNAK')
  for item in query_item:
    for field, val in item.items():
      print '%s: %s' % (field, val)
    print '--------------------'


  # 'query_2' example to query items of userID (_eq) AND range key (__gt) 
  query_item = requests.query_2(userID__eq='USER-ID-0123-EDSMQNAK', created_date__gt=143918200000)


  # 'scan' example to scan with key "last_access_day" >= "20150810" 
  query_item = requests.scan(last_access_day__gte=20150810)

if __name__ == '__main__':
  main()

Supplement

Differences between get_item, query_2, scan

Recommended Posts

Getting started with Dynamo from Python boto
1.1 Getting Started with Python
Getting Started with Python
Getting Started with Python
Getting Started with Python Functions
Getting Started with Python Django (4)
Getting Started with Python Django (3)
Getting Started with Python Django (6)
Python3 | Getting Started with numpy
Getting Started with Python Django (5)
Getting Started with Python responder v2
Getting Started with Python Web Applications
Getting Started with Python for PHPer-Classes
Getting Started with Python Basics of Python
Getting Started with Python Genetic Algorithms
Getting started with Python 3.8 on Windows
Getting Started with Python for PHPer-Functions
Getting Started with python3 # 1 Learn Basic Knowledge
Getting Started with Python Web Scraping Practice
Getting Started with Python Web Scraping Practice
Django 1.11 started with Python3.6
Getting Started with Golang 2
Getting started with apache2
Getting Started with Django 1
Getting Started with Optimization
Getting Started with Golang 3
Getting Started with Numpy
Getting started with Spark
Getting Started with Pydantic
Getting Started with Golang 4
Getting Started with Jython
Getting Started with Django 2
Getting started with Python with 100 knocks on language processing
Getting started with AWS IoT easily in Python
Materials to read when getting started with Python
Settings for getting started with MongoDB in python
Getting Started with python3 # 2 Learn about types and variables
Translate Getting Started With TensorFlow
Getting Started with Tkinter 2: Buttons
Getting Started with Go Assembly
Getting Started with Google App Engine for Python & PHP
Get started with Python! ~ ② Grammar ~
Getting Started with Django with PyCharm
With skype, notify with skype from python!
Getting Started with python3 # 3 Try Advanced Computations Using Import Statements
Getting Started with Drawing with matplotlib: Creating Diagrams from Data Files
Getting Started with Mathematics Starting with Python Programming Challenges Personal Notes-Problem 1-1
Get started with Python! ~ ① Environment construction ~
Call C from Python with DragonFFI
Link to get started with python
Using Rstan from Python with PypeR
Install Python from source with Ansible
Create folders from '01' to '12' with python
Getting Started with Git (1) History Storage
Getting started with Sphinx. Generate docstring with Sphinx
Getting Started with Sparse Matrix with scipy.sparse
Run Aprili from Python with Orange
Getting Started with Cisco Spark REST-API
Getting started with USD on Windows
Call python from nim with Nimpy
Read fbx from python with cinema4d