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 pip 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: data = json.load (json_file) except Exception as E: print ' Error:no such file like ' + 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): conf = Self.load_json (path) self.client = 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
def list_all_table (self): page=1lastevaluationtablename = "" while true:if page = = 1: response = Self.client.list_tables () Else: response = self.client.list_tables ( exclusivestarttablename= Lastevaluationtablename ) tablenames = response[' tablenames ']for table in Tablenames:print Tableif Response.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 response["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): response = self.get_table_desc_only (table) stastic = {} 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
def create_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) Print response
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 ' Error:put item fail. msg: ' + 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
def delete_table (self,table): try: self.client.delete_table ( tablename=table ) except Exception as E:print ' error:delete table ' + table + ' fail. msg: ' + str (e) Else:print ' delete table ' + table + ' succ '
To be continued .....