在没有全局索引的情况下查询DynamoDB

时间:2017-11-23 18:35:13

标签: node.js amazon-dynamodb

我的DynamoDB中有一个简单的表,我想做一个简单的查询,只是一个status = active

的列表
const AWS = require('aws-sdk');
const config = require('../../../../config/dynamo');

const { URI } = config;

AWS.config.update({
  region: 'us-east-1'
});

const dynamodb = new AWS.DynamoDB({
  endpoint: new AWS.Endpoint(URI)
});

const params = {
  AttributeDefinitions: [{
    AttributeName: 'idPhysicalPerson',
    AttributeType: 'N'
  },
  {
    AttributeName: 'cpf',
    AttributeType: 'S'
  }],
  KeySchema: [{
    AttributeName: 'idPhysicalPerson',
    KeyType: 'HASH'
  },
  {
    AttributeName: 'cpf',
    KeyType: 'RANGE'
  }],
  ProvisionedThroughput: {
    ReadCapacityUnits: 5,
    WriteCapacityUnits: 5
  },
  TableName: 'PhysicalPerson'
};
dynamodb.createTable(params, (err, data) => {
  if (err) {
    console.log(err, err.stack);
  } else {
    console.log(data);
  }
});

这个表包含很多属性,如果我想执行一个简单的查询,我总是需要将这些字段放在全局索引中?

我只想获取具有active状态的所有数据,但在实际应用程序中,如果我有advanced filter这样的功能,我可以设置很多属性,如何处理在DynamoDB中,whitout将所有属性都放在全局索引中?

1 个答案:

答案 0 :(得分:1)

您正在寻找scan feature

在DynamoDB中,如果您有大型数据集(because it reads the entire dataset before returning what you want),则不建议这样做。

您可能希望在所有数据都可用的情况下执行复合排序键:

cpf_otherValue_otherValue2_...

可能不是全部,但使用最常用的2/3和查询begins_with http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Query.html#DDB-Query-request-KeyConditionExpression

或者如果你有高级搜索功能,最好有一个ElasticSearch集群而不是DynamoDB:DynamoDB是一个支持大数据集和高吞吐量的键/值存储,高级搜索功能不是常见的用例:但是再次,如果你没有太多的数据,它可以工作。