雜志網(wǎng)站建設(shè)方案優(yōu)化大師免安裝版
AWS DynamoDB是一個(gè)NOSQL數(shù)據(jù)庫。
可以通過IAM直接控制權(quán)限,和AWS其他服務(wù)連用非常方便。
DynamoDB的幾個(gè)概念
Partition Key:分區(qū)鍵。如果沒有Sort key,那么Partition Key必須唯一,如有Sort key,Partition Key可以重復(fù)。
Sort key: 排序鍵。
Composite Key:Partition Key和Sort key的合稱,是一個(gè)邏輯概念。
GSI: 獨(dú)立于Partition Key和Sort key之外的第二套索引機(jī)制??梢詣?chuàng)建多個(gè)GSI。
用Boto3查詢DyanmoDB
使用GSI來查詢數(shù)據(jù),需要指定indexname:
這里假設(shè)分區(qū)鍵叫key1,排序鍵叫sortkey,GSI叫g(shù)si-index
import boto3
from boto3.dynamodb.conditions import Attr, Keydef query_app(self, key1: str, sortkey: str):response = self.table.query(IndexName='gsi-index',KeyConditionExpression = Key("key1").eq(key1) & Key("sortkey").begins_with(sortkey),ScanIndexForward = False)code = response.get('ResponseMetadata').get('HTTPStatusCode')if code == 200:logging.info(f"query item successfully!")return response.get("Items")else:logging.warning(f"query item fail!, response is {code}")
query和scan的區(qū)別:
- query是利用key來查詢。(推薦。)
- scan是全表掃描以后再過濾。、
參考資料:
PowerPoint Presentation (sides-share.s3.cn-north-1.amazonaws.com.cn)