November 29, 2020
Using DynamoDb in Coldfusion 2021
Comments
(1)
November 29, 2020
Using DynamoDb in Coldfusion 2021
Newbie 1 posts
Followers: 0 people
(1)

Hello guys

Here we will be learning how to get started with AWS DynamoDb using Coldfusion.

So, what is dynamodb after all?

Amazon DynamoDB is a key-value and document database. It’s a fully managed, multi region, durable database with built-in security, backup and restore. DynamoDB may look very similar to JSON document, with one difference being that each record must include the key attribute. It has the advantage that it lets us do updates on a record. In a JSON database, like MongoDB, we cannot update records. Instead we must delete them and then add back the changed version to effect the same change.

DynamoDB also lets you work with transactions. Not all NoSQL databases supports transactions. This is important as certain database operations logically must happen completely or get reverted.

So, without wasting much time let’s get started.

Installing DynamoDb Module

Before we can start using dyamodb in our code, we need to install this module. Installation is very simple and requires only few steps:

  1. Go to the directory: cfusion/bin/
  2. Run the command cfpm from the command prompt
  3. Use the list command to check which modules are currently installed
  4. Run install awsdynamodb to install the dynamodb module if its currently not installed
  5. Once the module gets installed, it will be available for use.

Creating a Table

Creating a table is very simple. We need the AWS credentials in order to access AWS services. Consider the following code snippet:

cred = {
“alias” : “MyCredential”,
“vendorName” : “AWS”,
“region” : “ap-southeast-2”,
“secretAccessKey” : “xxxxxxxxxxxxxxxxxxx”,
“accessKeyId” : ” xxxxxxxxxxxxxxxxxxx”

}

Next, we need to set the config object with DYANMODB in order to tell ColdFusion that we wish to use dynamodb service.

config = {
“serviceName” : “DYNAMODB”
};

Using the above two we can get the CF dynamo service handle as follows:

dynamo = getCloudService(cred, config);

At this point we are ready to call the createTable API to create the table. We provide the table details call the API as follows:

<cfscript>
cred = {
“alias” : “MyCredential”,
“vendorName” : “AWS”,
“region” : “ap-southeast-2”,
“secretAccessKey” : “xxxxxxxxxxxxx”,
“accessKeyId” : “xxxxxxxxxxxx”
}
config = {
“serviceName” : “DYNAMODB”
};
dynamo = getCloudService(cred, config);
strct = {
“TableName” : “Student”,
“KeySchema”:[
{
“AttributeName”: “id”,
“KeyType”: “HASH”
},
{
“AttributeName”: “name”,
“KeyType”: “RANGE”
}
],
“AttributeDefinitions”:[
{
“AttributeName”: “name”,
“AttributeType”: “S”
},
{
“AttributeName”: “id”,
“AttributeType”: “N”
}
],
“ProvisionedThroughput”: {
“ReadCapacityUnits”: 10,
“WriteCapacityUnits”: 10
}
};
try{
//Create a new table
result = dynamo.createTable(strct);
writedump(result);
}
catch(any exception){
writedump(exception);
}
</cfscript>

One read request unit (ReadCapacityUnit) represents one strongly consistent read request, or two eventually consistent read requests, for an item up to 4 KB in size. Two read request units represent one transactional read for items up to 4 KB. If you need to read an item that is larger than 4 KB, DynamoDB needs additional read request units.

Similarly, one write request unit (WriteCapacityUnits) represents one write for an item up to 1 KB in size. If you need to write an item that is larger than 1 KB, DynamoDB needs to consume additional write request units.

Deleting a Table

Deleting a table is very simple. Just call the deleteTable API and pass the table name to be deleted.

See the following example:

strct = {
“TableName”: “Student”
}
//Delete the given table
dynamo.deleteTable(strct);

Note: In the above example and from now on, the creation dynamodb handle will not be included as it seems redundant. You can refer the earlier section of this blog to see how to build dynamodb handle using credential and the config object.

Getting a List of all existing Tables

Often, we may want to know list all the existing tables in a specific aws region. Use the listTables API to view this info.

try{
//List all the tables present in this AWS region
result = dynamo.listTables({});
writedump(result.TableNames);
} catch(any e){
writeDump(e);
}

Inserting an item into the table

We can use putItem API to insert a single item into the dynamodb table. The “Item” attribute must include the partition and the sort key. Apart from this any other key value pair can be added in the item.

Following is an example:

strct = {
“TableName”:”Student”,
“Item”:{
“name”: “Mohan Roy”,
“id”: 44111,
“class”: 9,
“subjects”: [“Maths”, “Science”, “History”, “Geography”]
}
};
try{
//Insert this item into the given table
result = dynamo.putItem(strct);
writedump(result);
} catch(any e){
writeDump(e);
}

Fetching a single item from the table

We use getItem API to retrieve a single item from the DynamoDB table. Consider the following example:

