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

查看:428
本文介绍了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):


  • 通过哈希键+范围键获取单个项目

  • 在所有项目中查询特定的哈希键

  • 扫描整个表

使用用户ID的哈希键和组织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也会异步更新,因此它们最终是一致的。它们还具有自己的预置读/写吞吐量,并且从理论上讲,这可能会影响您在表上实现的最大吞吐量,具体取决于您的访问模式。

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天全站免登陆