2017-04-11 95 views
3

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

更新信息 也许我的索引没有创建,因为它应该?

表信息:

  • ID - >主分区键
  • 主要排序键 - >名

GSI:

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

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

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

您可以创建并查询GSI,而不需要主ta的分区键BLE。你可以在你想要查询GSI的地方显示你的代码吗?请提及GSI的关键属性。 – notionquest

+0

谢谢!我会用我的代码更新这个问题 – eagleEye

回答

7

注: -由于您没有提供完整的代码,它是很难模拟和找出问题。但是,我创建了类似的表和索引。这对我来说可以。你可以参考下面的代码获取更多细节。

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

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

这已经过测试,工作正常。

1)与索引 '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)将一些数据,以城市表

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

'IndexName:'是使用排序键的主索引生命保护程序。 –