Overview of DynamoDB
Once your table is set up, you can start adding items to it. Each item represents a unique entry in your table. You'll define an item using a JSON-like structure, specifying keys and their associated values. Here’s an example in both Node.js and Python:
Node.js:
const docClient = new AWS.DynamoDB.DocumentClient(); const table = 'UserTable'; const userID = 'user_001'; const userName = 'John Doe'; const params = { TableName: table, Item: { 'UserID': userID, 'UserName': userName } }; docClient.put(params, function(err, data) { if (err) console.error('Error', err); else console.log('Item Added', data); });
This Node.js snippet initializes a DynamoDB DocumentClient, creates an item with a user ID and name, and then uses the put method to add the item to the 'UserTable'.
Python:
table = dynamodb.Table('UserTable') response = table.put_item( Item={ 'UserID': 'user_001', 'UserName': 'John Doe' } ) print("PutItem succeeded:", response)
The Python code snippet uses Boto3 to reference the 'UserTable', creates an item, and adds it to the table with the put_item method, then prints the success response.