Amazon dynamodb 查询没有主键的DynamoDB

Amazon dynamodb 查询没有主键的DynamoDB,amazon-dynamodb,Amazon Dynamodb,我需要通过一个不同于其主键的键来查询DynamoDB表。我试图为它创建一个全局二级索引。但是我得到了这个错误:“查询键条件不支持dynamodb”。通过查看一些示例,看起来我无法通过辅助索引进行查询,除非我还包括主索引/键,对吗?假设我需要查询在某个城市工作的所有员工,我可以在没有employeeID的情况下进行查询吗 更新信息 也许我的索引没有按应有的方式创建 表格信息: id-->主分区键 主排序键-->名称 GSI: 分区键/主键-->城市 投影-->全部 当我从节点查询时,我将城

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

更新信息 也许我的索引没有按应有的方式创建

表格信息:

  • id-->主分区键
  • 主排序键-->名称
GSI:

  • 分区键/主键-->城市
  • 投影-->全部
当我从节点查询时,我将城市和索引名作为参数发送:

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

注意:-由于您没有提供完整的代码,因此很难模拟和识别问题。但是,我创建了类似的表和索引。这对我来说很好。有关更多详细信息,请参阅下面的代码

下面是表创建脚本和查询索引

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

这已经过测试,运行良好

1)创建带有索引“city\u index”的表“city”:

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

    });
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));
    }
});
2)将一些数据插入城市表

3)使用索引进行查询:-

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

    });
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));
    }
});

这是我对Node.js(通过另一个字段查询)的实现,包括:


您可以在不使用主表的分区键的情况下创建和查询GSI。你能把你的代码显示在你试图查询GSI的地方吗?请同时提及GSI的关键属性。谢谢!我将用我的代码
IndexName:
更新这个问题,它是带有排序键的主索引的救命稻草。我使用所有参数(
TableName
IndexName
,等等)执行相同的查询,并得到
查询条件缺少关键模式元素
。还有其他的见解吗?我桌上有一个GSI。