首页 文章

查询没有主键的DynamoDB

提问于
浏览
8

我需要通过与其主键不同的键来查询DynamoDB表 . 我试图为它创建一个全球二级索引 . 但是我收到此错误:“查询关键条件不支持dynamodb” . 通过查看一些示例,看起来我不能通过二级索引查询,除非我还包括主索引/键,这是正确的吗?假设我需要查询在某个城市工作的所有员工,我可以在没有employeeID的情况下进行查询吗?

Updated info 也许我的索引不是应该创建的?

表信息:

  • id - >主分区键

  • 主要排序键 - >名称

GSI:

  • 分区键/主键 - >城市

  • 预计 - >全部

当我从节点I查询作为参数发送的城市,以及索引名称:

const filter = { city: city};
    return this.getRecordsFromDb(filter, { IndexName: "myIndexName" })
        .then(records => __.head(records));

1 回答

  • 12

    Note:- 由于您尚未提供完整代码,因此很难模拟和识别问题 . 但是,我创建了类似的表和索引 . 这对我来说可以 . 您可以参考以下代码了解更多详情 .

    这是表创建脚本和查询索引 .

    如果需要,您可以更改表名称和索引名称 . 我遵循了你在帖子中提到的相同的键属性结构 .

    这已经过测试并且工作正常 .

    1) Create table 'city' with index 'city_index':-

    var params = {
            TableName: 'city',
            KeySchema: [ // The type of of schema.  Must start with a HASH type, with an optional second RANGE.
                { // Required HASH type attribute
                    AttributeName: 'id',
                    KeyType: 'HASH',
                },
                { // Required HASH type attribute
                    AttributeName: 'name',
                    KeyType: 'RANGE',
                }            
    
            ],
            AttributeDefinitions: [ // The names and types of all primary and index key attributes only
                {
                    AttributeName: 'id',
                    AttributeType: 'S', // (S | N | B) for string, number, binary
                },
                {
                    AttributeName: 'name',
                    AttributeType: 'S', // (S | N | B) for string, number, binary
                },
                {
                    AttributeName: 'city',
                    AttributeType: 'S', // (S | N | B) for string, number, binary
                },
    
            ],
            ProvisionedThroughput: { // required provisioned throughput for the table
                ReadCapacityUnits: 400, 
                WriteCapacityUnits: 400, 
            },
            GlobalSecondaryIndexes: [ // optional (list of GlobalSecondaryIndex)
                { 
                    IndexName: 'city_index', 
                    KeySchema: [
                        { // Required HASH type attribute
                            AttributeName: 'city',
                            KeyType: 'HASH',
                        }
                    ],
                    Projection: { // attributes to project into the index
                        ProjectionType: 'ALL' // (ALL | KEYS_ONLY | INCLUDE)
                    },
                    ProvisionedThroughput: { // throughput to provision to the index
                        ReadCapacityUnits: 400,
                        WriteCapacityUnits: 400,
                    },
                },
                // ... more global secondary indexes ...
            ],
    
        };
        dynamodb.createTable(params, function(err, data) {
            if (err){ console.log("error :" +JSON.stringify(err));} // an error occurred
            else console.log("success :" +JSON.stringify(data)); // successful response
    
        });
    

    2) Insert some data to city table

    3) Query using index:-

    var docClient = new AWS.DynamoDB.DocumentClient();
    var table = "city";
    var params = {
        TableName : table,
        IndexName : 'city_index',
        KeyConditionExpression : 'city = :cityVal', 
        ExpressionAttributeValues : {
            ':cityVal' : 'london'        
        }
    };
    
    docClient.query(params, function(err, data) {
        if (err) {
            console.error("Unable to read item. Error JSON:", JSON.stringify(err,
                    null, 2));
        } else {
            console.log("GetItem succeeded:", JSON.stringify(data, null, 2));
        }
    });
    

相关问题