大家好,希望你们没事,我正在学习和试验肉桂药,我正在寻求帮助。
创建LSI和GSI的目标
第一阶段:创建索引并使用假数据
import os
import boto3
import json
from faker import Faker
import random
import pynamodb.attributes as at
import datetime
from datetime import datetime
from pynamodb.models import Model
from pynamodb.attributes import *
AWS_ACCESS_KEY = ""
AWS_SECRET_KEY = ""
AWS_REGION_NAME = "us-east-1"
faker = Faker()
class UserModel(Model):
class Meta:
table_name = 'table_learn'
aws_access_key_id = AWS_ACCESS_KEY
aws_secret_access_key = AWS_SECRET_KEY
email = UnicodeAttribute(null=True)
job = UnicodeAttribute(null=True)
first_name = UnicodeAttribute(range_key=True)
last_name = UnicodeAttribute(hash_key=True)
company = ListAttribute()
UserModel.create_table(billing_mode='PAY_PER_REQUEST')
average = []
for i in range(1, 20):
starttime = datetime.now()
UserModel(email=faker.email(),
first_name=faker.first_name(),
last_name=faker.last_name(),
job=faker.job(),
company = [faker.company() for i in range(1,6)]
).save()
endtime = datetime.now()
delta = endtime-starttime
elapsed_time = int((delta.seconds * 1000) + (delta.microseconds / 1000))
average.append(elapsed_time)
print("Exection Time: {} MS ".format(elapsed_time))
averagetime = sum(average)/ len(average)
print("\nAverage Time in MS: {} ".format(averagetime))到目前为止一切都好
第二阶段设置GSI和LSI
from pynamodb.indexes import GlobalSecondaryIndex, AllProjection
from pynamodb.attributes import NumberAttribute
class ViewIndex(GlobalSecondaryIndex):
"""
This class represents a global secondary index
"""
class Meta:
aws_access_key_id = AWS_ACCESS_KEY
aws_secret_access_key = AWS_SECRET_KEY
projection = AllProjection()
email = UnicodeAttribute(hash_key=True)
class UserModel(Model):
class Meta:
table_name = 'table_learn'
aws_access_key_id = AWS_ACCESS_KEY
aws_secret_access_key = AWS_SECRET_KEY
email = UnicodeAttribute(hash_key=True)
job = UnicodeAttribute(range_key=True)
first_name = UnicodeAttribute()
last_name = UnicodeAttribute()
company = ListAttribute()
view_index = ViewIndex()UserModel.create_table()
错误
on DynamoDB I don't see GSI and LSI what am I doing wrong please guide if possible 正如您所看到的,在创建表和填充数据方面,我很好,我正在与GSI和LSI分区键进行斗争,LSI分区键是姓氏,排序键是first_name,为了更好地学习和理解概念,我试图使用pynamodb设置不同的分区键和排序键,但无法做到。
发布于 2022-09-26 15:57:31
我不知道你是否修复了这个问题,但是对于那些可能下一个看到这个帖子的人来说,他们发现自己也处在同样的情况下。我认为这个示例缺少索引的名称和主表中对名称的引用。索引还可能缺少with range_key=True的一个属性。
from pynamodb.indexes import GlobalSecondaryIndex, AllProjection
from pynamodb.attributes import NumberAttribute
class ViewIndex(GlobalSecondaryIndex):
"""
This class represents a global secondary index
"""
class Meta:
index_name = "my_index_name"
aws_access_key_id = AWS_ACCESS_KEY
aws_secret_access_key = AWS_SECRET_KEY
projection = AllProjection()
email = UnicodeAttribute(hash_key=True)
class UserModel(Model):
class Meta:
table_name = 'table_learn'
index = "my_index_name"
aws_access_key_id = AWS_ACCESS_KEY
aws_secret_access_key = AWS_SECRET_KEY
email = UnicodeAttribute(hash_key=True)
job = UnicodeAttribute(range_key=True)
first_name = UnicodeAttribute()
last_name = UnicodeAttribute()
company = ListAttribute()
view_index = ViewIndex()https://stackoverflow.com/questions/72050257
复制相似问题