AWS DynamoDB是一個NOSQL數(shù)據(jù)庫。
可以通過IAM直接控制權(quán)限,和AWS其他服務連用非常方便。
DynamoDB的幾個概念
Partition Key:分區(qū)鍵。如果沒有Sort key,那么Partition Key必須唯一,如有Sort key,Partition Key可以重復。
Sort key: 排序鍵。
Composite Key:Partition Key和Sort key的合稱,是一個邏輯概念。
GSI: 獨立于Partition Key和Sort key之外的第二套索引機制??梢詣?chuàng)建多個GSI。
用Boto3查詢DyanmoDB
使用GSI來查詢數(shù)據(jù),需要指定indexname:
這里假設分區(qū)鍵叫key1,排序鍵叫sortkey,GSI叫g(shù)si-index
import boto3
from boto3.dynamodb.conditions import Attr, Key
def 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是全表掃描以后再過濾。、
參考資料:文章來源:http://www.zghlxwxcb.cn/news/detail-694260.html
PowerPoint Presentation (sides-share.s3.cn-north-1.amazonaws.com.cn)文章來源地址http://www.zghlxwxcb.cn/news/detail-694260.html
到了這里,關(guān)于AWS DynamoDB淺析的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!