【问题标题】:Querying DynamoDB without Primary Key查询没有主键的 DynamoDB
【发布时间】:2017-04-11 17:03:37
【问题描述】:

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

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

表信息:

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

GSI:

  • 分区键/主键-->城市
  • 预计-->全部

当我从节点查询时,我将城市和索引名称作为参数发送:

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

【问题讨论】:

  • 无需主表的分区键即可创建和查询GSI。你能在你试图查询 GSI 的地方显示你的代码吗?请同时提及 GSI 的关键属性。
  • 谢谢!我会用我的代码更新问题

标签: amazon-dynamodb


【解决方案1】:

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

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

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

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

1) 使用索引“city_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

    });

2) 向城市表中插入一些数据

3) 使用索引查询:-

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

【讨论】:

  • IndexName : 是带有排序键的主索引的救星。 ?
  • 我正在使用所有参数(TableNameIndexName 等)执行相同的查询,然后我得到Query condition missed key schema element。任何额外的见解?我在表中有一个 GSI。
【解决方案2】:

这是我使用 scan 使用 Node.js(通过另一个字段查询)的实现:

  var params = {
    TableName: 'TableName',
    FilterExpression: 'AnotherFieldName = :email',
    ExpressionAttributeValues: {
      ":email": { S: emailISearchFor }  
    }
  };

  ddb.scan(params, function(err, data){
    if(err){
      ...
    } else {

      if(data.Items.length > 0){ // here is the info
        valueIWant = data.Items[0].PrimaryKeyName.S;
      }

    }

  });

【讨论】:

    猜你喜欢
    • 2015-02-20
    • 2018-05-04
    • 1970-01-01
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    • 2020-09-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多