dynamodb unmarshall阵列DynamoDB记录? (SDK V3 JavaScript)

发布于 2025-02-07 05:13:38 字数 1600 浏览 2 评论 0 原文

我找不到一种揭开DynamoDB记录阵列的方法吗?我从DynamoDB中的查询中获取了多个项目,并且我无法使用来自数组上的@AWS-SDK/UTIL-DYNAMODB 函数记录。当我迭代一系列记录并删除每个记录时,这是有效的,但这不是最好的解决方案,因为我会恢复大量数据。我如何使DynamoDB返回多个记录,而不必通过一系列记录迭代到每个记录中删除每个记录?

代码:

  const client = new DynamoDBClient({ region: 'us-east-1' });  
  const currentDateUTC = moment().utc().format('YYYY-MM-DD');

  const command = new QueryCommand({
    TableName: 'Device-Data',
    IndexName: 'timestamp-index',
    KeyConditionExpression: '#timestamp = :timestamp',
    ExpressionAttributeNames: { '#timestamp': 'timestamp' },
    ExpressionAttributeValues: { ':timestamp': { S: currentDateUTC } },
  });
  
  const data = await client.send(command)

响应:

[
  {
    deviceId: {
      S: "test1",
    },
    userId: {
      S: "318ecd46-bead-440f-83eb-60345aaa1c40",
    },
    timestamp: {
      S: "2022-06-12",
    },
    frequency: {
      S: "Monthly",
    },
  },
  {
    deviceId: {
      S: "test2",
    },
    userId: {
      S: "318ecd46-bead-440f-83eb-60345aaa1c40",
    },
    timestamp: {
      S: "2022-06-12",
    },
    frequency: {
      S: "Monthly",
    },
  },
]

所需响应:

[
  {
    deviceId: "test1",
    userId: "318ecd46-bead-440f-83eb-60345aaa1c40",
    timestamp: "2022-06-12",
    frequency: "Monthly",
  },
  {
    deviceId: "test2",
    userId: "318ecd46-bead-440f-83eb-60345aaa1c40",
    timestamp: "2022-06-12",
    frequency: "Monthly",
  },
]

I can't find a way to unmarshal an array of DynamoDB records? I'm getting multiple items back from my query in DynamoDB, and I can't use the unmarshal function that comes from @aws-sdk/util-dynamodb on an array of records. When I iterate through the array of records and unmarshal each one, that works, but this is not the best solution since I'm getting back a lot of data. How can I get DynamoDB to return multiple records without iterating through the array of records to unmarshall each one?

Code:

  const client = new DynamoDBClient({ region: 'us-east-1' });  
  const currentDateUTC = moment().utc().format('YYYY-MM-DD');

  const command = new QueryCommand({
    TableName: 'Device-Data',
    IndexName: 'timestamp-index',
    KeyConditionExpression: '#timestamp = :timestamp',
    ExpressionAttributeNames: { '#timestamp': 'timestamp' },
    ExpressionAttributeValues: { ':timestamp': { S: currentDateUTC } },
  });
  
  const data = await client.send(command)

Response:

[
  {
    deviceId: {
      S: "test1",
    },
    userId: {
      S: "318ecd46-bead-440f-83eb-60345aaa1c40",
    },
    timestamp: {
      S: "2022-06-12",
    },
    frequency: {
      S: "Monthly",
    },
  },
  {
    deviceId: {
      S: "test2",
    },
    userId: {
      S: "318ecd46-bead-440f-83eb-60345aaa1c40",
    },
    timestamp: {
      S: "2022-06-12",
    },
    frequency: {
      S: "Monthly",
    },
  },
]

Desired Response:

[
  {
    deviceId: "test1",
    userId: "318ecd46-bead-440f-83eb-60345aaa1c40",
    timestamp: "2022-06-12",
    frequency: "Monthly",
  },
  {
    deviceId: "test2",
    userId: "318ecd46-bead-440f-83eb-60345aaa1c40",
    timestamp: "2022-06-12",
    frequency: "Monthly",
  },
]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天涯沦落人 2025-02-14 05:13:39

您可以在V3中执行这样的操作:

const { unmarshall } = require("@aws-sdk/util-dynamodb");
    
const items = data.Items.map( (item) => {
    return unmarshall(item);
});

You can do something like this in v3:

const { unmarshall } = require("@aws-sdk/util-dynamodb");
    
const items = data.Items.map( (item) => {
    return unmarshall(item);
});
情深缘浅 2025-02-14 05:13:39

DynamodBdocumentClient 是为了自动删除数据并使其更易于在代码中使用的数据。

import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
import { DynamoDBDocumentClient, QueryCommand } from '@aws-sdk/lib-dynamodb';

...

const client = new DynamoDBClient();
const docClient = DynamoDBDocumentClient.from(client);

const command = new QueryCommand({ [config here...] });  
const data = await docClient.send(command);

这将为您提供Dynamo数据作为常规JavaScript对象,如您所期望的。

文档:

DynamoDBDocumentClient is made to automatically unmarshall your data and make it easier to work with in your code.

import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
import { DynamoDBDocumentClient, QueryCommand } from '@aws-sdk/lib-dynamodb';

...

const client = new DynamoDBClient();
const docClient = DynamoDBDocumentClient.from(client);

const command = new QueryCommand({ [config here...] });  
const data = await docClient.send(command);

This will give you the Dynamo data as a regular JavaScript object as you'd expect.

Documentation: https://docs.aws.amazon.com/en_us/AWSJavaScriptSDK/v3/latest/classes/_aws_sdk_lib_dynamodb.dynamodbdocumentclient-1.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文