pynamodb / PynamoDB

A pythonic interface to Amazon's DynamoDB
http://pynamodb.readthedocs.io
MIT License
2.46k stars 427 forks source link

PynamoDB unable to Create GSI and LSI #1040

Open soumilshah1995 opened 2 years ago

soumilshah1995 commented 2 years ago

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

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

ikonst commented 2 years ago

Make sure you either (a) delete the old table first, or (b) add the GSI/LSI through AWS console or some other means.

soumilshah1995 commented 2 years ago

Well pynamodb doest not support creation of tables with LSI or GSI ?

soumilshah1995 commented 2 years ago

Make sure you either (a) delete the old table first, or (b) add the GSI/LSI through AWS console or some other means.

Does PynamoDB support the creation of LSI after a table has been created through the client library? Same question for GSI