Tech Talk
by Engineers of Acroquest Myanmar Technology
What is DynamoDB and guide to set up local DynamoDB
Go to English summary >>
အားလုံးပဲ မင်္ဂလာပါ။
ကျွန်မနာမည်ကတော့ နှင်းနှင်းတိုးပါ။ Acroquest Myanmar Technologyမှာ Senior Software Engineerတစ်ယောက်အနေနဲ့ အလုပ်လုပ်ကိုင်နေပါတယ်။
အခုAmazon Web Services (AWS)ရဲ့ serviceတစ်ခုဖြစ်တဲ့ DynamoDBအကြောင်း ပြောပြပေးချင်ပါတယ်။ ဒီtopicလေးက DynamoDBကို စတင်လေ့လာနေတဲ့သူ၊ ပြီးတော့ AWS accountမရှိသေးပဲ DynamoDBကို localမှာသုံးချင်နေတဲ့သူတွေ ဒါမှမဟုတ် productionမတိုင်ခင် localမှာအရင်စမ်းသုံးချင်နေတဲ့သူတွေအတွက် ရည်ရွယ်ပါတယ်။

အရင်ဆုံး AWS အကြောင်းလေးကို မိတ်ဆက်ပေးပါမယ်။
◆ AWSက ဘာလဲ?
တော်တော်များများသိကြတဲ့အတိုင်း IT infrastructure မှာ Cloud Computingက တော်တော်လေး ကျော်ကြား အသုံးများလျက်ရှိပါတယ်။ AWSက လူသိအများဆုံး cloud service providerဖြစ်ပါတယ်။ ၂၀၀၂ခုနှစ်မှာ စတင်မိတ်ဆက်ခဲ့ပြီး အခုလက်ရှိမှာတော့ serviceပေါင်း 165ခုကို ထောက်ပံ့ပေးနေပြီး အသုံးပြုသူ သန်းပေါင်းများစွာရှိပါတယ်။ AWSကို စတင်အသုံးပြုမယ်ဆိုရင်တော့ AWS accountပြုလုပ်ရမှာဖြစ်ပြီး ကိုယ်ရဲ့ အသုံးပြုမှုအတိုင်း ကုန်ကျစားရိတ်ရှိမှာဖြစ်ပါတယ်။
ကိုယ်က အခုမှစလေ့လာတာ၊ ဒါမှမဟုတ် productionမတိုင်ခင် ကုန်ကျစားရိတ် လျော့ချချင်ရင်တော့ DynamoDBအပါအ၀င် တစ်ချို့serviceတွေက localမှာအရင်စမ်းသုံးလို့ရပါတယ်။ အခုလည်း DynamoDB local set upလုပ်ဖို့ပြောပြပေးသွားမှာပါ။
ကျွန်မတို့ Acroquestမှာလည်း project requirements ပေါ်မူတည်ပြီး AWS serviceအမျိုးမျိူးကို အသုံးပြုနေပါတယ်။ ဒါ့အပြင် Head office ဖြစ်တဲ့ Acroquest Technology Co.Ltd.က “AWS Machine Learning Competency” partnerအဖြစ် AWSက ၂၀၂၁ အောက်တိုဘာလမှာ အသိအမှတ်ပြုခဲ့ပါတယ်။
အသေးစိတ်ကို ဒီAWS Partner – Acroquest Myanmar Technology (acromyanmar.com)ကနေဖတ်နိုင်ပါတယ်။
◆ DyanmoDBက ဘာလဲ?
DynamoDBက
- key-value NoSQL Database
- auto-scalingကိုထောက်ပံ့ပေးသော
- dataတွေကို အသွင်း၊အထုတ်လုပ်တဲ့အခါlatencyနည်းသော
- Serverless databaseဖြစ်တာနဲ့အညီတစ်ခြားသော hardwareတွေ၊ Serverတွေကို တပ်ဆင်၊စီမံ၊ခန့်ခွဲစရာမလိုသော
- availability နဲ့ Partition Toleranceကို ပြေလည်စေသော
- up-to-date dataတွေကို returnပြန်ပေးနိုင်သော(strongly consistence read if you pay more)
databaseဖြစ်ပါတယ်။
အသေးစိတ်သိချင်ရင်တော့ ဒီ link ကနေဖတ်ကြည့်နိုင်ပါတယ်။
◆ ဘယ်အချိန်မှာသုံးမလဲ?
Big data processing applicationsတွေလို့ပြောနေကြတဲ့ သန်းပေါင်းများစွာသော dataတွေနဲ့ manipulate လုပ်ရတဲ့အခါမျိုးမှာ DynamoDB ကိုသုံးသင့်ပါတယ်။ဘာကြောင့်လဲဆိုတော့ သူ့ရဲ့ auto-scaling၊ dataများစွာနဲ့ အလုပ်လုပ်တဲ့အခါlow latencyဖြစ်စေတဲ့featuresတွေကြောင့်ဖြစ်ပါတယ်။ DynamoDB က NoSQL အမျိုးအစားဖြစ်လို့ relation ရှိနေတဲ့ dataတွေနဲ့ manipulate လုပ်ရတဲ့ projectတွေ၊ dataတွေ၊ tableတွေကို joinပြီး ရှုပ်ထွေးတဲ့ queryသုံးရတဲ့ project တွေအတွက်ဆိုရင်တော့ မသုံးသင့်ပါဘူး။
◆ ဘယ်လိုအလုပ်လုပ်လဲ?
DynamoDB က SQL database(eg-MySQL)တို့နဲ့မတူပဲ databaseဆောက်စရာမလိုပါဘူး။ ကိုယ်လိုချင်တဲ့ tableကိုဆောက်လုပ်နိုင်ပါတယ်။ ပြီးတော့ schemalessဆိုတဲ့အတိုင်း dataတစ်ခုနဲ့တစ်ခုမှာလည်း မတူညီတဲ့ dataပုံစံရှိနိုင်ပါတယ်။
DynamoDBရဲ့ အခြေခံကျပြီးအသုံး၀င်တဲ့ operationတစ်ချို့ကို စလေ့လာကြည့်ရအောင်။ ဒီbasic operation တွေနဲ့ နောက်မှာပြောပြမယ့် APIလေးတွေကိုနားလည်ရင် DynamoDB မှာ CRUD operationလုပ်နိုင်ပါလိမ့်မယ်။
Table: data collection တွေကိုသိမ်းသော အရာတစ်ခုဖြစ်ပါတယ်။
Items: attributes တွေစုထားသော အစုအဖွဲ့တစ်ခုဖြစ်ပြီး တစ်ခြားitemတွေကြားထဲ ခွဲခြားနိုင်တဲ့အရာဖြစ်ပါတယ်။
Attributes: attributeတစ်ခုက data element တစ်ခုဖြစ်ပါတယ်။ attribute တွေပေါင်းထားတဲ့အခါ itemဖြစ်လာပါတယ်။
Primary key
Primary keyဆိုတာက itemတွေအများကြီးထဲမှာ itemတစ်ခုချင်းစီကိုခွဲခြားပေးနိုင်တဲ keyဖြစ်ပါတယ်။ RDBMSမှာဆို ခွဲခြားနိုင်တဲ့ idကို primary keyအဖြစ်သတ်မှတ်လေ့ရှိပါတယ်။ DynamoDBမှာဆိုရင် partition key (hash key)ဒါမှမဟုတ် partition key (hash key) နဲ့ sort key (range key) တွေကိုပေါင်းပြီး primary keyအဖြစ်သတ်မှတ်ပါတယ်။ tableတစ်ခု ဆောက်တာနဲ့ primary keyကို သတ်မှတ်ရမှာဖြစ်ပါတယ်။
Partition key (hash key): DynamoDBက itemတွေသိမ်းမယ့်နေရာကို ဆုံးဖြတ်ပေးတဲ့hash function [https://en.wikipedia.org/wiki/Hash_function]ရဲ့ input အဖြစ်သုံးမယ့် attribute တစ်ခုဖြစ်ပါတယ်။ partition keyက itemတစ်ခုချင်းစီအတွက် uniqueဖြစ်တဲ့ attribute ဖြစ်ပါတယ်။
Partition key (hash key) and Sort key (range key): partition keyနဲ့ sort keyတွေကို ပေါင်းထားလို့ Composite primary keyလို့ခေါ်ပါတယ်။ ဒီ keyနဲ့ဆိုရင် itemတွေကို တူညီတဲ့ partitionမှာ မတူညီတဲ့ sort keyနဲ့ သိမ်းနိုင်ပါတယ်။
Secondary indexes
DynamoDB က တစ်ခြားdatabaseတွေနဲ့မတူတဲ့ secondary indexes ကိုထောက်ပံ့ပေးနိုင်ပါတယ်။ secondar y indexဆိုတာက tableအစား dataတွေကိုqueryနဲ့ဆွဲထုတ်ပေးနိုင်တဲ အရာတစ်ခုဖြစ်ပါတယ်။ မြင်သာအောင်ပြောရရင် tableကိုqueryလုပ်မယ်ဆိုရင် primary keyနဲ့တစ်ခြားလိုတဲ့အရာတွေ နဲ့ filterလုပ်ပါတယ်။ ဒါပေမယ့် တစ်ခါတစ်လေ ကိုယ်queryလုပ်ချင်တဲ့attributeက primary keyမဟုတ်တဲ့အခါ queryလုပ်လို့မရနိုင်တော့ပါဘူး။ အဲ့အခါ secondary indexes တွေကိုtableအစား အသုံးပြုနိုင်ပါတယ်။
Secondary indexes နှစ်မျိုးရှိပါတယ်။
- Global secondary index (GSI) – GSI ရဲ့ primary keyတွေ sort keyတွေက tableနဲ့မတူညီတဲ့ attribute တွေဖြစ်ပါတယ်။
- Local secondary index (LSI) – LSIမှာဆိုရင် partition keyက tableနဲ့တူပြီး sort keyက မတူညီတဲ့ attribute ဖြစ်ပါတယ်။
Secondary indexes အကြောင်း အသေးစိတ်ကို အောက်ပါlink ကနေဖတ်နိုင်ပါတယ်။
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/bp-indexes-general.html
အခုကတော့ developerတွေအသုံးများတဲ့၊ application တွေရေးရင် မရှိမဖြစ်လိုတဲ့ DynamoDB ရဲ့ CRUD API လေးတွေပြောပြပေးပါမယ်။
Category | Operation | Description |
---|---|---|
Put Data | PutItem | Itemတစ်ခုထည့်ခြင်း။(Primary key attributes must be specified.) |
BatchWriteItem | Itemတွေအများကြီးတစ်ခါတည်းထည့်ခြင်း(up to 25) ★ Itemတွေအများကြီးဖျက်ဖို့ အသုံးပြုနိုင်ပါတယ်။ | |
Retrieve data | GetItem | သတ်မှတ်ထားသော primary keyဖြင့်Itemတစ်ခုဖတ်ခြင်း။ |
BatchGetItem | Itemတွေအများကြီးတစ်ခါတည်းဖတ်ခြင်း(up to 100) | |
Query | Itemတွေကိုသတ်မှတ်ထားတဲ့partition keyနဲ့ ဆွဲထုတ်ခြင်း။ | |
Scan | table ဒါမှမဟုတ် indexကနေitemအားလုံးဆွဲထုတ်ခြင်း။ | |
Update data | UpdateItem | သတ်မှတ်ထားသော primary keyဖြင့်Itemတစ်ခု updateလုပ်ခြင်း။ |
Delete data | DeleteItem | သတ်မှတ်ထားသော primary keyဖြင့်Itemတစ်ခု ဖျက်ခြင်း။ |
APIတစ်ခုချင်းစီမှာတော့ အားသာချက်၊အားနည်းချက်ကလေးတွေရှိပါတယ်။ ကျွန်မရဲ့အတွေ့အကြုံအရဆိုရင် scanကိုသုံးတဲ့အခါမှာ အများကြီးသတိထားဖို့လိုပါတယ်။ scanက tableကdataအားလုံးကိုဖတ်တာဖြစ်တာကြောင့် dataများရင်များသလောက် costကလည်းများမှာဖြစ်ပါတယ်။ scanအစား secondary indexes ( GSI, LSI)ကို ကိုယ့်Projectလိုအပ်ချက်အတိုင်းသတ်မှတ်ပြီးသုံးနိုင်ပါတယ်။အဲ့တာဆို ကုန်ကျစားရိတ်သက်သာစေနိုင်မှာပါ။
အခုလောက်ဆို အခြေခံအချက်လေးတွေသိပြီဆိုတော့ DynamoDBကိုအတော်အသင့်နားလည်ပြီထင်ပါတယ်။ local set upလေးလုပ်ပြီး practiceလေးလုပ်ကြရအောင်။
◆ Local DynamoDB set up
■ Prerequisites
- Java Runtime Environment (JRE) version 8.x နှင့်အထက်
- AWS CLI
(set upမလုပ်ရသေးရင် ဒီ link ကအတိုင်း လုပ်နိုင်ပါတယ်။ )
■ Local set up
- zip file ကို Download လုပ်ပါ။
- Download လုပ်ထားတဲ့ zip fileကို unzipလုပ်ပါ။
- Unzip လုပ်ထားတဲ့နေရာကို သွားပါမယ်။ Windowရဲ့ command promptအစား၊ Git Bashကို အသုံးပြုမှာဖြစ်လို့ Gitမရှိသေးရင် installလုပ်ရပါမယ်။
- DynamoDB ကိုlocalမှာစဖို့ အောက်ပါ command ကိုrunပါ။
java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar –sharedDb

5. AWS CLIကိုသုံးပြီး AWS credentials ကို set upလုပ်ပါ။ ဒီနေရာမှာ AWS account မရှိသေးလို့ fake credentials ကိုသုံးပါမယ်။
AWS Access Key ID: "testLocalDynamoDB" AWS Secret Access Key: "testLocalDynamoDBSecretKey”

အခုဆိုDynamoDBကိုlocalမှာသုံးဖို့အတွက် အဆင်သင့်ဖြစ်နေပါပြီ။
အောက်ကပေးထားတဲ့tableအတွက်operationတစ်ချို့လုပ်ကြည့်ရအောင်။
No | Field Name | Column Name | Data Type | PK(Partition Key) | SK(Sort Key) | Description |
---|---|---|---|---|---|---|
1 | Staff number | staff_number | Number | 〇 | employee number | |
2 | Employee name | Employee name | name | String | 〇 GSI 1 | employee name |
3 | Gender | gender | String | Gender | ||
4 | Office branch | office_branch | bool | 〇 GSI 1 | Employees are from head office or branch office. True: head office False: branch office | |
5 | Position | position | String | Current position | ||
6 | Department | department | String | Current working department | ||
7 | Entering date | entering_date | String | Entering date to company | ||
8 | Resigned date | resigned_date | String | Resigned date from company |
အရင်ဆုံးtable တွေlistလုပ်ကြည့်မယ်။ createလုပ်ပြီးသားtableကမရှိသေးလို့ empty listပဲထွက်လာမယ်။
aws dynamodb list-tables --endpoint-url http://localhost:8000

employees tableကို createလုပ်မယ်။
aws dynamodb create-table \ --table-name employees \ --attribute-definitions AttributeName=staff_number,AttributeType=S AttributeName=name,AttributeType=S \ --key-schema AttributeName=staff_number,KeyType=HASH AttributeName=name,KeyType=RANGE \ --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 \ --endpoint-url http://localhost:8000

ပြီးတော့ required fieldsနှစ်ခုပဲပါတဲ့itemကိုထည့်မယ်။ ပြီးရင်ပြန်ဆွဲထုတ်ကြည့်မယ်။
aws dynamodb put-item \ --table-name employees \ --item '{"staff_number": {"S": "s000"}, "name": {"S": "Micheal"} }' \ --endpoint-url http://localhost:8000 aws dynamodb get-item --consistent-read \ --table-name employees \ --key '{"staff_number": {"S": "s000"}, "name": {"S": "Micheal"}}' \ --endpoint-url http://localhost:8000

Updateလုပ်မယ်။
aws dynamodb update-item \ --table-name employees \ --key '{"staff_number": {"S": "s000"}, "name": {"S": "Micheal"}}' \ --update-expression "SET office_branch = :ob, current_position = :po" \ --expression-attribute-values '{":ob" : {"BOOL": true}, ":po" : {"S": "CTO"}}' \ --return-values ALL_NEW \ --endpoint-url http://localhost:8000

Itemကိုဖျက်မယ်။
aws dynamodb delete-item \ --table-name employees \ --key '{"staff_number": {"S": "S002"}, "name": {"S": "John Smith"}}' \ --endpoint-url http://localhost:8000

Tableကိုဖျက်မယ်။
aws dynamodb delete-table --table-name employees --endpoint-url http://localhost:8000

အခုလောက်ဆို DynamoDBအကြောင်းနဲ့ အလုပ်လုပ်ပုံတွေကိုသိလောက်ပြီထင်ပါတယ်။
နောက်ထပ်လေ့လာချင်တယ်ဆိုရင် ဒီ Hands-onလေးလိုက်လုပ်ကြည့်လို့ရပါတယ်။
https://amazon-dynamodb-labs.com/hands-on-labs.html
နောက်လာမယ့်sharing တွေအတွက်လည်း STAY TUNEDပါ။
Hello, everyone. I am Hnin Hnin Toe. I am working as the Senior Software Engineer in Acroquest Myanmar Technology. Now, I would like to share about DynamoDB, one of the Amazon Web Services (AWS). If you are a beginner to AWS DyanmoDB and also you don’t have AWS account or want to save cost before production, this topic will be useful for you.
Firstly, I will share briefly about AWS.
■ What is AWS?
Nowadays, Cloud computing has become very popular in IT. Amazon Web Service (AWS) is the most popular cloud service provider in the world. It was launched in 2002. Now, it is offering over 165 fully-featured services and it is used by millions of customers. To be able to use AWS, we need to create AWS account.
Of course, we, Acroquest, also using AWS in our project and I also have the experience of using some services (like DynamoDB, S3, StepFunctions , …) based on the requirement of each PJ. Moreover, Acroquest Technology Co., Ltd. (Headquarters: Yokohama, Kanagawa.) has received certificate for “AWS Machine Learning Competency” partners from Amazon Web Services in October 2021.
You can read details from here.
■ What is Amazon DynamoDB?
- key-value NoSQL database (use JSON)
- provide auto-scaling
- low latency of data acquisition
- serverless database, not need to manage servers or hardware setup and configuration
- satisfy “Availability” and “Partition Tolerance”
- consistent Reads (strongly if you pay more)
For more details, you can read the official documentation. https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Introduction.html
■ When to use?
It is useful for big data processing, applications requiring large amounts of data processing, etc. In Big Data applications, we need to manipulate multiple gigabytes or terabytes of information. Because of its features like auto-scaling, low latency of data acquisition, we can develop without degrading the performance. As DynamoDB is NoSQL, we should avoid using for project which is manipulation on relational data and using complex queries like joining data and tables.
■ How it works?
Different from RDBMS (e.g., MySQL), DynamoDB doesn’t require to create a DB. Each data from table can have different structure as it is schemaless.
The following are some components of DynamoDB. If you understand how to use those components, you can do basic operations (CRUD) efficiently.
Table: to store the collection of data.
Items: An item is a group of attributes that is uniquely identifiable among all of the other items.
Attributes: An attribute is a data element. One or more attributes combined as an item.
Primary key
Primary key is the unique, identifiable key of item among all items. In RDBMS, we used to define primary key as id. In DynamoDB, we can define by using primary key (hash key) or the combination of partition key (hash key) and sort key (range key). We must define the primary key when we create a table.
Partition key (hash key): The partition key is an attribute that DynamoDB will use as the input of an internal hash function [https://en.wikipedia.org/wiki/Hash_function), which determines the partition to store the item. The partition key must be unique for each item.
Partition key (hash key) and Sort key (range key): It is composite primary key which is the combination of partition key and sort key. With this type of key, items will be stored with the same partition but different sort key value.
Partition key (hash key): One main attribute which is unique. No duplication.
Sort key (range key): Combination of two attributes. When you create a table, declare one attributes as hash key and other as range key.
Secondary indexes
DynamoDB supports secondary indexes. A secondary index is used to query the data in the table using an alternate key, in addition to query against the primary key. You can use indexes to be more flexible in querying data in your application. With secondary index, you can read data from index as the same way to do from table.
DynamoDB supports two kinds of indexes:
- Global secondary index (GSI) – An index with a partition key and sort key that can be different from those on the table.
- Local secondary index (LSI) – An index that has the same partition key as the table, but a different sort key.
I think you understand some useful components of DynamoDB. I will share some API which developers are mainly concerned about.
Category | Operation | Description |
---|---|---|
Put Data | PutItem | Insert an item to table. Primary key attributes must be specified. |
BatchWriteItem | Write multiple items (up to 25) at once ★ can be used to delete multiple items. | |
Retrieve data | GetItem | Retrieve an item from table by specified primary key |
BatchGetItem | Retrieve multiple items (up to 100) at once | |
Query | Retrieve all items for specified partition key. | |
Scan | Retrieve all items from the specified table or index | |
Update data | UpdateItem | Update one or more attributes of item. Primary key must be specified. |
Delete data | DeleteItem | Delete an item of specified primary key. |
Each API has pros and cons. According to my experience, you need to be careful for using Scan. As it is scanning all data from the table or index, there can be high cost. If possible, you better avoid using scan. Instead of it, you can consider using GSI or LSI based on your project requirements.
I hope you understand about DynamoDB by practicing locally and can apply in your projects.
STAY TUNED for future post ^^)
★★★We are hiring the staff who are interested in latest technologies.★★★
If you are interested in our company, please see the available job descriptions in the following links.
Senior Software Developer: https://www.acromyanmar.com/senior-software-developer/
Intermediate Software Developer: https://www.acromyanmar.com/intermediate-software-developer/