I have a table with an attribute with name id and of type HASH. I want to get all items from a array of id's.
{
TableName: `MyTable`,
FilterExpression: 'id IN (:id)',
ExpressionAttributeValues: { ':id': ids },
};
What should I do to get all items by my ids?
You can also use DocumentClient and batchGet.
const AWS = require('aws-sdk');
const documentClient = new AWS.DynamoDB.DocumentClient();
let queryParams = {RequestItems: {}};
queryParams.RequestItems['tableName'] = {
Keys: [{'id': 'Value1'}, {'id': 'value2'}],
ProjectionExpression: 'id' //define other fileds that you have Ex: 'id,name'
};
documentClient.batchGet(queryParams, function (err, data) {
if (err) {
console.log('failure:getItemByBatch data from Dynamo error', err);
} else {
console.log('success:getItemByBatch data from Dynamo data');
console.log(data)
}
});
Please use BatchGetItem API to get multiple values from DynamoDB table.
BatchGetItem
Example:-
var dynamodb = new AWS.DynamoDB({maxRetries: 5, retryDelayOptions: {base: 300} });
var table = "Movies";
var year_val = 2015;
var title = "The Big New Movie";
var params = {
"RequestItems" : {
"Movies" : {
"Keys" : [ {
"yearkey" : {N : "2016"},
"title" : {S : "The Big New Movie 1"}
} ]
}
},
"ReturnConsumedCapacity" : "TOTAL"
};
dynamodb.batchGetItem(params, function(err, data) {
if (err) {
console.error("Unable to get item. Error JSON:", JSON.stringify(err,
null, 2));
} else {
console.log("Movie data:", JSON.stringify(data, null, 2));
}
});
Its in C#, below code is to get all items by an array of ids from a dynamodb table having different guid's using BatchGet or CreateBatchGet
string tablename = "AnyTableName"; //table whose data you want to fetch
var BatchRead = ABCContext.Context.CreateBatchGet<ABCTable>(
new DynamoDBOperationConfig
{
OverrideTableName = tablename;
});
foreach(string Id in IdList) // in case you are taking string from input
{
Guid objGuid = Guid.Parse(Id); //parsing string to guid
BatchRead.AddKey(objGuid);
}
await BatchRead.ExecuteAsync();
var result = BatchRead.Results;
// ABCTable is the table modal which is used to create in dynamodb & data you want to fetch
Related
I am trying to overwrite an item in DynamoDB (that uses a primary key called username) by using put as shown below:
console.log('writing commands',existingCommands,message.username);
var t2 = performance();
var writeParams = {
Item: {
username: message.username,
commands: existingCommands // Sorry for the confusing name, due to deepExtend existingCommands are the new commands
},
TableName: TableName
};
docClient.put(writeParams, function(err, data){
if(err){
console.error('error',err);
} else {
console.log('write result',data);
var t3 = performance();
console.info('delete & write performance',(t3-t2).toFixed(3));
}
// End function
context.done();
});
That works for:
Inserting a new item where the username doesn't exist.
Updating an item that matches the schema of the Item i'm trying to insert, for example, I'm trying to insert that item:
{
"username":"ausin441062133",
"commands": {
"command1":"command",
"command2":"command"
}
}
and if there's an item that matches the schema and the username it'll get overwritten, i.e.
{
"username":"ausin441062133",
"commands": {
"command1":"I will be overwritten",
"command2":"I will be overwritten"
}
}
But when there's an item with the exact username but different schema, it doesn't work, i.e.
{
"username":"ausin441062133",
"commands": {
"command1":"I will NOT be overwritten"
}
}
What command do I need to use to overwrite an existing item if it matches the username?
Eventually as Dmitry suggested update works but it needs some different params as opposed to put here's my code:
// Step 3 write command back
console.log('writing commands',existingCommands,message.username);
var t2 = performance();
var updateParams = {
Key: {
username: message.username
},
UpdateExpression: "set commands = :c",
ExpressionAttributeValues: {
":c":existingCommands
},
ReturnValues: "UPDATED_NEW",
TableName: TableName
};
docClient.update(updateParams, function(err, data){
if(err){
console.error('error',err);
} else {
console.log('write result',data);
var t3 = performance();
console.info('delete & write performance',(t3-t2).toFixed(3));
}
// End function
context.done();
});
Im new to DynamoDB and have a table which is "feeds" and partition key is "id" and i have 3 other attributes which are "category", "description", "pubDate".
I want to query the "category" attribute. But it doesn't work, because i can only query the partition key (hashkey), if im right.
Now my query is that which doesnt work;
let category = event.category;
const params = {
Key: {
"category": {
S: category
}
},
TableName: "feeds"
};
dynamodb.getItem(params, function (err, data) {
if (err) {
console.log(err);
callback(err);
}
else {
console.log(data);
callback(null, data);
}
});
How can i make it work? I tried to write a scan query but i couldn't understand the documentation of AWS good.
Edit: I did make it work with the help of Dunedan. Here is the working code,
var params = {
TableName: 'feeds',
IndexName: 'category-index',
KeyConditionExpression: 'category = :category',
ExpressionAttributeValues: {
':category': 'backup',
}
};
var docClient = new AWS.DynamoDB.DocumentClient();
docClient.query(params, function(err, data) {
if (err) callback(err);
else callback(null, data);
});
If your application will regularly query for the category, you should check out Global Secondary Indexes (GSI), which allow you to generate a projection of your data with another key than the original hash key as the key you can use to query.
Scanning and filtering as you suggested doesn't scale very well, as it fetches all data in the table and just filters the results.
I am trying to create a silent auction site. My Lambda function should update an item under the condition that the submitted bid > current high bid. However, the condition does not seem to be applying; the update occurs even if the submitted bid is less than the current high bid.
Please help me identify what I'm doing wrong:
##My DynamoDB table looks like this:##
Table name: SilentAuction2
Primary partition key: ItemID (String)
other attributes:
CurrentHighBid (Number)
HighestBidder (String)
Notes (String)
BidHistoryBids (List)
BidHistoryPPL (List)
My condition, as stated above, is that the incoming event.NewBid is greater than the CurrentHighBid. Here is the Lambda code in Node.JS:
const AWS = require('aws-sdk');
const docClient = new AWS.DynamoDB.DocumentClient({region: 'us-east-1'});
exports.handler = function index(event, context, callback) {
var table = "SilentAuction2";
var ItemID = event.ItemID;
var NewBid = event.NewBid;
var NewBidder = event.NewBidder;
var Notes = event.Notes;
var params = {
TableName:table,
Key:{
"ItemID": ItemID
},
//update and append values
UpdateExpression: "SET #BidList = list_append(#BidList, :hb),
#PplList = list_append(#PplList, :hp),
CurrentHighBid =:c,
HighestBidder=:h,
Notes=:n",
//condition on which to update if true
ConditionalExpression: " :c > CurrentHighBid",
ExpressionAttributeNames: {
"#BidList" : "BidHistoryBids",
"#PplList" : "BidHistoryPPL"
},
ExpressionAttributeValues:{
":hb":[NewBid],
":hp":[NewBidder],
":n":Notes,
":c":NewBid,
":h":NewBidder
},
ReturnValues:"UPDATED_NEW"
};
docClient.update(params, function(err, data){
if(err){
callback(err, null);
}else{
callback(null, data);
}
});
};
This links were helpful in researching ConditionalExpression within DynamoDB:
https://egkatzioura.com/2016/08/09/update-dynamodb-items-with-node-js/
http://docs.aws.amazon.com/amazondynamodb/latest/gettingstartedguide/GettingStarted.NodeJs.03.html#GettingStarted.NodeJs.03.05
I am facing some issue with querying data from DynamoDB can any one of you help me on this? I am coding in NodeJS.
My table looks like below with,
Primary key: RequestId
Secondary index: Userid with sortkey Timestamp
When I am pulling the data using UserId, I am getting lot's for records so, planning to pull the data with Timestamp condition.
RequestId Request Timestamp UserId
var doc = require("dynamodb-doc");
var dynamo = new doc.DynamoDB();
var userid_col = "amzn1.ask.account.AGCAPY7JBHQWHTNGAHJJ";
var databaserec = { TableName: "dna_cdknow_prod_historylog",
IndexName: "UserId-TimeStamp-index",
KeyConditionExpression: "UserId = :input",
FilterExpression : 'created between :val1 and :val2',
ExpressionAttributeValues:{ ":input": userid_col, ":val1" : "2016-05-23T00:00:00Z", ":val2" : "2017-05-23T16:20:49Z" } };
The below code should work if the GSI definition is as follows:-
UserId - Partition key of GSI
created - Sort key of GSI
Corrected code:-
var databaserec = {
TableName: "dna_cdknow_prod_historylog",
IndexName: "UserId-TimeStamp-index",
KeyConditionExpression: "UserId = :input AND created between :val1 and :val2'",
ExpressionAttributeValues:{ ":input": userid_col, ":val1" : "2016-05-23T00:00:00Z", ":val2" : "2017-05-23T16:20:49Z" }
};
docClient.query(databaserec, function(err, data) {
if (err) {
console.error("Unable to query. Error:", JSON.stringify(err, null, 2));
} else {
console.log(params);
console.log("Query succeeded.");
data.Items.forEach(function(item) {
console.log("Item :" + JSON.stringify(item));
});
}
});
I am trying to use Dynamodb on a new project and i have to say i am very confused.
So i have setup a new table with a hash key called ID which i am setting as a timestamp as it needs to be unique.
I am then inserting my data as follow with Lambda
var tableName = "TrackerTable";
var datetime = new Date().getTime().toString();
var putData = {
"TableName": tableName,
"Item": {
"ID" : {
"N": datetime
},
"TrackIt": {
"S": event.trackit
},
"Latitude": {
"N": event.lat
},
"Longitude": {
"N": event.lon
},
"Time": {
"N": datetime
}
}
}
dynamoDB.putItem(putData, function(err, data) {
if (err) {
context.fail('ERROR: Dynamo failed: ' + err);
} else {
console.log('Dynamo Success: ' + JSON.stringify(data, null, ' '));
context.succeed('SUCCESS');
}
});
So my table starts to look like this which is fine.
So i am trying to use Lambda to return my results based on the trackit column?
I have the following code which doesnt work.
var tableName = "TrackerTable";
var datetime = new Date().getTime().toString();
var queryData = {
"TableName": tableName,
"ConsistentRead": true,
"KeyConditionExpression": "trackit = :val",
"ExpressionAttributeValues": {":val": {"S": "05LUGFr7494"}}
};
dynamoDB.query(queryData, function(err, data) {
if (err) {
context.fail('ERROR: Dynamo failed: ' + err);
}else{
console.log('Dynamo Success: ' + JSON.stringify(data, null, ' '));
context.done(null, data.Items);
}
});
I get this error.
{
"errorMessage": "ERROR: Dynamo failed: ValidationException: Query condition missed key schema element: ID"
}
How can i query all the trackit values i cant add the ID value as it will only return one value?
Very confused.
The way you have your table setup I think you'll need to do a scan here instead of a query[1]. If this is an operation that your will be doing frequently and want to have the ability to do a query, you would need to add the trackit property as one of your secondary indexes.
[1] http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/QueryAndScan.html