0

I was trying to query a DynamoDB table using a Lambda function.

My table's partition key is id. I am trying to query it on another key named dipl_idpp. I unstrood that that is not possible.

I found here a solution: I need to create a Global Secondary Index poitning on the column that I want to query on (in my case dipl_idpp).

I did that on Dynamo. But when I execute my function I still have the same problem:

An error occurred (ValidationException) when calling the Query operation: Query condition missed key schema element: id', 'occurred at index 0')"

This is the code I use:

def query_dipl_dynamo(key_table,valeur_query,name_table):
    dynamoDBResource = boto3.resource('dynamodb')
    table = dynamoDBResource.Table(name_table)
    response = table.query(
    KeyConditionExpression=Key(key_table).eq(valeur_query))
    df_fr = pd.DataFrame([response['Items']])
    if len(df_fr.columns) > 0 :
        print("hellooo1")
        df = pd.DataFrame([response['Items'][0]])
        return valeur_query, df["dipl_libelle"].iloc[0]
//
//
df9_tmp["dipl_idpp"] = df8_tmp.apply(lambda x : query_dipl_dynamo("dipl_idpp",x["num_auto"], "ddb-dev-PS_LibreAcces_Dipl_AutExerc")[0], axis=1)

Should I change something else beside creating the index? Too little documentation is available.

Thank you!

Haha
  • 973
  • 16
  • 43

1 Answers1

1

I just found the solution. When we use Indexes we must provide an argument namde IndexName who takes the name of the index in Dynamo. I had to change my code to:

def query_dipl_dynamo(key_table,valeur_query,name_table):
    dynamoDBResource = boto3.resource('dynamodb')
    table = dynamoDBResource.Table(name_table)
    response = table.query(
    IndexName:"NameOfTheIndexInDynamoDB",
    KeyConditionExpression=Key(key_table).eq(valeur_query))
    df_fr = pd.DataFrame([response['Items']])
    if len(df_fr.columns) > 0 :
        df = pd.DataFrame([response['Items'][0]])
        return valeur_query, df["dipl_libelle"].iloc[0]
//
//
df9_tmp["dipl_idpp"] = df8_tmp.apply(lambda x : query_dipl_dynamo("dipl_idpp",x["num_auto"], "ddb-dev-PS_LibreAcces_Dipl_AutExerc")[0], axis=1)
Haha
  • 973
  • 16
  • 43