2017-01-19 1 views
0

Je suit le code Python en utilisant Boto:vérification dans le tableau dynamo db si elles existent attribut et si elle ne manque alors avec raison - Boto

import boto3 

# Get the service resource. 
dynamodb = boto3.resource('dynamodb', region_name='us-west-2') 

# Instantiate a table resource object without actually 
# creating a DynamoDB table. Note that the attributes of this table 
# are lazy-loaded: a request is not made nor are the attribute 
# values populated until the attributes 
# on the table resource are accessed or its load() method is called. 
table = dynamodb.Table('djin-dev-genesis') 

def addtodatabase(appname, chef_max, arp_score): 

    response = table.get_item(Key={'appname': str(appname)}) 
    if 'Item' not in response.keys(): 
     print "partititon key (appname) not found, creating a new item in dynamo db with the key" 
     table.put_item(
      Item={ 
       'appname': appname, 
       'chef_max': True, 
       'arp_score': '110' 
      } 
     ) 
    else: 
     print "partition key = "+str(appname)+" found, here is the item dict : "+str(response['Item']) 

Je veux dans le scénario d'autre à regarder si (dans le cas clé existe), si l'une des valeurs d'attributs existe déjà et correspond. Dans ce cas je veux échouer avec le raisonnement sinon mettre à jour l'attribut.

Quel serait le code dans boto pour le faire?

Répondre

0

Utilisez une expression de condition sur l'appel PutItem comme attribute_not_exists(appname) pour autoriser uniquement la création d'un élément s'il n'existe pas déjà. Attraper le ConditionalCheckFailedException et obtenir l'élément avec GetItem autrement.