Basic interaction of Python--boto3 with DYNAMODB, backup and recovery of tables

Source: Internet
Author: User
Tags dynamodb

Reference: http://boto3.readthedocs.io/en/latest/reference/services/dynamodb.html

Recently, due to work needs, the study of Boto3 in the Dynamodb part, a little experience, in this summary.

The first is the installation of Boto3, which runs on a machine with Python and Pip

sudo Install Boto3  

In the official website document, BOTO3 provides the following interfaces to interact with DYNAMODB:

Batch_get_item () Batch_write_item () can_paginate () create_table () Delete_item () delete_table () describe_limits () Describe_table () describe_time_to_live () Generate_presigned_url () get_item () Get_paginator () Get_waiter () list_ Tables () List_tags_of_resource () Put_item () query () scan () Tag_resource () Untag_resource () Update_item () update_table ( ) update_time_to_live ()

To be blunt, it is to increase, delete, check and change the tables and records. This article mainly describes the several interfaces that I have recently used.

To use Boto3 in Python, you have to import Boto3 first. Of course, this is nonsense. For ease of use, I first wrote a JSON-formatted configuration file, as follows:

{    "region_name":"xxx",    "  aws_access_key_id":"xxx",    "  aws_secret_access_key":"xxx"}

It then encapsulates a class that is dedicated to manipulating Dynamodb, and there's nothing

class dynamodb_operation ():

It requires a way to read the JSON file:

    def Load_json (Self,path):         Try : With            open (path) as Json_file:                = json.load (json_file)        except  Exception as E:            print' + path            exit (-1  )        Else:            return data

Since the file read may not be in JSON format, I just want him to make a mistake and then quit. If you don't want it to quit, change it in except.

Then, I want this class to have a private member client, to establish a connection when I instantiate the object, and then, with the following initialization methods:

    def __init__ (Self,path):         = Self.load_json (path)        = boto3.client ('dynamodb', region_name=conf['  region_name'],aws_access_key_id=conf['aws_access_key_id  '], aws_secret_access_key=conf['aws_secret_access_key')

Corresponds to the previous configuration file.

With this foundation, you can encapsulate the method you want to use. The instructions on the official website of each method are not copied over.

1. List all the tables in Dynamodb

    deflist_all_table (self): page=1Lastevaluationtablename=""         whileTrue:ifpage = = 1: Response=self.client.list_tables ()Else: Response=Self.client.list_tables (Exclusivestarttablename=lastevaluationtablename) Tablenames= response['Tablenames']             forTableinchTablenames:PrintTableifResponse.has_key ('Lastevaluatedtablename'): Lastevaluationtablename= response["Lastevaluatedtablename"]            Else:                 Breakpage+ = 1

The List_table () method can fetch up to 100 table names at a time, and at each return, the value of key "Lastevaluatedtablename" is the table name of the last table, which can be used as a parameter for the next request. This loops the call to get all the table names. If there is no table at the back, there will be no lastevaluatedtablename in response. Here I just want to print the table name to the terminal, if you want to save it, it is also possible.

2. Get information about a table describe_table ()

 def   Get_table_desc_only (self,table): try  : Response  = Self.client.describe _table (Tablename=table)  except   Exception as E:  print  "  error:no such table like   " +  table exit ( -1 else  :  return  respon Se[ " table   "]

This simply returns the response["Table", without doing any other processing.

If I want to know the size of a table, you can:

    def get_table_size (self,table):         = self.get_table_desc_only (table)        = {}        stastic['tablesizebytes' ] = response['tablesizebytes']        stastic['  ItemCount'] = response['ItemCount']          return stastic

If you want to know other information and just want to know the information, you can also write a corresponding method.

3. Create a table

    defcreate_table (self,tablename,keyschema,attributedefinitions,provisionedthroughput): Table=self.client.create_table (TableName=TableName, Keyschema=Keyschema, Attributedefinitions=attributedefinitions, Provisionedthroughput=provisionedthroughput)#Wait until the table exists.Self.client.get_waiter ('table_exists'). Wait (tablename=tablename) Response= Self.client.describe_table (tablename=tablename)PrintResponse

This is the creation of a table with no indexes. It takes time, so the Get_waiter () method is used.

4. Inserting data

    def Put_item (self,tablename,item):         Try :            self.client.put_item (                    TableName=TableName,                    item=item                    )         except Exception as E:             Print '  ' + str (e)            exit (-1)        Else:              Return

This method of encapsulation needs to pass in a well-formed JSON, and the key should correspond to the table. Like what:

{'UID':{'N':'999'},'Aid':{'N':'999'},'Sid':{'N':'999'},'Ksid':{'N':'999'}}

5. Delete Table

    defdelete_table (self,table):Try: self.client.delete_table (TableName=table)exceptException as E:Print 'error:delete Table'+ table +'fail. Msg:'+Str (e)Else:            Print 'Delete Table'+ table +'succ'

To be continued .....

Basic interaction of Python--boto3 with DYNAMODB, backup and recovery of tables

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.