PynamoDB Creating GSI and LSI Python Error

561 Views Asked by At

Hello All hope you are fine i am reaching out for some help as I am learning and experimenting with pynamodb

Goal to create LSI and GSI

Phase 1: create index and popular with fake data

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))

All good so far

Phase 2 Setting GSI and 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()

Error

on DynamoDB I don't see GSI and LSI what am I doing wrong please guide if possible 

As you can see up to the point of creating table and populating with data I am fine I am struggling with GSI and LSI partition key is last name and sort key is first_name to learn and understand concepts better I am trying to set different partition key and sort key using pynamodb but unable to do so

1

There are 1 best solutions below

0
On

i dont know if you fixed this, but for whoever may see this post next and find themselves in the same situation. i think this example is missing the name of the index and the reference to the name from the main table. the index may also be missing an attribute of 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()