DynamoDB - Crafting Items
Creating an element in DynamoDB consists primarily of the specification of the element and attribute, and the ability to specify conditions. Each element exists as a set of attributes, each named attribute is assigned a value of a specific type.
Value types include scalar, document, or set. Elements have a size limit of 400 KB, with the possibility of any number of attributes able to fit within this limit. The name and value sizes (binary length and UTF-8 length) determine the size of the element. Using short attribute names helps keep the size of the element to a minimum.
Note. All attributes of the primary key must be specified, with primary keys requiring only the partition key; and composite keys, requiring both a partition and a sort key.
Also, remember that tables don't have a predefined schema. You can store completely different sets of data in the same table.
Use a GUI console, Java, or another tool to complete this task.
How to create an element using the GUI console?
Go to console. In the navigation bar on the left, select Tables . Select the table name to use as the destination and then select the Elements tab as shown in the following screenshot.
Select Create item . The Create Item screen provides an interface for entering the required attribute values. Any secondary indexes must also be entered.
If you need more attributes, select the action menu to the left of the message . Then select Add and select the desired data type.
After entering all the required information, select Save to add the item.
How to use Java in item creation?
Using Java in item creation operations consists of creating an instance of the DynamoDB class, an instance of the Table class, an instance of the Item class, and specifying the primary key and attributes of the item being created. Then add a new item using the putItem method.
DynamoDB dynamoDB = new DynamoDB ( new AmazonDynamoDBClient ( new ProfileCredentialsProvider ())); Table table = dynamoDB . getTable ( "ProductList" ); // Spawn a related items list List < Number > RELItems = new ArrayList < Number >(); RELITEms . add ( 123 ); RELITEms . add ( 456 ); RELITEms . add ( 789 ); //Spawn a product picture map Map < String , String > photos = new HashMap < String , String >(); photos . put ( "Anterior" , "http://xyz.com/products/101_front.jpg" ); photos . put ( "Posterior" , "http://xyz.com/products/101_back.jpg" ); photos . put ( "Lateral" , "http://xyz.com/products/101_LFTside.jpg" ); //Spawn a product review map Map < String , List < String >> prodReviews = new HashMap < String , List < String >>(); List < String > fiveStarRVW = new ArrayList < String >(); fiveStarRVW . add ( "Shocking high performance." ); fiveStarRVW . add ( "Unparalleled in its market." ); productReviews . put ( "5 Star" , fiveStarRVW ); List < String > oneStarRVW = new ArrayList < String >(); oneStarRVW . add ( "The worst offering in its market." ); productReviews . put ( "1 Star" , oneStarRVW ); // Generate the item Item item = new Item () . withPrimaryKey ( "Id" , 101 ) . withString ( "Nomenclature" , "PolyBlaster 101" ) . withString ( "Description" , "101 description" ) . withString ( "Category" , "Hybrid Power Polymer Cutter" ) . withString ( "Make" , "Brand - XYZ" ) . , 50000 ) . withString ( "ProductCategory" , "Laser Cutter" ) . withBoolean ( "Availability" , true ) . withNull ( "Qty" ) . withList ( "ItemsRelated" , RELItems ) . withMap ( "Images" , photos ) . withMap ( "Reviews" , prodReviews ); // Add item to the table PutItemOutcome outcome = table . putItem ( item );
You can also look at the next larger example.
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).
The following example also uses the Eclipse IDE, the AWS credential file, and the AWS toolkit in an Eclipse AWS Java project.