12

In one of my DynamoDb tables I have a column/key named "status", which turned out to be a reserved keyword. Unfortunately it isn't an option to delete the whole table and reinitiate it. How can I rename the key?

Here is the Lambda Code that causes the Exception:

try :
            response = table.query(
                IndexName='myId-index',
                KeyConditionExpression=Key('myId').eq(someId)
            )
            for item in response['Items']:
                print('Updating Item: ' + item['id'])
                table.update_item(
                    Key={
                        'id': item['id']
                    },
                    UpdateExpression='SET myFirstKey = :val1, mySecondKey = :val2, myThirdKey = :val3, myFourthKey = :val4, myFifthKey = :val5, status = :val6',
                    ExpressionAttributeValues={
                        ':val1': someValue1,
                        ':val2': someValue2,
                        ':val3': someValue3,
                        ':val4': someValue4,
                        ':val5': someValue5,
                        ':val6': someValue6
                    }
                )
except Exception, e:
            print ('ok error: %s' % e)

And here is the Exception:

2016-06-14 18:47:24 UTC+2 ok error: An error occurred (ValidationException) when calling the UpdateItem operation: Invalid UpdateExpression: Attribute name is a reserved keyword; reserved keyword: status

Marcos Dimitrio
  • 6,651
  • 5
  • 38
  • 62
weka1
  • 737
  • 2
  • 9
  • 20
  • https://forums.aws.amazon.com/thread.jspa?threadID=91491 – Iłya Bursov Jun 14 '16 at 16:54
  • Thank you for your answer. The problem is, since the key is a reserved keyword, i can not programmatically access it. Lambda will throw an error. So theres no way to copy it's value to a new key and clearing the old one. I can not do it by hand. Too many records. :( – weka1 Jun 14 '16 at 17:06
  • Hi @weka1 welcome to SO or sort of ... SO is not a 1) a code outsourcing facility 2) a code factory SO is 1) a website where you have to break down to pieces your problem 2) is run by volunteers e.g we are not paid for our help 3) read this post for your next questions http://odedcoster.com/blog/2010/07/28/getting-good-answers-on-stackoverflow-part-1-of-n/ – Andy K Jun 14 '16 at 18:27

3 Answers3

13

There is no real easy way to rename a column. You will have to create a new attribute for each of the entries and then delete all the values for the existing attribute.

There is no reason to drop your attribute/column, if you are having trouble querying the table use Expression Attribute Names.

From the Expression Attribute Names documentation:

On some occasions, you might need to write an expression containing an attribute name that conflicts with a DynamoDB reserved word... To work around this, you can define an expression attribute name. An expression attribute name is a placeholder that you use in the expression, as an alternative to the actual attribute name.

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
Shibashis
  • 8,023
  • 3
  • 27
  • 38
  • 1
    Thank you for the quick response. The problem is not querying the data. It is that i can not access the data in the problem-keys programmatically since lambda will give me a keyword error then. So there is also no way to just copy the values to a new key and clearing the old one. :( – weka1 Jun 14 '16 at 17:01
  • Are you retrieving data using lambda? or are u using dynamo db streams? – Shibashis Jun 14 '16 at 17:12
  • I'm not quite sure, what you mean. The data goes into the dynamodb via lambda triggered by s3. I fetch it into my app via the iOS sdk and i'm trying to update various other tables, in case values in this table change, by lambda. But lambda crashes/throws an exception if i try to read/write the values, which are stored in keys which have the same name as a dynamodb keyword. – weka1 Jun 14 '16 at 17:16
  • Got it, can you share the code snippet where it throws the exception? – Shibashis Jun 14 '16 at 17:19
  • Thank you, i added the code to the original question! – weka1 Jun 14 '16 at 17:26
  • 1
    It took me a while, but you were right all along. Adding Expression Attribute Names to the update_item expression did make it work. Thank you! – weka1 Jun 15 '16 at 15:45
5

There is a simple solution instead of renaiming a column: Use projection-expression and expression-attribute-names in your query.

I run over the same problem (my table contains the column "name". Here ist a sample query:

TableName: 'xxxxxxxxxx',
  ExpressionAttributeNames: {
    '#A': 'country',
    '#B': 'postcode',
    '#C': 'name'
  },
  ExpressionAttributeValues: {
    ':a': {S: 'DE'},
    ':c': {S: 'Peter Benz'}
  },
  FilterExpression: 'country = :a AND #C = :c',
  ProjectionExpression: '#A, #B, #C'
Thomas Bayer
  • 71
  • 1
  • 3
1

Using the NoSQL Workbench app AWS supports I exported a copy of the table which had the column name(s) I wanted to change. I opened the JSON file NoSQL Workbench created then did a simple Find/Replace for the name of the column in question.

With the names looking correct on the .JSON file I re-imported the table back into dynamodb using the NoSQL app. The import overwrites existing data which will wipe out the bad column name.

If you have a HUGE data set, downloading a copy to your local computer may not be a good solution, but for my small table it worked pretty well.

Stephen
  • 337
  • 2
  • 7
  • I don't think there's a way to export more than 100 items from a table in that app though – jcollum Jun 22 '22 at 20:32
  • There's an alternative way to export DynamoDB - to S3, using [Point-in-Time recovery](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/PointInTimeRecovery.html) - it exports the data in special JSON-format which is supported by Boto3. So you can export to S3, parse it in-place in S3 in chunks, and import back to Dynamo. – Alex Medveshchek Oct 26 '22 at 18:43