简体   繁体   English

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

[英]AWS Lambda: How to Add Numbers to a NS Set in Dynamodb

The Issue 问题

I have tried several approaches, but haven't been able to find out how to add numbers to a NS set. 我尝试了几种方法,但未能找到如何将数字添加到NS集。 This is all running inside a lambda function. 这都是在lambda函数中运行的。

What I'm trying to accomplish 我正在努力实现的目标

I am creating a dynamodb table where different colors in hex align to a set of ids. 我正在创建一个dynamodb表,其中十六进制中的不同颜色与一组id对齐。 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. 我正在优化表以进行快速读取并避免重复,这就是为什么我想为每个十六进制维护一组id。

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 然后我尝试在集合中添加更多ID

Using the Dynamodb Update Item Documentation standards 使用Dynamodb更新项目文档标准

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(?) 这会返回以下错误,因此dynamo认为:newIds是一个地图类型而不是一个集合(?)

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

I have also tried these alternative approaches 我也试过这些替代方法

Try 2: 试试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): 错误2(同样的错误):

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

Try 3: 尝试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): 错误3(同样的错误):

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

Try 4: 尝试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) 错误4(类似的错误,但发电机认为我这次添加了一个清单)

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

Stack Overflow/Github Questions I've Tried Stack Overflow / Github我试过的问题

https://stackoverflow.com/a/37585600/4975772 (I'm adding to a set, not a list) https://stackoverflow.com/a/37585600/4975772 (我正在添加一个集合,而不是列表)

https://stackoverflow.com/a/37143879/4975772 (I'm using javascript, not python, but I basically need this same thing just different syntax) https://stackoverflow.com/a/37143879/4975772 (我使用的是javascript,而不是python,但我基本上需要同样的东西只是不同的语法)

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 https://github.com/awslabs/dynamodb-document-js-sdk/issues/40#issuecomment-123003444 (我需要做这件事,但我没有使用dynamodb-document-js-sdk,我使用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: 这导致了这个dynamodb表:

使用

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) 创建集并向集添加项(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 ) (不要将此代码用于将来的开发,我只将其包含在内以帮助使用现有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 请注意,当我问这个问题时,结果集看起来像一个文字的json地图对象(在dynamodb屏幕截图中查看),这将解释此错误消息

"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 该解决方案可在(现在已删除的AWS Labs dynamodb-document-js-sdk文档中找到

The full documentation for the newer Document Client can be viewed here: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html 可以在此处查看较新的Document Client的完整文档: http//docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html

I've been struggling with this too. 我也一直在努力解决这个问题。 I discovered that there are actually 2 api's for DynamoDB. 我发现DynamoDB实际上有2个api。

AWS.DynamoDB AWS.DynamoDB

AWS.DynamoDBDocumentClient AWS.DynamoDBDocumentClient

Like you, I was not able to make the ADD function work. 和你一样,我无法使ADD功能正常工作。 I tried to import and use the DynamoDB api instead of the DynamoDBDocumentClient api. 我尝试导入并使用DynamoDB api而不是DynamoDBDocumentClient api。 It solved the problem for me. 它解决了我的问题。 Below is my code snippet that works with the DynamoDB api but not with the DynamoDBDocumentClient api. 下面是我的代码片段,它与DynamoDB api一起使用,但不与DynamoDBDocumentClient api一起使用。 I use a String Set instead of a Number Set, but that won't make a difference I guess. 我使用字符串集而不是数字集,但我认为这不会产生任何影响。

var AWS = require('aws-sdk');
var dynamo = new AWS.DynamoDB();
// var dynamo = new AWS.DynamoDB.DocumentClient();

...

var params = {};
params.TableName = “MyTable”;
params.ExpressionAttributeValues = { ':newIds': { "SS": ["someId"] } };
// :newIds represents a new dynamodb set with 1 element
params.UpdateExpression = "ADD someAttribute :newIds";
dynamoClient.updateItem(params, function(err, data) { ….}

This answer might be helpful to those who are using npm dynamoDB module 这个答案可能对那些使用npm dynamoDB模块的人有所帮助

We had the same issue . 我们遇到了同样的问题。 AS we were using npm dynamodb module for our queries rather than exporting from AWS.DynamoDB module, given solution of AWS.DynamoDBDocumentClint was implementable untill and unless we could shift from npm dynamoDb module to AWS.DynamoDB queries. 当我们使用NPM dynamodb模块为我们的查询,而不是从出口AWS.DynamoDB模块,给出解决方案AWS.DynamoDBDocumentClint是可实现的,直到除非我们能够从NPM dynamoDb模块转移到AWS.DynamoDB查询。 So instead of shifting/transforming queries. 因此,而不是转移/转换查询。

We tried to downgrade dynamoDB npm module to version ~1.1.0 and it worked. 我们试图将dynamoDB npm模块降级到版本~1.1.0并且它有效。 Previous version was 1.2.X. 以前的版本是1.2.X.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM