AWS Lambda:如何将数字添加到 Dynamodb 中的 NS 集

AWS Lambda: How to Add Numbers to a NS Set in Dynamodb(AWS Lambda:如何将数字添加到 Dynamodb 中的 NS 集)

本文介绍了AWS Lambda:如何将数字添加到 Dynamodb 中的 NS 集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

我尝试了几种方法,但无法找到如何将数字添加到 NS 集.这一切都在一个 lambda 函数中运行.

我想要完成的事情

我正在创建一个 dynamodb 表,其中十六进制的不同颜色与一组 ID 对齐.我正在优化表以实现快速读取并避免重复,这就是为什么我想为每个十六进制维护一组 id.

我如何向表格中添加项目:

let doc = require('dynamodb-doc');让 dynamo = new doc.DynamoDB();变量对象 = {'表名':'十六进制','物品': {'十六进制': '#FEFEFE',身份证":{'NS':[2,3,4]}}}dynamo.putItem(对象,回调);

结果

然后我尝试向集合中添加更多 id

使用

如果该集合不存在,那么该代码将为您创建它.您还可以使用不同的集合运行该代码以更新集合的元素.超级方便.

创建集合并将项目添加到集合(旧 API)

let doc = require('dynamodb-doc');让 dynamo = new doc.DynamoDB();变量参数 = {表名:'十六进制',键:{'十六进制':'#555555'},UpdateExpression:'添加#oldIds:newIds',表达式属性名称:{'#o​​ldIds' : 'ids'},表达式属性值:{':newIds' : dynamo.Set([2,3], 'N')}};dynamo.updateItem(参数,回调);

(不要将此代码用于未来的开发,我只是将它包含在帮助任何使用现有 DynamoDB 文档 SDK)

原版失败的原因

注意当我问这个问题时,结果集看起来像一个文字 json 地图对象(在 dynamodb 屏幕截图中查看时),这可以解释这个错误消息

errorMessage":Invalid UpdateExpression:运算符或函数的操作数类型不正确;运算符:ADD,操作数类型:MAP"

所以我使用了错误的语法.该解决方案可在(现已弃用)AWS 实验室 dynamodb-document-js-sdk 文档

可在此处查看较新文档客户端的完整文档:http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html

The Issue

I have tried several approaches, but haven't been able to find out how to add numbers to a NS set. This is all running inside a lambda function.

What I'm trying to accomplish

I am creating a dynamodb table where different colors in hex align to a set of ids. I am optimizing the table for fast reads and to avoid duplicates which is why I would like to maintain a set of ids for each hex.

How I'm adding items to the table:

let doc = require('dynamodb-doc');
let dynamo = new doc.DynamoDB();

var object = {
    'TableName': 'Hex',
    'Item': {
        'hex': '#FEFEFE',
        'ids': {
            'NS': [2,3,4]
        }
    }
}

dynamo.putItem(object, callback);

Which results in

Then I try to add more ids to the set

Using the Dynamodb Update Item Documentation standards

var params = {
    "TableName" : "Hex",
    "Key": {
      "hex": "#FEFEFE"
    },
    "UpdateExpression" : "ADD #oldIds :newIds",
    "ExpressionAttributeNames" : {
      "#oldIds" : "ids"
    },
    "ExpressionAttributeValues": { 
      ":newIds" : {"NS": ["5", "6"]}
   },
};

dynamo.updateItem(params, callback);

This returns the following error, so dynamo thinks :newIds is a map type instead of a set(?)

"errorMessage": "Invalid UpdateExpression: Incorrect operand type for operator or function; operator: ADD, operand type: MAP"

I have also tried these alternative approaches

Try 2:

var setTest = new Set([5, 6]);

var params = {
    "TableName" : "Hex",
    "Key": {
      "hex": "#FEFEFE"
    },
    "UpdateExpression" : "ADD #oldIds :newIds",
    "ExpressionAttributeNames" : {
      "#oldIds" : "ids"
    },
    "ExpressionAttributeValues": { 
      ":newIds" : setTest
   },
};

dynamo.updateItem(params, callback);

Error 2 (same error):

"errorMessage": "Invalid UpdateExpression: Incorrect operand type for operator or function; operator: ADD, operand type: MAP"

Try 3:

var params = {
    "TableName" : "Hex",
    "Key": {
      "hex": "#FEFEFE"
    },
    "UpdateExpression" : "ADD #oldIds :newIds",
    "ExpressionAttributeNames" : {
      "#oldIds" : "ids"
    },
    "ExpressionAttributeValues": { 
      ":newIds" : { "NS" : { "L" : [ { "N" : "5" }, { "N" : "6" } ] }}
   },
};

dynamo.updateItem(params, callback);

Error 3 (same error):

"errorMessage": "Invalid UpdateExpression: Incorrect operand type for operator or function; operator: ADD, operand type: MAP"

Try 4:

var params = {
    "TableName" : "Hex",
    "Key": {
      "hex": "#FEFEFE"
    },
    "UpdateExpression" : "ADD #oldIds :newIds",
    "ExpressionAttributeNames" : {
      "#oldIds" : "ids"
    },
    "ExpressionAttributeValues": { 
      ":newIds" : [5,6]
   },
};

dynamo.updateItem(params, callback);

Error 4 (similar error, but dynamo thinks I'm adding a list this time)

"errorMessage": "Invalid UpdateExpression: Incorrect operand type for operator or function; operator: ADD, operand type: LIST"

Stack Overflow/Github Questions I've Tried

https://stackoverflow.com/a/37585600/4975772 (I'm adding to a set, not a list)

https://stackoverflow.com/a/37143879/4975772 (I'm using javascript, not python, but I basically need this same thing just different syntax)

https://github.com/awslabs/dynamodb-document-js-sdk/issues/40#issuecomment-123003444 (I need to do this exact thing, but I'm not using the dynamodb-document-js-sdk, I'm using AWS Lambda

解决方案

How to Create a Set and Add Items to a Set

let AWS = require('aws-sdk');
let docClient = new AWS.DynamoDB.DocumentClient();

...

var params = {
    TableName : 'Hex',
    Key: {'hex': '#FEFEFE'},
    UpdateExpression : 'ADD #oldIds :newIds',
    ExpressionAttributeNames : {
      '#oldIds' : 'ids'
    },
    ExpressionAttributeValues : {
      ':newIds' : docClient.createSet([1,2])
    }
};

docClient.update(params, callback);

Which results in this dynamodb table:

If the set doesn't exist, then that code will create it for you. You can also run that code with a different set to update the set's elements. Super convenient.

Create a Set and Add Items to a Set (OLD API)

let doc = require('dynamodb-doc');
let dynamo = new doc.DynamoDB();

var params = {
    TableName : 'Hex',
    Key: {'hex': '#555555'},
    UpdateExpression : 'ADD #oldIds :newIds',
    ExpressionAttributeNames : {
      '#oldIds' : 'ids'
    },
    ExpressionAttributeValues : {
      ':newIds' : dynamo.Set([2,3], 'N')
    }
};

dynamo.updateItem(params, callback);

(Don't use this code for future development, I only include it to help anyone using the existing DynamoDB Document SDK)

Why the Original Was Failing

Notice how when I asked the question, the resulting set looked like a literal json map object (when viewed in the dynamodb screenshot), which would explain this error message

"errorMessage": "Invalid UpdateExpression: Incorrect operand type for operator or function; operator: ADD, operand type: MAP"

So I was using the wrong syntax. The solution is found in the (now depricated) AWS Labs dynamodb-document-js-sdk docs

The full documentation for the newer Document Client can be viewed here: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html

这篇关于AWS Lambda:如何将数字添加到 Dynamodb 中的 NS 集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:AWS Lambda:如何将数字添加到 Dynamodb 中的 NS 集

基础教程推荐