在DynamoDB中创建项目主要包括项目和属性规范,以及指定条件的选项.每个项目都作为一组属性存在,每个属性都被命名并赋予特定类型的值.
值类型包括标量,文档或集合.物品的尺寸限制为400KB,可以使任何数量的属性符合该限制.名称和值大小(二进制和UTF-8长度)确定项目大小.使用短属性名称有助于最小化项目大小.
注意 : 您必须指定所有主键属性,主键只需要分区键;和复合键同时需要分区和排序键.
另外,请记住表没有预定义的模式.您可以在一个表中存储截然不同的数据集.
使用GUI控制台,Java或其他工具执行此任务.
如何创建使用GUI控制台的项目?
导航到控制台.在左侧的导航窗格中,选择表.选择用作目的地的表名,然后选择项标签,如以下屏幕截图所示.
选择创建项目. "创建项"屏幕提供用于输入所需属性值的界面.还必须输入任何二级索引.
如果如果需要更多属性,请选择消息左侧的操作菜单.然后选择附加和所需的数据类型.
输入所有基本信息后,选择保存添加项目.
如何在项目创建中使用Java?
在项目创建操作中使用Java包括创建DynamoDB类实例,Table类实例,Item类实例,以及指定要创建的项目的主键和属性.然后使用putItem方法添加新项目.
示例
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."); prodReviews.put("5 Star", fiveStarRVW); List<String> oneStarRVW = new ArrayList<String>(); oneStarRVW.add("The worst offering in its market."); prodReviews.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") .withNumber("Price", 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);
您还可以查看以下更大的示例.
注意 去;以下示例可以假定先前创建的数据源.在尝试执行之前,获取支持库并创建必要的数据源(具有所需特征的表或其他引用的源).
以下示例还使用Eclipse IDE,AWS凭证文件和Eclipse AWS Java项目中的AWS Toolkit.
package com.amazonaws.codesamples.document;import java.io.IOException;import java.util.Arrays;import java.util.HashMap;import java.util.HashSet;import java.util.Map;import com.amazonaws.auth.profile.ProfileCredentialsProvider;import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;import com.amazonaws.services.dynamodbv2.document.DeleteItemOutcome;import com.amazonaws.services.dynamodbv2.document.DynamoDB;import com.amazonaws.services.dynamodbv2.document.Item;import com.amazonaws.services.dynamodbv2.document.Table;import com.amazonaws.services.dynamodbv2.document.UpdateItemOutcome;import com.amazonaws.services.dynamodbv2.document.spec.DeleteItemSpec;import com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec;import com.amazonaws.services.dynamodbv2.document.utils.NameMap;import com.amazonaws.services.dynamodbv2.document.utils.ValueMap;import com.amazonaws.services.dynamodbv2.model.ReturnValue;public class CreateItemOpSample { static DynamoDB dynamoDB = new DynamoDB(new AmazonDynamoDBClient ( new ProfileCredentialsProvider())); static String tblName = "ProductList"; public static void main(String[] args) throws IOException { createItems(); retrieveItem(); // Execute updates updateMultipleAttributes(); updateAddNewAttribute(); updateExistingAttributeConditionally(); // Item deletion deleteItem(); } private static void createItems() { Table table = dynamoDB.getTable(tblName); try { Item item = new Item() .withPrimaryKey("ID", 303) .withString("Nomenclature", "Polymer Blaster 4000") .withStringSet( "Manufacturers", new HashSet<String>(Arrays.asList("XYZ Inc.", "LMNOP Inc."))) .withNumber("Price", 50000) .withBoolean("InProduction", true) .withString("Category", "Laser Cutter"); table.putItem(item); item = new Item() .withPrimaryKey("ID", 313) .withString("Nomenclature", "Agitatatron 2000") .withStringSet( "Manufacturers", new HashSet<String>(Arrays.asList("XYZ Inc,", "CDE Inc."))) .withNumber("Price", 40000) .withBoolean("InProduction", true) .withString("Category", "Agitator"); table.putItem(item); } catch (Exception e) { System.err.println("Cannot create items."); System.err.println(e.getMessage()); } } }
免责声明:以上内容(如有图片或视频亦包括在内)有转载其他网站资源,如有侵权请联系删除
-
设计总结|如何更好地表达活动品牌?
-
谈谈“目标思维”的落地
编辑导读:我们在做数据分析之前,一定要搞清楚需求方的目标到底是什么,要根据目标来重新定义业务方提出的问题,这就是目标思维。目标思维有多重要呢?应该如何落地呢...
-
在线教育平台竞品分析:网易云课堂vs腾讯课堂
本文从移动端出发,对当前比较热门的两款在线教育平台软件-网易云课堂和腾讯课堂进行比较和分析,不足之处还请大家多提意见。 市场分析 随着国内互联网技术的发展和移...
-
即学即用|父亲节活动的4种运营策略
-
B端产品经理和体验设计师的工作职责边界梳理
-
豆果美食电商分支用户体验报告及建议
-
数据分析师如何提高工作效率
在我们的日常工作中,提高工作效率是每个岗位都需要实现的,在工作中,面对比较凌乱的事情时,首先我们需要梳理清楚,按重要级进行开展;本文作者分享了关于...
-
2016中国云计算SaaS移动办公平台年度综合报告
-
网易大布局教育事业:网易公开课、网易云课堂和MOOC分析
-
一篇文章搞懂语音交互的来龙去脉