def query_gsi()

in design-patterns/query_employees.py [0:0]


def query_gsi(tableName,attribute,value,):
    dynamodb = boto3.resource(**boto_args)
    table = dynamodb.Table(tableName)

    if attribute == 'name':
        ke = Key('GSI_1_PK').eq('root') & Key('GSI_1_SK').eq(value)
    else:
        ke = Key('GSI_1_PK').eq(attribute + "#" + value)

    response = table.query(
        IndexName='GSI_1',
        KeyConditionExpression=ke
        )

    print('List of employees with %s in the attribute %s:' % (value,attribute))
    for i in response['Items']:
        print('\tEmployee name: %s - hire date: %s' % (i['name'],i['hire_date']))

    return response['Count']