【问题标题】:'The provided key element does not match the schema' for secondary index二级索引的“提供的关键元素与架构不匹配”
【发布时间】:2016-11-01 07:07:04
【问题描述】:

我有一个描述的 DynamoDB 表(用户应该可以通过唯一的 user_id 或用户名 + system_id 的组合来选择):

"TableName": "auth-users",
"KeySchema": [ {
        "KeyType": "HASH", 
        "AttributeName": "user_id"
    }
], 
"AttributeDefinitions": [ {
    {
        "AttributeName": "user_id", 
        "AttributeType": "S"
    },
        "AttributeName": "system_id", 
        "AttributeType": "S"
    },  {
        "AttributeName": "username", 
        "AttributeType": "S"
    }
], 
"GlobalSecondaryIndexes": [
    {
        "IndexName": "username-system_id-index", 
        "Projection": {
            "ProjectionType": "ALL"
        }, 
        "IndexStatus": "ACTIVE", 
        "KeySchema": [ {
               "KeyType": "HASH", 
               "AttributeName": "username"
            }, {
               "KeyType": "RANGE", 
               "AttributeName": "system_id"
            }
        ]
    }
],
... 

当我使用主键运行以下代码时,我得到了一个记录列表,正如预期的那样:

var AWS = require('aws-sdk');
var dynamodb = new AWS.DynamoDB({region: 'ap-southeast-2');
var userTable = 'auth-users';
var key = { user_id: '123412341234' };

dynamodb.getItem({
   TableName: tableName,
   Key: key
}, function(err,data) { 
   console.error('err:',err); 
   console.info('data:', data); 
});

...但是如果我尝试使用全局二级索引,请将 key 替换为:

var key = {
    username:  {S: 'testuser'}, 
    system_id: {S: 'test-system'}
};

然后我得到一个 ValidationException: The provided key element does not match the schema.

【问题讨论】:

    标签: node.js amazon-web-services amazon-dynamodb


    【解决方案1】:

    ...没关系 - RTFM:

    GetItem 操作返回项目的一组属性 给定的主键

    要使用二级索引,您需要执行query 并提供索引名称。

    【讨论】:

      猜你喜欢
      • 2015-10-31
      • 1970-01-01
      • 2021-08-26
      • 2019-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多