strct = {
“TableName”:”Student”,
“Key”:{
“name”: “Rohan Jain”,
“id”: 44111
}
}
try{
//Fetch the item from the given table
result = dynamo.getItem(strct, {
“customResponse”: true
});
writedump(result.Item);
} catch(any e){
writeDump(e);
}

Updating an item from the table

We use the updateItem API to update a single item in the dynamo table. The updateItem requires the attribute UpdateExpression where we specify the update logic & which part of document needs to be updated. We can also optionally set “ReturnValues” attribute to “ALL_OLD”. This will return the old record in the response which was updated by updateItem API.

strct = {
“TableName”:”Student”,
“Key”:{
“name”: “Rohan Jain”,
“id”: 44111
},
“ExpressionAttributeNames”:{
“%classAttr”: “class”
},
“ExpressionAttributeValues”:{
“:newClass”: 11
},
“UpdateExpression”: “set %classAttr = :newClass”,
“ReturnValues”:”ALL_OLD”
};
try{
//Update the item corresponding to the given condition
result = dynamo.updateItem(strct, {
“customResponse”: true
});
writedump(result.Attributes);
}catch(any e){
writeDump(e);
}
 

Deleting an item from the table

We use deleteItem API to delete an item from the table. We can set the attribute ReturnValues to ALL_OLD to get the deleted item in response. See the following code snippet:

strct = {
“TableName”:”Student-99″,
“Key”:{
“class”: 9,
“name”: “Rohan Roy”
},
“ReturnValues”:”ALL_OLD”
};
try{
//Delete the given item from the table and return the deleted item
result = dynamo.deleteItem(strct,{
“customResponse”: true
});
writedump(result);
} catch(any e){
writeDump(e);
}
 

Searching multiple items from the table

In order to search multiple items from the table we use the query API. The following code searches for all the items in the Student table whose id is equal to 44111.

strct= {
“TableName”:”Student”,
“KeyConditionExpression”: “%idKey = :idVal”,
“ExpressionAttributeNames”:{
“%idKey”: “id”
},
“ExpressionAttributeValues”:{
“:idVal”: 44111
}
};
try{
//Fetch the item/items corresponding to the query from the given table
result = dynamo.query(strct, {
“customResponse”: true
});
writedump(result.Items);
} catch(any e){
writeDump(e);
}

Limiting our search query

In order to limit the number of results in our query, we can use the “Limit” attribute to limit the number of results returned by dynamodb.

strct= {
“TableName”:”Student”,
“KeyConditionExpression”: “%idKey = :idVal”,
“ExpressionAttributeNames”:{
“%idKey”: “id”
},
“ExpressionAttributeValues”:{
“:idVal”: 44111
},
“Limit”: 2
};
try{
//Fetch the item/items corresponding to the query from the given table
result = dynamo.query(strct, {
“customResponse”: true
});
writedump(result.Items);
} catch(any e){
writeDump(e)
}

Scan API

Unlike query API, the Scan API reads every item in a table. By default, a Scan operation returns all the data attributes for every item in the table. We can use the ProjectionExpression parameter so that Scan only returns some of the attributes, rather than all of them. Scan always returns a result set. If no matching items are found, the result set is empty.

Following is a simple example which uses scan API to retrieve all items along with their attributes “id” and “subjects” only:

strct= {
“TableName”:”Student”,
“ProjectionExpression” : “id, subjects”,
“Limit”: 20
};
try{
//Fetch the item/items corresponding to the filter condition from the given table
result = dynamo.scan(strct, {
“customResponse”: true
});
writedump(result.Items);
} catch(any e){
writeDump(e);
}

Paginating the results

DynamoDB paginates the results from the Scan API. With pagination, the Scan results are divided into “pages” of data that are 1 MB in size (or less). We can process the first page of results, then the second page, and so on. A single Scan only returns a result set that fits within the 1 MB size limit. To determine whether there are more results and whether we need to retrieve them, one page at a time, we should do the following:

1)      Examine the Scan response.

a.       If the result contains a LastEvaluatedKey element, proceed to step 2.

b.       If there is not a LastEvaluatedKey in the result, then there are no more items to be retrieved.

2)      Construct a new Scan request, with the same parameters as the previous one. However, this time, take the LastEvaluatedKey value from step 1 and use it as the ExclusiveStartKey parameter in the new Scan request.

3)      Run the new Scan request.

4)      Go to step 1.

In other words, the LastEvaluatedKey from a Scan response should be used as the ExclusiveStartKey for the next Scan request. If there is no LastEvaluatedKey element in a Scan response, you have retrieved the final page of results. (The absence of LastEvaluatedKey is the only way to know that you have reached the end of the result set.)

1 Comment
2022-10-31 13:25:54
2022-10-31 13:25:54

How would you go about doing a batch write?

Like
Add Comment