如何使用dynamo db和node js基于二级索引进行查询

时间:2017-04-22 06:01:32

标签: javascript node.js amazon-dynamodb aws-sdk

如何使用dynamo db和node js

查询二级索引

到目前为止,这是我的代码,但它不起作用

getObjectByAlternateKey: function (keyName, keyObj, indexName, callback, consistentRead) {
    var params = {
        TableName: this.tableName,
        KeyConditionExpression: keyName + ' = :v_key',
        ExpressionAttributeValues: converters.jsObjectToDynamoMap({ ':v_key': keyObj }),
        IndexName: indexName,
        ConsistentRead: !!consistentRead
    };
    this.dynamo.query(params, function (error, data) {
        console.dir(data);
        if (error) {
            return callback(error);
        }
        if (data.Items && data.Items.length > 0) {
            if (data.Items.length !== 1) {
                console.warn("Got more than one item returned for query!", { query: params, data: data });
            }
            return callback(null, converters.dynamoMapToJsObject(data.Items[0]));
        }

        return callback(null, null);
    });
},

这是我用来创建表格的云形态模板:

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Parameters": {
    "TableName": {
      "Description": "Table name to use",
      "Type": "String",
      "Default": "test-user-unique-ids-prod-ue1"
    },
    "ReadCapacityUnits": {
      "Description": "Provisioned read throughput",
      "Type": "Number",
      "Default": "100",
      "MinValue": "1",
      "MaxValue": "10000",
      "ConstraintDescription": "must be between 1 and 10000"
    },
    "WriteCapacityUnits": {
      "Description": "Provisioned write throughput",
      "Type": "Number",
      "Default": "100",
      "MinValue": "1",
      "MaxValue": "10000",
      "ConstraintDescription": "must be between 1 and 10000"
    }
  },
  "Resources": {
    "sparkUserUniqueIds": {
      "Type": "AWS::DynamoDB::Table",
      "Properties": {
        "TableName": {
          "Ref": "TableName"
        },
        "AttributeDefinitions": [
          {
            "AttributeName": "guid",
            "AttributeType": "S"
          },
          {
            "AttributeName": "unique_id",
            "AttributeType": "S"
          }
        ],
        "KeySchema": [
          {
            "AttributeName": "guid",
            "KeyType": "HASH"
          }
        ],
        "GlobalSecondaryIndexes": [
          {
            "IndexName": "test-user-by-unique-id",
            "KeySchema": [
              {
                "AttributeName": "unique_id",
                "KeyType": "HASH"
              }
            ],
            "Projection": {
              "ProjectionType": "ALL"
            },
            "ProvisionedThroughput": {
              "ReadCapacityUnits": {
                "Ref": "ReadCapacityUnits"
              },
              "WriteCapacityUnits": {
                "Ref": "WriteCapacityUnits"
              }
            }
          }
        ],
        "ProvisionedThroughput": {
          "ReadCapacityUnits": {
            "Ref": "ReadCapacityUnits"
          },
          "WriteCapacityUnits": {
            "Ref": "WriteCapacityUnits"
          }
        }
      }
    }
  }
}

1 个答案:

答案 0 :(得分:2)

记录您的params值,它应该是这样的

{ TableName: 'test-user-unique-ids-prod-ue1',
  IndexName: 'test-user-by-unique-id',
  KeyConditions: 
   { unique_id: 
      { ComparisonOperator: 'EQ',
        AttributeValueList: [ { S: 'test' } ] } } }