简体   繁体   English

AWS Lambda w / Dynamo DB上的Python UUID(概念)

[英]AWS Lambda w/ Python UUID on Dynamo DB (Concept)

This is a concept question: 这是一个概念问题:

We would like to create a unique primary key for a Dynamo DB table, while running our code on AWS Lambda. 我们希望在AWS Lambda上运行我们的代码时为Dynamo数据库表创建唯一的主键。

If we use the python built in function uuid on AWS Lambda to create a unique key for a dynamo DB database is there any possibility that it could create a double of the key, if we had for example 5-20 billion items in our dynamodb database. 如果我们在AWS Lambda上使用内置函数uuid来创建一个发电机数据库数据库的唯一密钥,那么它有可能创建密钥的两倍,如果我们的dynamodb数据库中有例如5-20亿个项目。

I know for example that the possibilities in a normal application for a double uuid key is extremely low nearly impossible. 我知道,例如,双uuid键的正常应用中的可能性几乎是不可能的。

As I understand every time uuid runs it makes sure it can't create a double by saving some previous value in memory. 据我所知,每次uuid运行时,都要确保它不能通过在内存中保存一些先前的值来创建double。

However I am unsure if Lambda is just running the function below over and over with the same python console (and saving the uuid to make sure its unique) or if it is creating multiple separate console instances and running them through separately (not saving the memory of uuid). 但是我不确定Lambda是否只是使用相同的python控制台反复运行下面的函数(并保存uuid以确保其唯一)或者是否创建多个单独的控制台实例并单独运行它们(不保存内存) uuid)。

Although this is a concept question here is some sample code: 虽然这是一个概念问题,但这里有一些示例代码:

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}

Amazon AWS have their own example for creating a UUID using Python in Lambda and storing this in elasticache. Amazon AWS有自己的示例,用于在Lambda中使用Python创建UUID并将其存储在elasticache中。 Amazon don't explicitly say this will definitively create a unique entry every time, but you can combine this with a check to see if the generated UUID is already in DynamoDB before insertion. 亚马逊没有明确说明每次都会明确地创建一个唯一的条目,但您可以将其与检查结合起来,以确定在插入之前生成的UUID是否已经在DynamoDB中。 The downside to checking if the UUID already exists is that this will use some read capacity on the DynamoDB table and therefore conceptually this is a cost to you. 检查UUID是否已存在的缺点是,它将使用DynamoDB表上的一些读取容量,因此从概念上讲这是一个成本。

Here's the code from AWS, if you adjust this to suit for DynamoDB with the check to see if the UUID has already been created in the table then this would work: 以下是来自AWS的代码,如果您调整此代码以适应DynamoDB,并检查是否已在表中创建了UUID,那么这将起作用:

From: Amazon Lambda Documentation - Create a Lambda Function example 来自: Amazon Lambda文档 - 创建Lambda函数示例

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM