Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python检查dynamodb表中是否存在值,并获取该记录_Python_Python 3.x_Amazon Dynamodb_Boto3_Dynamodb Queries - Fatal编程技术网

使用python检查dynamodb表中是否存在值,并获取该记录

使用python检查dynamodb表中是否存在值,并获取该记录,python,python-3.x,amazon-dynamodb,boto3,dynamodb-queries,Python,Python 3.x,Amazon Dynamodb,Boto3,Dynamodb Queries,我是Dynamodb的新手,目前正在学习。我正在使用python查询dynamodb,以根据IF条件获取一些记录 我有一个dynamodb表cus_token,只有两个属性a)customerId和b)token 当用户提供令牌时,检查cus_令牌表中是否存在该令牌,如果存在,我想查询并获取该令牌的customerID。我试着这样做 假设 user\u input=“Rooxp9”(这是cus\u令牌表中的令牌) 首先使用token属性获取所有值 import boto3 import jso

我是Dynamodb的新手,目前正在学习。我正在使用python查询dynamodb,以根据IF条件获取一些记录

我有一个dynamodb表
cus_token
,只有两个属性a)customerId和b)token

当用户提供令牌时,检查cus_令牌表中是否存在该令牌,如果存在,我想查询并获取该令牌的customerID。我试着这样做

假设
user\u input=“Rooxp9”
(这是cus\u令牌表中的令牌)

首先使用token属性获取所有值

import boto3
import json

def gettokens(y):
    dynamodb = boto3.resource('dynamodb')
    table = dynamodb.Table('cus_token')
    response = table.scan()

    tokens = []
    for i in response['Items']:
        nameList.append(i['token'])

    return tokens

temp = gettokens(cus_token)
检查使用条件

for item in temp:
    if item == user_input:
        "Here I want to write the code to fetch the customerID"

听起来您想要获取一个
客户ID
,给定一个
令牌

要在当前的表设计中实现这一点,您需要使用a请求DynamoDB返回
标记
等于用户输入的所有属性。例如,以下是伪代码中的一个片段:

response = table.scan({"FilterExpression": "#token = :token",
                       "ExpressionAttributeNames": {"#token":"token"},
                       "ExpressionAttributeValues": {":token": {"S": <user_input>}})

response=table.scan({“FilterExpression”:“#token=:token”,
“ExpressionAttributeNames”:{“#token”:“token”},
“ExpressionAttributeValues:{”:标记:{“S”:})
无需扫描整个数据库,将其返回到应用程序,然后在应用程序中过滤结果


如果您刚刚开始使用DynamoDB,我强烈推荐。它在向初学者解释DynamoDB方面做得很好。

我还想验证用户给定的令牌是否与表中现有的令牌匹配。只有当令牌匹配时,我才需要获取相应的CustomerId。此扫描操作将只返回令牌与您要求的匹配(用户输入)。如果不匹配,您将不会得到任何结果。不需要额外验证。默认情况下,扫描操作将返回匹配项中的所有属性,这意味着您将在结果中包含CustomerID。