BatchGetItem 或查询 DynamoDb - 按范围查询 [英] BatchGetItem or Query DynamoDb - Query by Range

查看:27
本文介绍了BatchGetItem 或查询 DynamoDb - 按范围查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 User 的表.它有一个用户 ID 的哈希键和一个组织 ID 的范围键.

I have a table called User. It has a Hash key of User Id and a Range key of Organization Id.

如何返回所有组织 ID 为3"的用户

How can I return all of the Users that have the Organization Id of "3"

(顺便说一下,这是一个 Lambda 函数)

(This is a Lambda function, by the way)

这段代码给了我一个错误:

This code is giving me an error:

console.log('Loading event');
var AWS = require('aws-sdk');
var dynamodb = new AWS.DynamoDB({apiVersion: '2012-08-10'});

exports.handler = function(event, context) {
dynamodb.listTables(function(err, data) {
});

var params = {
    "TableName": "PoliceUser",
     "Key":
        {"User Id"   : {"S":event.objectId}, "Organization Id" : {"S": event.organizationId}
    },
   "ProjectionExpression": "#firstName, #lastName, #longitude, #latitude, #organizationName",
   "ExpressionAttributeNames" : {"#firstName": "First Name", "#lastName": "Last Name", "#longitude": "Longitude", "#latitude": "Latitude", "#organizationName": "Organization"},
   "ConsistentRead"    : true
  }

   dynamodb.BatchGetItem(params, function(err, data)
{
    if (err) {
        context.fail('error','Error updating item: '+err);
        console.log(err);
    }
  else  
  {
      //  console.log('great success: '+JSON.stringify(data, null, '  '));
       console.log(data);   
        context.succeed( data);
    }

    // successful response


});
};

推荐答案

DynamoDB 提供了几种查询项目的方法(假设您的表具有哈希键和范围键):

DynamoDB provides a few ways to query items (assuming your table has a hash key and range key):

  • 通过哈希键 + 范围键获取单个项目
  • 查询所有项目的特定哈希键
  • 扫描整个表格

使用User Id"哈希键和Organization Id"范围键,您只能查询与单个用户关联的所有组织.

With a hash key of "User Id" and range key of "Organization Id" you can only query for all of the organizations that an individual user is associated with.

听起来你想要相反的,属于一个组织的所有用户.

It sounds like you want the opposite, all of the users that belong to an organization.

一种选择是交换您的哈希键和范围键.在您这样做之前,请确保这对您的用例确实有意义.

One option would be to swap your hash key and range key. Before you do that make sure that this actually makes sense for your use case.

或者,您可以将 全球二级索引 添加到您的哈希键是组织 ID"和范围键是用户 ID"的表,同时保留现有的哈希/范围键,因为它们当前存在.然后,您就可以使用该索引返回所有组织 ID 为3"的用户.

Alternatively you could add a Global Secondary Index to your table where the hash key is "Organization Id" and range key is "User Id" while leaving the existing hash/range key as they currently exist. You would then be able to use this index to return all users that have the Organization Id of "3".

我建议您在创建 GSI 之前先阅读一些 GSI.它们非常有用,但可能很棘手.GSI 在技术上是数据的物理副本,因此您需要决定要投影哪些列(使用索引时可以读取哪些列).GSI 也是异步更新的,因此它们最终是一致的.它们还具有自己预置的读/写吞吐量,理论上可以根据您的访问模式影响您在表上可以实现的最大吞吐量.

I'd recommend you read up on GSIs some before creating one. They are very useful, but can be tricky. The GSI is technically a physical copy of the data so you need to decide which columns you want to project (what columns you can read when using an index). Also GSIs are asynchronously updated so they are eventually consistent. They also have their own provisioned read/write throughput and can theoretically impact the maximum throughput you can achieve on a table depending on your access patterns.

这篇关于BatchGetItem 或查询 DynamoDb - 按范围查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