Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DynamoDb how to query a Global Secondary Index?

I have created a table as following with a Global Secondary Index at Em (representing email).

    TableName : "Users",
    KeySchema: [
        { AttributeName: "Ai", KeyType: "HASH"},  //Partition key
        { AttributeName: "Ui", KeyType: "RANGE" }  //Sort key
    ],
    AttributeDefinitions: [
        { AttributeName: "Ai", AttributeType: "S" },
        { AttributeName: "Ui", AttributeType: "S" },
        { AttributeName: "Em", AttributeType: "S" }
    ],
    GlobalSecondaryIndexes: [
      {
        IndexName: 'EmailIndex',
        KeySchema: [
          { AttributeName: 'Em', KeyType: "HASH" },
        ],
        Projection: {
          ProjectionType: 'ALL'
        },
        ProvisionedThroughput: {
          ReadCapacityUnits: 1,
          WriteCapacityUnits: 1
        }
      }
    ],
    ProvisionedThroughput: {
        ReadCapacityUnits: 1,
        WriteCapacityUnits: 1
    }
}

From my the node.js SDK I am trying to query the table with the EmailIndex as following:

const params = {
  TableName: 'Users',
  IndexName: 'EmailIndex',
  Key: {
    Em: email
  }
}
try {
  const data = await docClient.get(params).promise()
  return data
} catch (error) {
  throw error
} 

I am getting an reply as following:

{
  "message": "The number of conditions on the keys is invalid",
  "code": "ValidationException",
  "time": "2018-07-02T10:33:13.313Z",
  "requestId": "edc5f2e0-3c2b-4354-9342-4a96c74988a6",
  "statusCode": 400,
  "retryable": false,
  "retryDelay": 17.366914444025173
}

I am trying to get the data from the Email address. What am I doing wrong?

like image 516
Kucl Stha Avatar asked Jul 02 '18 10:07

Kucl Stha


People also ask

Can you query on global secondary index?

A global secondary index lets you query over the entire table, across all partitions. A local secondary index lets you query over a single partition, as specified by the partition key value in the query. Queries on global secondary indexes support eventual consistency only.

What is a global secondary index in DynamoDB?

DynamoDB supports two types of secondary indexes: Global secondary index — An index with a partition key and a sort key that can be different from those on the base table. A global secondary index is considered "global" because queries on the index can span all of the data in the base table, across all partitions.

How many global secondary Indexs are there in DynamoDB?

Each table in DynamoDB can have up to 20 global secondary indexes (default quota) and 5 local secondary indexes. For more information about the differences between global secondary indexes and local secondary indexes, see Improving data access with secondary indexes.


1 Answers

You need to run a query on the secondary index, not an item read from the table.

var email = "[email protected]";

var docClient = new AWS.DynamoDB.DocumentClient();

var params = {
    TableName : "Users",
    IndexName : "EmailIndex",
    KeyConditionExpression: "#email = :v_email",
    ExpressionAttributeNames:{
        "#email": "Em"
    },
    ExpressionAttributeValues: {
        ":v_email": email
    }
};

docClient.query(params, function(err, data) {
    if (err) {
        console.error("Unable to query. Error:", JSON.stringify(err, null, 2));
    } else {
        console.log("Query succeeded.");
        data.Items.forEach(function(item) {
            console.log(" -", item.Em + ": " + item.Ai);
        });
    }

Query Data - https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.NodeJs.04.html Indexes - https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/SecondaryIndexes.html

like image 122
Kevin Smith Avatar answered Oct 25 '22 16:10

Kevin Smith