Read an Item from a DynamoDB Table

Reading an item is a common operation, allowing you to retrieve information from your table. You'll use the primary key to specify the item you want to read, and DynamoDB will return the item with all its attributes. Here’s how you can do it in Node.js and Python:

Node.js:

const params = {  
    TableName: table,  
    Key: {  
       'UserID': userID  
    }  
};  
      
docClient.get(params, function(err, data) {  
    if (err) console.error('Error', err);  
    else  console.log('GetItem succeeded:', data);  
});

This Node.js code constructs parameters to fetch an item by 'UserID' from 'UserTable' and uses the get method of DocumentClient to retrieve it, logging the result or error.

Python:

response = table.get_item(  
    Key={  
       'UserID': 'user_001'  
    }  
)  
item = response['Item']  
print(item)

The Python snippet retrieves an item from 'UserTable' using 'UserID' with Boto3's get_item method, then prints the retrieved item's details.