Ejemplo n.º 1
0
def query_item(tablename):

    t = ti.table_exists(tablename)
    if t == None:
        print("** Table", tablename, "does not exist in this region")
        return

    tab = ddb.Table(tablename)
    queryResponse = tab.query(KeyConditionExpression=Key('kcol').eq('keyval1'))

    # Invalid - OR in key - only one condition allowed in key
    # KeyConditionExpression = Key('kcol').eq('keyval1') | Key('kcol').eq('keyval3')
    # Invalid - 'query key condition not supported'
    # KeyConditionExpression = Key('kcol').begins_with('keyval')
    print(queryResponse)
    print()
    print(len(queryResponse['Items']), "filtered item(s) found",
          queryResponse['ScannedCount'], "item(s) were scanned")
    count = 0
    for item in queryResponse['Items']:
        print("Item", count)
        for k, v in item.items():
            print("-", k, ":", v)
        count += 1

    return
Ejemplo n.º 2
0
def scan_items(tablename):

    t = ti.table_exists(tablename)
    if t == None:
        print("** Table", tablename, "does not exist in this region")
        return

    response = ddb_client.scan(TableName=tablename)

    print(len(response['Items']), "item(s) found")
    count = 0
    for item in response['Items']:
        print("Item", count)
        for k, v in item.items():
            print("-", k, ":", v)
        count += 1

    # Do a count scan operation
    countResponse = ddb_client.scan(TableName=tablename, Select="COUNT")

    print()
    print("Count:", countResponse['Count'], "Scanned count:",
          countResponse['ScannedCount'])

    # Do a filtered query - using DDB low-levelclient means using FilterExpression as a string
    # Not clear how to format these - get syntax errors.
    #filter = "nkeyint = '2'"
    #filteredResponse = ddb_client.scan(
    #    TableName = tablename,
    #    FilterExpression = filter
    #)

    # So use the DDB resource-level interface instead, which has a higher-level filtering approach, using Attr objects (see imports)
    tab = ddb.Table(tablename)
    filteredResponse = tab.scan(
        FilterExpression=Attr('nkeyint').eq(2)
        & Attr('nkeystring').begins_with('non')  # NB & not and
    )

    print(len(filteredResponse['Items']), "filtered item(s) found",
          filteredResponse['ScannedCount'], "item(s) were scanned")
    count = 0
    for item in filteredResponse['Items']:
        print("Item", count)
        for k, v in item.items():
            print("-", k, ":", v)
        count += 1

    return
Ejemplo n.º 3
0
def put_items(tablename):

    t = ti.table_exists(tablename)
    if t == None:
        print("** Table", tablename, "does not exist in this region")
        return

    count = 0
    for d in [("keyval1", 1, "nonkeyval1"), ("keyval2", 2, "nonkeyval2"),
              ("keyval3", 3, "nonkeyval3")]:
        keyval = d[0]
        nonkeyintval = d[1]
        nonkeystringval = d[2]

        # Add records, specifying the key column and two non-key columns
        # NB for Python, type seems to be inferred, so just do
        #    'colname' : <stringvalue>
        # rather than the syntax inplied in the documentation.
        #    'colname' : { "S" : <stringvalue> }
        print("Adding record", keyval, "...")
        itemsDict = {
            'kcol': keyval,
            'nkeyint': nonkeyintval,
            'nkeystring': nonkeystringval
        }

        # Add another non-key field to one item, don't have to have same fields in each item
        if count == 2:
            itemsDict['anothernonkey'] = 'abc'

        try:
            response = ddb_client.put_item(
                TableName=tablename,
                Item=itemsDict,
                ConditionExpression=
                'attribute_not_exists(kcol)'  # i.e. don't replace existing item with the same key
            )
            print(response)
        except ddb_client.exceptions.ConditionalCheckFailedException as e:
            print("- duplicate key, not added")
        except Exception as e:
            print("Exception", e)

        count += 1

    return
Ejemplo n.º 4
0
def delete_items(tablename):

    t = ti.table_exists(tablename)
    if t == None:
        print("** Table", tablename, "does not exist in this region")
        return

    count = 0
    for keyval in ["keyval1", "keyval2", "keyval3"]:

        # Add records, specifying the key column and two non-key columns
        print("Deleting record", keyval, "...")
        keysDict = {'kcol': keyval}

        response = ddb_client.delete_item(TableName=tablename, Key=keysDict)

        count += 1
        print(response)

    return
Ejemplo n.º 5
0
def get_item(tablename) :

    t = ti.table_exists(tablename)
    if t == None :
        print("** Table", tablename, "does not exist in this region")
        return

    tab = ddb.Table(tablename)

    keyval = 'keyval1'
    response = tab.get_item(
        Key= { 'kcol' : keyval }
    )

    if 'Item' in response :
        print(keyval, "found in table", tablename)
        for k,v in response['Item'].items() :
            print("-", k, ":", v)
    else :
        print(keyval, "not found in table", tablename)

    return
Ejemplo n.º 6
0
def update_item(tablename) :

    t = ti.table_exists(tablename)
    if t == None :
        print("** Table", tablename, "does not exist in this region")
        return

    tab = ddb.Table(tablename)

    keyval = 'keyval1'
    response = tab.get_item(
        Key= { 'kcol' : keyval }
    )

    nkeyValue = 0
    if 'Item' in response :
        print(keyval, "found in table", tablename)
        for k,v in response['Item'].items() :
            print("-", k, ":", v)
        nkeyValue = response['Item']['nkeyint']
    else :
        print(keyval, "not found in table", tablename)
        return

    nkeyValue2 = nkeyValue + 10
    print()
    print("Updating nkeyint value from", nkeyValue, "to", nkeyValue2)

    updateResponse = tab.update_item(
        Key= { 'kcol' : keyval },
        UpdateExpression = "SET nkeyint = :val1",
        ExpressionAttributeValues = { ':val1' : nkeyValue2 }
    )

    print(updateResponse)

    return