ã¯ããã«
ãã¼ã¿ãã¼ã¹ã§ã¬ã³ã¼ãã管çããã¨ããã¬ã³ã¼ããã¨ã«ã¦ãã¼ã¯ãªãã¼ãè¨å®ãã¦ããããå ´é¢ã¯å¤ã ããã¾ãããã
ä¾ãã°ãMySQL ã®å ´å㯠AUTO_INCREMENT
ã®ãããªèªåã·ã¼ã±ã³ã¹æ©è½ã使ããã¨ã§é£çªãå²ãå½ã¦ã¦ããã¾ãã
ããããDynamoDB ã«ã¯ãã®ãããªæ©è½ã¯ããã¾ããã
ã§ã¯ DynamoDB ã§ã¦ãã¼ã¯ãã¼ãè¨å®ããã«ã¯ã©ãããã°ããã®ã§ããããã
ã¨ãããã¨ã§ãä»åã¯ãã¢ãããã¯ã«ã¦ã³ã¿ã使ã£ãé£çªç®¡çãã¨ãUUID ã使ã£ãä¸æãªãã¼ã®çæãã®2ã¤ã®å
·ä½çãªå®è£
æ¹æ³ããµã³ãã«ã³ã¼ãï¼Pythonï¼ã¨ã¨ãã«ç´¹ä»ãã¾ãã
- ã¯ããã«
- ã¢ãããã¯ã«ã¦ã³ã¿ã«ããé£çªç®¡ç
- UUID ã«ããã¦ãã¼ã¯ãã¼ã®çæ
- ã¾ã¨ã
ã¢ãããã¯ã«ã¦ã³ã¿ã«ããé£çªç®¡ç
DynamoDB ã®ã¢ãããã¯ã«ã¦ã³ã¿ã使ã£ã¦ãé£çªã管çããæ¹æ³ãç´¹ä»ãã¾ãã ã¢ãããã¯ã«ã¦ã³ã¿ã使ããã¨ã§ãåæã«è¤æ°ã®ããã»ã¹ããã¢ã¯ã»ã¹ããã£ã¦ããæ£ç¢ºã«ä¸æãªé£çªãçæã§ãã¾ãã
ã¢ãããã¯ã«ã¦ã³ã¿ã® Python ãµã³ãã«ã³ã¼ã
DynamoDB ã® UpdateItem
æä½ã使ã£ã¦ãã«ã¦ã³ã¿å¤ãã¤ã³ã¯ãªã¡ã³ããã¾ãã
import boto3 table_name = "dynamodb_table_name" counter_key = "counter_key" dynamodb_client = boto3.client("dynamodb") def increment_counter(): # ã«ã¦ã³ã¿ãã¤ã³ã¯ãªã¡ã³ããã¦æ´æ°ãã response = dynamodb_client.update_item( TableName=table_name, Key={ "id": {"S": counter_key} # ã«ã¦ã³ã¿ç¨ã¢ã¤ãã ã®ãã¼ }, UpdateExpression="SET #val = if_not_exists(#val, :start) + :inc", ExpressionAttributeNames={ "#val": "counter_value" # ã«ã¦ã³ã¿å¤ãä¿æãããã£ã¼ã«ã }, ExpressionAttributeValues={ ":inc": {"N": "1"}, # ã¤ã³ã¯ãªã¡ã³ãããå¤ ":start": {"N": "0"} # åæå¤ }, ReturnValues="UPDATED_NEW" ) # æ´æ°å¾ã®ã«ã¦ã³ã¿å¤ãåå¾å¯è½ new_counter_value = response["Attributes"]["counter_value"]["N"] return new_counter_value new_id = increment_counter() print(f"New Counter ID: {new_id}")
UUID ã«ããã¦ãã¼ã¯ãã¼ã®çæ
UUIDï¼Universally Unique Identifierï¼ã¯ãä¸ææ§ãä¿è¨¼ããããã®ã©ã³ãã ãªèå¥åã§ãä¸çä¸ã®ã·ã¹ãã éã§è¡çªããå¯è½æ§ãé常ã«ä½ãããããã¦ãã¼ã¯ãã¼ã¨ãã¦å©ç¨ããã¾ãã Pythonã®æ¨æºã©ã¤ãã©ãªã«ã¯ãUUID ãçæããããã®æ©è½ãå«ã¾ãã¦ãããç¹å¥ãªã¤ã³ã¹ãã¼ã«ã¯å¿ è¦ããã¾ããã
UUID 㯠128 ãããã®èå¥åã§ãé常㯠16 é²æ°è¡¨ç¾ã§è¡¨ããã¾ãã ãããæ°å¤åã«å¤æããããé¨åçã«å©ç¨ãããã¨ã§ãDynamoDB ã«é©ç¨ãããã¨ãå¯è½ã§ãã
UUID ã®åºæ¬çãªçææ¹æ³
Python 㧠UUID ãçæããã«ã¯ãæ¨æºã©ã¤ãã©ãªã® uuid
ã¢ã¸ã¥ã¼ã«ã使ç¨ãã¾ãã
以ä¸ã®ã³ã¼ãã ã¨ã24cc08f8-1f58-4983-8de3-7598ffefa5b1
ã®ãã㪠36 æåã® ID ãçæããã¾ãã
åèï¼https://docs.python.org/ja/3/library/uuid.html
import uuid # UUIDãçæ unique_id = str(uuid.uuid4())
UUID ã® Python ãµã³ãã«ã³ã¼ã
DynamoDB ã®ã«ã©ã ãæ°å¤åã§ããå ´åãUUID ããã®ã¾ã¾å©ç¨ããã®ã§ã¯ãªããæ°å¤ã«å¤æãã¦ä½¿ããã¨ãã§ãã¾ãã
æ°å¤ã«å¤æãã㨠43009895629459701174015780120016914245
ã®ãããªå½¢ã§é·ãæ°ååã«ãªãããã以ä¸ã®ã³ã¼ãã§ã¯æ¡æ°ã 12 æ¡ã«å¶éãã¦ä½¿ãä¾ã示ãã¾ãã
import uuid import boto3 table_name = "your-dynamodb-table" dynamodb_client = boto3.client("dynamodb") def insert_item_to_dynamodb(): # UUIDãçæ unique_id = uuid.uuid4() # ä¾ï¼327925c6-5b1b-4815-9988-8a303e320e4d num_unique_id = unique_id.int % (10**12) # ä¾ï¼468322877005 # æ¿å ¥ãã¼ã¿ item = { "id": {"S": num_unique_id}, "SID": {"S": "Content Cell"}, "lbc": {"S": "Content Cell"} } # DynamoDBã«ãã¼ã¿ãæ¿å ¥ dynamodb_client.put_item(TableName=table_name, Item=item) insert_item_to_dynamodb()
ã¾ã¨ã
æ¬è¨äºã§ã¯ãDynamoDB ã«ãããã¦ãã¼ã¯ãã¼çæã®ããã®2ã¤ã®ã¢ããã¼ããç´¹ä»ãã¾ããã ã¦ã¼ã¹ã±ã¼ã¹ã«å¿ãã¦ãUUID ãã¢ãããã¯ã«ã¦ã³ã¿ã®ã©ã¡ãããã¾ãã¯ä¸¡æ¹ãé¸æãã¦æ´»ç¨ãã¦ãã ããã
é£çªã®ç®¡çãéè¦ã§ãè¤æ°ã®ããã»ã¹ããã®åæã¢ã¯ã»ã¹ãå¤ãå ´åã¯ã¢ãããã¯ã«ã¦ã³ã¿ããã·ã¹ãã å ¨ä½ã§ã¦ãã¼ã¯ãªèå¥åãå¿ è¦ã§ãé£çªã§ããå¿ è¦ããªãå ´å㯠UUID ã¨ããããã«ä½¿ãåããã¨ããããããã¾ãããã
ãã®è¨äºãã©ãªããã®ãå½¹ã«ç«ã¦ãã°å¹¸ãã§ãã
é¦å æå (è¨äºä¸è¦§)
2023年度æ°åå ¥ç¤¾
ã¢ããªã±ã¼ã·ã§ã³ãµã¼ãã¹é¨ãã£ããããã¡ã³ããµã¼ãã¹3課æå±