首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PynamoDB创建GSI和LSI Python错误

PynamoDB创建GSI和LSI Python错误
EN

Stack Overflow用户
提问于 2022-04-28 21:10:42
回答 1查看 123关注 0票数 0

大家好,希望你们没事,我正在学习和试验肉桂药,我正在寻求帮助。

创建LSI和GSI的目标

第一阶段:创建索引并使用假数据

代码语言:javascript
复制
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

代码语言:javascript
复制
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()

错误

代码语言:javascript
复制
on DynamoDB I don't see GSI and LSI what am I doing wrong please guide if possible 

正如您所看到的,在创建表和填充数据方面,我很好,我正在与GSI和LSI分区键进行斗争,LSI分区键是姓氏,排序键是first_name,为了更好地学习和理解概念,我试图使用pynamodb设置不同的分区键和排序键,但无法做到。

EN

回答 1

Stack Overflow用户

发布于 2022-09-26 15:57:31

我不知道你是否修复了这个问题,但是对于那些可能下一个看到这个帖子的人来说,他们发现自己也处在同样的情况下。我认为这个示例缺少索引的名称和主表中对名称的引用。索引还可能缺少with range_key=True的一个属性。

代码语言:javascript
复制
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()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72050257

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档