boto3 dynamodb 入門使用

2021-07-27 12:46:04 字數 4314 閱讀 5529

介紹

amazon dynamodb 是一項快速靈活的 nosql 資料庫服務,適合所有需要一致性且延遲低於 10 毫秒的任意規模的應用程式。它是完全託管的雲資料庫,支援文件和鍵值儲存模型。靈活的資料模型和可靠的效能使其成為移動、web、遊戲、廣告技術、物聯網和眾多其他應用的不二之選。

定價 官方**的**介紹:

但是我實際開發的時候乙個月只有4000多寫收了27rmb,我一共建立了兩個表,沒有搞清怎麼收費,還是每個表有乙個最低收費。我配置每張表的讀取,寫入的速度,可以根據每張表的實際使用讀寫和寫入速度,這樣可以最大的降低費用,避免不必要的浪費。

上手實驗營:

**例子

建立新錶

import boto3

# get the service resource.

dynamodb = boto3.resource('dynamodb')

# create the dynamodb table.

table = dynamodb.create_table(

tablename='users',

keyschema=[,],

attributedefinitions=[,,

],provisionedthroughput=

)# wait until the table exists.

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

# print out some data about the table.

print(table.item_count)

新增新item

table.put_item(

item=

)

跟新item

table.update_item(

key=,

updateexpression='set age = :val1',

expressionattributevalues=

)

我寫的乙個專案,物件dynamodb:

# coding:utf-8

from boto3 import session

from error import dynamodbconnectionerror, dynamodberror

from botocore.exceptions import clienterror

from boto3.dynamodb.conditions import attr

from helper import decimalencoder

import logging

import json

logger = logging.getlogger(__name__)

class

myservicedynamodb

(object):

""" """

def__init__

(self, **kwargs):

self.region = kwargs['region']

self.access_key = kwargs['access_key']

self.secret_key = kwargs['secret_key']

self.session = self.__session()

def__session

(self):

try:

session = session(aws_access_key_id=self.access_key,

aws_secret_access_key=self.secret_key,region_name=self.region)

except:

raise dynamodbconnectionerror("failed to connect session in region".format(self.region))

return session

defget_table

(self, table_name):

dynamodb = self.session.resource('dynamodb')

ifnot dynamodb:

raise dynamodbconnectionerror("failed to get resource for dynamodb!")

try:

table = dynamodb.table(table_name)

except exception as e:

logger.error("failed to get table , error".format(table_name, e))

return table

defput_item

(self, table, item_dict=none):

try:

response = table.put_item(item=item_dict)

except exception as e:

logger.error("failed to put item in to :error".format(table,e))

return response

defstatus_scan_items

(self, table, event_status=none):

try:

response = table.scan(filterexpression=attr('event_status').eq(event_status))

except exception as e:

logger.error(' failed query items , error'.format(json.dumps(table, indent=4,cls=decimalencoder),e))

return response

defget_item

(self, table, id):

try:

response = table.get_item(key=)

except clienterror as e:

if e.response['error']['code'] == "conditionalcheckfailedexception":

logger.error(e.response['error']['message'])

else:

raise dynamodberror('failed to get item from dynamodb not by conditional')

else:

logger.info(json.dumps(response, indent=4, cls=decimalencoder))

return response

defupdate_status_by_event_id

(self, table, event_id, change_status=none):

try:

response = table.update_item(

key = ,updateexpression = "set event_status = :val1",

expressionattributevalues=,returnvalues="updated_new")

except clienterror as e:

if e.response['error']['code'] == "conditionalcheckfailedexception":

logger.error(e.response['error']['message'])

else:

raise dynamodberror('failed update the dynamodb by event_id,not failed conditional')

else:

logger.info("updateitem succeeded:".format(json.dumps(response, indent=4, cls=decimalencoder)))

return response

注意:

dynamodb的保留字段,在建立表的字段的時候不要使用一下字段不然的話,在進行表操作會報錯:attribute name is a reserved keyword; reserved keyword: status

dynamodb 保留字段:

參考文件:

官方文件:

python 操作boto3操作s3

定時任務實時生成pdf,將檔案tornado用非同步io上傳到s3,有幾個坑記錄下 import re import boto3 import logging from multiprocessing.dummy import pool as threadpool aws region name c...

C入門3 控制語句入門

練習1 1 比較兩個數的大小。include intmain else else return0 2 求乙個數的相反數和絕對值。include intmain else return0 3 閏年判斷。include intmain else return0 4 三角形構成判斷。include int...

directshow 學習入門 3

如何列舉系統的裝置和過濾器 1 系統裝置列舉器 系統裝置列舉器提供了乙個很好的方法根據種類來列舉系統中註冊的過慮器。也許枚一種不同的硬體都會有自己的過慮器,或許所有的硬體裝置共用同乙個filter。這個對於採用wdm驅動程式的硬體很有用。按照下面的步驟使用裝置列舉器 1 建立列舉器元件,clsid為...