【问题标题】:AWS Lambda w/ Python UUID on Dynamo DB (Concept)Dynamodb 中带有/Python UUID 的 AWS Lambda(概念)
【发布时间】:2016-02-25 11:13:29
【问题描述】:

这是一个概念问题:

我们想为 Dynamo DB 表创建一个唯一的主键,同时在 AWS Lambda 上运行我们的代码。

如果我们使用 AWS Lambda 上的 Python 内置函数 uuid 为 dynamo DB 数据库创建唯一键,它是否有可能创建双倍键,例如,如果我们有 5-200 亿个项目我们的 dynamodb 数据库。

例如,我知道在正常应用程序中使用双 uuid 密钥的可能性极低,几乎是不可能的。

据我了解,每次运行 uuid 时,它都会通过在内存中保存一些先前的值来确保它不能创建双精度值。

但是我不确定 Lambda 是否只是使用同一个 python 控制台一遍又一遍地运行下面的函数(并保存 uuid 以确保其唯一性),或者它是否正在创建多个单独的控制台实例并分别运行它们(不是保存 uuid 的内存)。

虽然这是一个概念问题,但这里有一些示例代码:

from __future__ import print_function
from decimal import *
import boto3
import json
from locale import str
import uuid

def my_handler(event, context):
    description = event['description'] 
    spot_id = uuid.uuid4() #Unique identifier for spot 
    dynamodb = boto3.client('dynamodb')
    tablesinfo = "sinfo"
    dynamodb.put_item(
    TableName = tablesinfo, Item = {
      'spot_id':{'S' : str(spot_id)},
      'description': {'S' : description}
      }
    )
    return {'spot_id' : spot_id}

【问题讨论】:

  • 您的代码示例不包括您生成 uuid 的部分。
  • @Oin 我更新了代码,我意识到它可能令人困惑,但这主要是一个基于概念的问题
  • 你有这个问题的答案吗?我一次又一次地得到相同的 UUID
  • @Himanshu 不,我没有,但我假设如果您一次又一次地获得相同的 UUID,它必须在每次运行代码时运行新的单独实例,而不是将其保存在内存中

标签: python amazon-web-services amazon-dynamodb aws-lambda


【解决方案1】:

Amazon AWS 有自己的示例,用于在 Lambda 中使用 Python 创建 UUID 并将其存储在 elasticache 中。 Amazon 没有明确表示这将每次都明确地创建一个唯一条目,但您可以将其与检查相结合,以查看生成的 UUID 在插入之前是否已经在 DynamoDB 中。检查 UUID 是否已经存在的缺点是这将使用 DynamoDB 表上的一些读取容量,因此从概念上讲,这对您来说是一种成本。

这是来自 AWS 的代码,如果您调整它以适应 DynamoDB 并检查 UUID 是否已经在表中创建,那么这将起作用:

发件人:Amazon Lambda Documentation - Create a Lambda Function example

from __future__ import print_function
import time
import uuid
import sys
import socket
import elasticache_auto_discovery
from pymemcache.client.hash import HashClient

#elasticache settings
elasticache_config_endpoint = "your-elasticache-cluster-endpoint:port"
nodes = elasticache_auto_discovery.discover(elasticache_config_endpoint)
nodes = map(lambda x: (x[1], int(x[2])), nodes)
memcache_client = HashClient(nodes)

def handler(event, context):
    """
    This function puts into memcache and get from it.
    Memcache is hosted using elasticache
    """

    #Create a random UUID... this will the sample element we add to the cache.
    uuid_inserted = uuid.uuid4().hex
    #Put the UUID to the cache.
    memcache_client.set('uuid', uuid_inserted)
    #Get item (UUID) from the cache.
    uuid_obtained = memcache_client.get('uuid')
    if uuid_obtained == uuid_inserted:
        # this print should go to the CloudWatch Logs and Lambda console.
        print ("Success: Fetched value %s from memcache" %(uuid_inserted))
    else:
        raise Exception("Value is not the same as we put :(. Expected %s got %s" %(uuid_inserted, uuid_obtained))

    return "Fetched value from memcache: " + uuid_obtained

【讨论】:

    猜你喜欢
    • 2021-06-03
    • 2020-11-25
    • 1970-01-01
    • 1970-01-01
    • 2021-04-01
    • 1970-01-01
    • 2020-02-05
    • 2023-01-27
    • 2018-08-03
    相关资源
    最近更新 更多