DynamoDB

https://boto3.amazonaws.com/v1/documentation/api/latest/guide/dynamodb.htmlhtml

 

Creating a  New  Tablepython

In order to create a new table, use the DynamoDB.ServiceResource.create_table() method:api

建立新的表的方法:ide

import  boto3ui

#获取dynamoDB 服务资源this

dynamodb  = boto3.resource('dynamodb')spa

# 建立DynamDB 表htm

使用create_table()方法建立表,对象

而且设置:ci

  表名称:TableName

  主键属性: keySchema

 

table = dynamodb.create_table(

  TableName=‘users’,

  KeySchema=[

    {

      'AttrbuteName':'username',

      'KeyType':'HASH'  #哈希

    },

    {

      'AttrbuteName':'last_name',

      'KeyType':'RANGE' #貌似列表

    }

  ],

  AttributeDefinitions=[

        {
            'AttributeName': 'username', 'AttributeType': 'S' }, { 'AttributeName': 'last_name', 'AttributeType': 'S' },

 

  ],

  ProvisionedThroughput={

    'ReadCapacityUnits':5,

    'WriteCapacityUnits':5

  }

)

# wait until the talble exists.

table.meta.client.get_waiter('table_exists').wait(TableName="users")

 

#print out some data about the table.

#打印表格数据

print(table.item_count)

 

这将建立一个名为users的表,该表分别具备散列和范围主键username和last_name。这个方法将返回一个DynamoDB。表资源来调用已建立表上的其余方法。

This creates a table named users that respectively has the hash and range primary keys username and last_name. This method will return a DynamoDB.Table resource to call additional methods on the created table.

 

Using an Existing Table 使用存在的表

 

It is also possible to create a DynamoDB.Table resource from an existing table:

也能够建立DynamoDB。来自现有表的表资源:

 

import boto3

#get the service resource.

dynamodb = boto3.resource('dynamodb')

# Instantiate a table resource object without actually
实例化表资源对象 # creating a DynamoDB table. Note that the attributes of this table
are lazy-loaded: a request is not made nor are the attribute
values populated until the attributes on the table resource are accessed or its load() method is called.

#建立DynamoDB表。注意,此表的属性是延迟加载的:在访问表资源上的属性或调用其load()方法以前,

不会发出请求,也不会填充属性值。

table = dynamodb.Table('user')

# Print out some data about the table.打印一些关于表格的数据。
# This will cause a request to be made to DynamoDB and its attribute values will be set based on the response.
译文:这将致使向DynamoDB发出请求,并根据响应设置其属性值。
print(table.creation_date_time)

Expected Output (Please note that the actual times will probably not match up):

2015-06-26 12:42:45.149000-07:00

译文:指望输出(请注意实际时间可能不匹配):

 

Creating a New Item

Once you have a DynamoDB.Table resource you can add new items to the table using DynamoDB.Table.put_item():

table.put_item(

  Item={

    'username':'janedoe',

    'first_name':'Jane',

    'age':25,

    'account_type':'standard_user',

  }

)

 

For all of the valid types that can be used for an item, refer to Valid DynamoDB Types.

 对于可用于项的全部有效类型,请参考有效的DynamoDB类型。

 

Valid DynamoDB Types 有效的数据类型:

Python Type

Python Type DynamoDB Type
string String (S)
integer Number (N)
decimal.Decimal Number (N)
boto3.dynamodb.types.Binary Binary (B)
boolean Boolean (BOOL)
None Null (NULL)
string set String Set (SS)
integer set Number Set (NS)
decimal.Decimal set Number Set (NS)
boto3.dynamodb.types.Binary set Binary Set (BS)
list List (L)
dict Map (M)
 
 
 

 

 

 

未完 待续

相关文章
相关标签/搜索