DynamoDB - Getting Items
To retrieve an item in DynamoDB, you must use GetItem and provide the table name and the item's primary key. Don't forget to include the full primary key and not skip the part.
For example, omitting the sort key of the composite key.
GetItem behavior follows three default settings −
- This is done as an eventually sequential read.
- It provides all the attributes.
- It does not detail its unit power consumption.
These options allow you to override the default GetItem behavior.
Get an item
DynamoDB provides reliability by supporting multiple copies of items across multiple servers. Each successful write creates these copies, but takes a significant amount of time to complete; the meaning eventually matches. This means that you cannot immediately try to read after the element has been written.
You can change the default GetItem consistent read, however the cost of more up-to-date data remains the consumption of more capacity units; in particular, twice as much. Note. DynamoDB typically ensures that every copy is consistent within a second.
You can use the GUI console, Java, or another tool to accomplish this task.
Finding items using Java
Using Java in item lookup operations requires creating an instance of the DynamoDB class, an instance of the table class, and calling the getItem method of the table instance. Then specify the element's primary key.
You can view the following example −
DynamoDB dynamoDB = new DynamoDB ( new AmazonDynamoDBClient ( new ProfileCredentialsProvider ())); Table table = dynamoDB . getTable ( "ProductList" ); Item item = table . getItem ( "IDnum" , 109 );
In some cases, you need to specify parameters for this operation.
The following example uses .withProjectionExpression and GetItemSpec to get search specifications −
GetItemSpec spec = new GetItemSpec () . withPrimaryKey ( "IDnum" , 122 ) . withProjectionExpression ( "IDnum, EmployeeName, Department" ) . withConsistentRead ( true ); Item item = table . getItem ( spec ); System . out . println ( item.toJSONPretty ( ) );
You can also consider the following big example for better understanding.
Note. The following example can use a previously created data source. Before attempting to execute, acquire the supporting libraries and create the necessary data sources (tables with the required characteristics or other referenced sources).
This example also uses the Eclipse IDE, an AWS credential file, and the AWS Toolkit in an Eclipse AWS Java project.