amazon-web-services - get row by partition key on aws dynamodb
问题描述
I'm trying to return a a row with id (partition key)
var db = new AWS.DynamoDB.DocumentClient();
const id = "123";
var queryParams = {
TableName: "students",
IndexName: "id",
KeyConditionExpression: "id = :id",
ExpressionAttributeValues: {
":id": id,
},
};
const query = db.query(queryParams).promise();
but is throwing this error:
ValidationException: 1 validation error detected: Value 'id' at 'indexName' failed to satisfy constraint: Member must have length greater than or equal to 3
isn't partition key enough to get a record? I'm confused partition key is not the same as index name?
I also tried with
var db = new AWS.DynamoDB.DocumentClient();
var queryParams = {
TableName: "students",
Key: {
id: "123",
},
};
const query = db.get(queryParams).promise();
and it doesn't return anything
解决方案
You don't actually have an index named id
. You don't need to provide an index when querying on the partition key. Simply remove this line:
IndexName: "id",
Also, note that both table and index names must be 3-255 characters in length (inclusive), which explains the error message that you see.
The following code (both query and get) works fine for me:
const AWS = require('aws-sdk');
AWS.config.update({ region: 'us-east-1' });
const db = new AWS.DynamoDB.DocumentClient();
const ID = "123";
const TABLE_NAME = "students";
const getParams = {
TableName: TABLE_NAME,
Key: {
id: ID,
},
};
const queryParams = {
TableName: TABLE_NAME,
KeyConditionExpression: "id = :id",
ExpressionAttributeValues: {
":id": ID,
},
};
(async () => {
const rc1 = await db.get(getParams).promise();
console.log('get rc:', rc1);
const rc2 = await db.query(queryParams).promise();
console.log('query rc:', rc2);
})();
推荐阅读
- leaflet - 使用传单绘制路径
- javascript - PM2 与 Meteor 在部署时出错
- php - 将pdf文件拆分为具有相同外观的多个pdf文件
- python - OpenERP功能使整个系统变慢
- teradata - COLLECT STATS 没有得到真正的价值
- java - 更新查询成功执行,但数据未在 java 中更新
- jquery - 通过jquery中的构造函数设置类属性
- c# - 字典
.Values - 它是否分配新的内存和时间复杂度 - java - 错误:类 SupportMapFragment 中的方法 getMapAsync 不能应用于给定类型;
- c# - I'm getting extra string char when I use GetBody
() for BrokeredMessage but works perfect with GetBody () c#