使用Java创建DynamoDB表
在这篇文章中,我们将使用java方法在DynamoDB数据库上创建表。 在开始之前,我们需要安装本地dynamodb,因为我们要避免使用dynamodb的任何费用。 有一个以前的岗位上本地dynamodb。
如果您使用docker,则可以找到本地dynamodb映像,也可以按照此处所述自行创建一个。 dynamodb java sdk使我们能够使用java代码创建dynamodb表。
最基本的操作是使用哈希键创建表。 在这种情况下,用户的电子邮件将是哈希密钥。
List<KeySchemaElement> elements = new ArrayList<KeySchemaElement>();KeySchemaElement keySchemaElement = new KeySchemaElement().withKeyType(KeyType.HASH).withAttributeName("email");elements.add(keySchemaElement);List<AttributeDefinition> attributeDefinitions = new ArrayList<>();attributeDefinitions.add(new AttributeDefinition().withAttributeName("email").withAttributeType(ScalarAttributeType.S));CreateTableRequest createTableRequest = new CreateTableRequest().withTableName("Users").withKeySchema(elements).withProvisionedThroughput(new ProvisionedThroughput().withReadCapacityUnits(5L).withWriteCapacityUnits(5L)).withAttributeDefinitions(attributeDefinitions);amazonDynamoDB.createTable(createTableRequest);
我们所做的是使用他的电子邮件作为哈希键创建Users表。 下表称为“登录名”。 每次用户登录时,登录都应保持跟踪。除了使用哈希键之外,我们还将使用范围键。
List<KeySchemaElement> elements = new ArrayList<KeySchemaElement>();KeySchemaElement hashKey = new KeySchemaElement().withKeyType(KeyType.HASH).withAttributeName("email");KeySchemaElement rangeKey = new KeySchemaElement().withKeyType(KeyType.RANGE).withAttributeName("timestamp");elements.add(hashKey);elements.add(rangeKey);List<AttributeDefinition> attributeDefinitions = new ArrayList<>();attributeDefinitions.add(new AttributeDefinition().withAttributeName("email").withAttributeType(ScalarAttributeType.S));attributeDefinitions.add(new AttributeDefinition().withAttributeName("timestamp").withAttributeType(ScalarAttributeType.N));CreateTableRequest createTableRequest = new CreateTableRequest().withTableName("Logins").withKeySchema(elements).withProvisionedThroughput(new ProvisionedThroughput().withReadCapacityUnits(5L).withWriteCapacityUnits(5L)).withAttributeDefinitions(attributeDefinitions);amazonDynamoDB.createTable(createTableRequest);
通过使用电子邮件作为哈希键,我们可以查询特定用户的登录名。 通过使用登录发生的日期作为范围键,可以查找登录条目的排序或基于特定用户的登录日期执行高级查询。
但是,在大多数情况下,哈希键和范围键不足以满足我们的需求。 DynamoDB为我们提供了全局二级索引和本地二级索引。
我们将创建表SupervisorS。 Supervisor的哈希键将是他的名字。 主管将为公司工作。 该公司将成为我们的全球二级指数。 由于公司拥有多个工厂,因此实地工厂将成为范围的关键。
List<KeySchemaElement> elements = new ArrayList<>();KeySchemaElement hashKey = new KeySchemaElement().withKeyType(KeyType.HASH).withAttributeName("name");elements.add(hashKey);List<GlobalSecondaryIndex> globalSecondaryIndices = new ArrayList<>();ArrayList<KeySchemaElement> indexKeySchema = new ArrayList<>();indexKeySchema.add(new KeySchemaElement().withAttributeName("company").withKeyType(KeyType.HASH)); //Partition keyindexKeySchema.add(new KeySchemaElement().withAttributeName("factory").withKeyType(KeyType.RANGE)); //Sort keyGlobalSecondaryIndex factoryIndex = new GlobalSecondaryIndex().withIndexName("FactoryIndex").withProvisionedThroughput(new ProvisionedThroughput().withReadCapacityUnits((long) 10).withWriteCapacityUnits((long) 1)).withKeySchema(indexKeySchema).withProjection(new Projection().withProjectionType(ProjectionType.ALL));globalSecondaryIndices.add(factoryIndex);List<AttributeDefinition> attributeDefinitions = new ArrayList<>();attributeDefinitions.add(new AttributeDefinition().withAttributeName("name").withAttributeType(ScalarAttributeType.S));attributeDefinitions.add(new AttributeDefinition().withAttributeName("company").withAttributeType(ScalarAttributeType.S));attributeDefinitions.add(new AttributeDefinition().withAttributeName("factory").withAttributeType(ScalarAttributeType.S));CreateTableRequest createTableRequest = new CreateTableRequest().withTableName("Supervisors").withKeySchema(elements).withProvisionedThroughput(new ProvisionedThroughput().withReadCapacityUnits(5L).withWriteCapacityUnits(5L)).withGlobalSecondaryIndexes(factoryIndex).withAttributeDefinitions(attributeDefinitions);amazonDynamoDB.createTable(createTableRequest);
下一个表将是公司表。 哈希键将是母公司,范围键将是子公司。 每个公司都有一位首席执行官。 CEO将是本地二级索引的范围键。
List<KeySchemaElement> elements = new ArrayList<>();KeySchemaElement hashKey = new KeySchemaElement().withKeyType(KeyType.HASH).withAttributeName("name");KeySchemaElement rangeKey = new KeySchemaElement().withKeyType(KeyType.RANGE).withAttributeName("subsidiary");elements.add(hashKey);elements.add(rangeKey);List<LocalSecondaryIndex> localSecondaryIndices = new ArrayList<>();ArrayList<KeySchemaElement> indexKeySchema = new ArrayList<>();indexKeySchema.add(new KeySchemaElement().withAttributeName("name").withKeyType(KeyType.HASH));indexKeySchema.add(new KeySchemaElement().withAttributeName("ceo").withKeyType(KeyType.RANGE));LocalSecondaryIndex ceoIndex = new LocalSecondaryIndex().withIndexName("CeoIndex").withKeySchema(indexKeySchema).withProjection(new Projection().withProjectionType(ProjectionType.ALL));localSecondaryIndices.add(ceoIndex);List<AttributeDefinition> attributeDefinitions = new ArrayList<>();attributeDefinitions.add(new AttributeDefinition().withAttributeName("name").withAttributeType(ScalarAttributeType.S));attributeDefinitions.add(new AttributeDefinition().withAttributeName("subsidiary").withAttributeType(ScalarAttributeType.S));attributeDefinitions.add(new AttributeDefinition().withAttributeName("ceo").withAttributeType(ScalarAttributeType.S));CreateTableRequest createTableRequest = new CreateTableRequest().withTableName("Companies").withKeySchema(elements).withProvisionedThroughput(new ProvisionedThroughput().withReadCapacityUnits(5L).withWriteCapacityUnits(5L)).withLocalSecondaryIndexes(localSecondaryIndices).withAttributeDefinitions(attributeDefinitions);amazonDynamoDB.createTable(createTableRequest);
您可以在github上找到源代码。
翻译自: https://www.javacodegeeks.com/2016/06/create-dynamodb-tables-java.html
使用Java创建DynamoDB表相关推荐
- dynamodb java_使用Java将项目插入DynamoDB表
dynamodb java 在上一篇文章中,我们学习了如何使用Java创建DynamoDB表. 下一步是将项目插入到先前创建的DynamoDB表中. 请记住,对于插入操作,最基本的步骤是指定主键. 对 ...
- 使用Java将项目插入DynamoDB表
在上一篇文章中,我们学习了如何使用Java创建DynamoDB表. 下一步是将项目插入到先前创建的DynamoDB表中. 请记住,对于插入操作,最基本的步骤是指定主键. 对于表用户,主键是属性电子邮件 ...
- Java连接HBASE数据库,创建一个表,删除一张表,修改表,输出插入,修改,数据删除,数据获取,显示表信息,过滤查询,分页查询,地理hash
准备工作 1.创建Java的Maven项目 创建好的目录结构如下: 另外注意junit的版本,最好不要太高,最开始笔者使用的junit4.12的,发现运行的时候会报错.最后把Junit的版本改成4.7 ...
- JAVA实现创建Excel表并导出(转发)
JAVA实现创建Excel表并导出(转发) 最近在做毕设,要求导出word,excel,pdf,这是excel. 原文是:http://blog.csdn.net/u014621859/article ...
- Java 创建、填充PDF表单域
表单域,可以按用途分为多种不同的类型,常见的有文本框.多行文本框.密码框.隐藏域.复选框.单选框和下拉选择框等,目的是用于采集用户的输入或选择的数据.下面的示例中,将分享通过Java编程在PDF中添加 ...
- java数据库表不存在_如果Java生产代码中不存在并在JUnit中确认,则创建数据库表...
Code-Apprentice 2 java sql junit jdbc 我正在用Java编写数据库程序,并且想要创建一个表(如果它还不存在).我从中了解DatabaseMetaData.getTa ...
- Activiti流创建数据表的过程中报错 java.sql.SQLSyntaxErrorException: Table ‘activiti.act_ge_property‘ doesn‘t exis
Activiti流创建数据表的过程中报错 java.sql.SQLSyntaxErrorException: Table 'activiti.act_ge_property' doesn't exis ...
- java刷新透视表数据源,Java 创建、刷新Excel透视表/设置透视表行折叠、展开
Java 创建.刷新Excel透视表/设置透视表行折叠.展开 透视表是依据已有数据源来创建的交互式表格,我们可在excel中创建透视表,也可编辑已有透视表.本文以创建透视表.刷新透视表以及设置透视表的 ...
- java excel 透视_Java在Excel中创建透视表方法解析
本文内容介绍通过Java程序在Excel表格中根据数据来创建透视表. 环境准备 需要使用Excel类库工具-Free Spire.XLS for Java,这里使用的是免费版,可通过官网下载Jar包并 ...
最新文章
- HR一般不会告诉你的八大真相
- 利用计算机技术教学图片,教育教学论文 科学学科如何利用计算机技术提高教学效率.doc...
- WEBSERVICE之JDK开发webservice
- MySQL 的 bug 必须修复吗?
- 使用LAMP创建基于wordpress的个从博客网站
- CF 1635E Cars 二分图 + 拓扑
- logging.getLogger(logger)
- 重新学习Ubuntu -- 截图软件的选择和安装
- 漫步微积分二十三——重力作用下的运动 逃逸速度和黑洞
- java instanceof运算符_java的instanceof运算符
- Python使用wordnet工具计算词集与词条基本用法(三)
- 安川机器人焊枪切换设定方法_【分享】焊接机器人的性能要求与系统构成
- su and sudo
- 企业级数据服务的一点感受
- windows命令行包管理工具 -Scoop
- 百度离线地图服务器搭建
- OneNote的同步问题
- ubuntu网络检查服务器端口是否开放
- USACO Section 1.1 Broken Necklace
- python开发学习-day01 (python安装与版本、字符串、字典、运算符、文件)
热门文章
- Tomcat Get请求的巨坑
- Hibernate之必须导入jar包
- sql server高级查询,看这篇文章就够了
- mybatis使用全注解的方式案例(包含一对多关系映射)
- java内部类选择题_java内部类详解(附相关面试题)
- AQS的细节--自用,非正常教程
- 对象作为参数示例java_功能Java示例 第6部分–用作参数
- 从Speedment 3.0.17或更高版本的事务轻松返回值
- rxjava 背压_背压加载文件– RxJava常见问题解答
- neo4j 添加属性_Neo4j:动态添加属性/设置动态属性