使用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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/353633.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

表达式前后缀表达形式 [zz]

(2012-09-12 13:08:39) 转载▼标签&#xff1a; 杂谈 转自&#xff1a;http://blog.csdn.net/whatforever/article/details/673853835,15,,80,70,-,*,20,/ //后缀表达方式(((3515)*(80-70))/20&#xff09;25 //中缀表达方式 /,*,,35,15,-,80,70, 2…

引用:初探Sql Server 执行计划及Sql查询优化

引用:初探Sql Server 执行计划及Sql查询优化 原文:引用:初探Sql Server 执行计划及Sql查询优化初探Sql Server 执行计划及Sql查询优化 收藏MSSQL优化之————探索MSSQL执行计划作者&#xff1a;no_mIss最近总想整理下对MSSQL的一些理解与感悟&#xff0c;却一直没有心思和时间…

Cities

问题 C: Cities 时间限制: 1 Sec 内存限制: 128 MB提交: 87 解决: 61[提交][状态][讨论版][命题人:admin]题目描述 There are n cities in Byteland, and the ith city has a value ai. The cost of building a bidirectional road between two cities is the sum of their v…

spring观察者模式_Spring事件的观察者模式

spring观察者模式介绍 观察者模式的本质是“定义对象之间的一对多依赖关系&#xff0c;以便当一个对象改变状态时&#xff0c;其所有依赖关系都会被通知并自动更新”。 GoF。 观察者模式是发布/订阅模式的子集&#xff0c;它允许许多观察者对象查看事件。 可以在不同的情况下使…

Lombok,自动值和不可变项

我喜欢布兰登&#xff08;Brandon &#xff09;在博客文章中比较Project Lombok &#xff0c; AutoValue和Immutables的建议 &#xff0c;而这篇文章试图做到这一点。 我已经简要概述了Project Lombok &#xff0c; AutoValue和Immutables &#xff0c;但是这篇文章有所不同&am…

linux对于zombie的处理

(Linux基础)[僵尸进程处理] 今天在服务器上推送项目的时候&#xff0c;突然发现很卡。就用top查看了一下&#xff0c;果然此事不简单啊。 top - 10:39:16 up 20 days, 23:11, 2 users, load average: 1.13, 1.09, 1.03 Tasks: 204 total, 2 running, 196 sleeping, 1 sto…

POJ - 1847 Tram(dijkstra)

题意&#xff1a;有向图有N个点&#xff0c;当电车进入交叉口&#xff08;某点&#xff09;时&#xff0c;它只能在开关指向的方向离开。 如果驾驶员想要采取其他方式&#xff0c;他/她必须手动更换开关。当驾驶员从路口A驶向路口B时&#xff0c;他/她尝试选择将他/她不得不手动…

用interrupt()中断Java线程

Javathread 最近在学习Java线程相关的东西&#xff0c;和大家分享一下&#xff0c;有错误之处欢迎大家指正&#xff0e; 假如我们有一个任务如下&#xff0c;交给一个Java线程来执行&#xff0c;如何才能保证调用interrupt()来中断它呢&#xff1f; Java代码 class ATask imple…

activemq和jms_保证主题,JMS规范和ActiveMQ的消息传递

activemq和jms最近&#xff0c;一位客户要求我仔细研究ActiveMQ的“持久”消息的实现&#xff0c;它如何应用于主题以及在存在非持久订户的故障转移方案中会发生什么。 我已经了解到&#xff0c;JMS语义规定&#xff0c;即使面对消息代理提供程序故障&#xff0c;也只能保证主题…

JAVA分代收集机制详解

Java堆中是JVM管理的最大一块内存空间。主要存放对象实例。在JAVA中堆被分为两块区域&#xff1a;新生代&#xff08;young&#xff09;、老年代&#xff08;old&#xff09;。堆大小新生代老年代&#xff1b;&#xff08;新生代占堆空间的1/3、老年代占堆空间2/3&#xff09;新…

FizzBu​​zz Kata与Java流

在柔道练习仅几周之后&#xff0c;我的儿子感到无聊。 他抱怨说自己没有学任何东西&#xff0c;因为他一遍又一遍地做着同样的事情。 混淆学习和做新事物的不仅仅是幼儿。 例如&#xff0c;有多少软件开发人员通过执行kata或参加dojos来进行刻意练习的麻烦&#xff1f; 重复您…

高可用架构

转载于:https://www.cnblogs.com/138026310/p/9088341.html

vc6.0快捷键

2010-09-14 17:46 F1 显示帮助,如果光标停在代码的某个字符上,显示MSDN中相应的帮助内容&#xff08;需要安装MSDN才能使用&#xff09; F2 书签功能: CtrlF2 --在某行设置一个书签(再按一次是取消) F2 --跳到下一个书签位置 ShiftF2 --跳到上一个书签位置 CtrlShiftF2 --删除…

ES6 各浏览器支持情况

http://kangax.github.io/compat-table/es6/转载于:https://www.cnblogs.com/likwin/p/9091008.html

MFC删除类的小窍门

VC 并没有提供删除一个类的简洁方法&#xff0c;我们需要手工删除&#xff0c;但过程并不复杂。例如&#xff0c;我们有一个 CMyButton 的类&#xff0c;对应的文件为 MyButton.h 和 MyButton.cpp &#xff0c;我们先到工程目录中删除这两个文件&#xff0c;此时该类信息自动从…

ios plist 国际化_Java与iOS对话:Java对象与Apple plist序列化

ios plist 国际化我很高兴地宣布我的第一个开源项目java-plist-serializer可以帮助您将Java&#xff08;尤其是基于Spring的应用程序&#xff09;与iOS应用程序集成在一起。 背景 我正在将Java webapp作为后端并且客户端是iOS设备的项目。 最近&#xff0c;我收到了创建Web服务…

vtk环境搭建(windowsXP/win7,vtk6.0.0+cmake2.8+vs2010)

1. 安装vs2010&#xff08;默认方式&#xff09; 2. 安装cmake2.8&#xff08;默认方式&#xff09; 3. 新建文件夹作为vtk文件根目录&#xff08;例如"E:\vtk6.0"&#xff09;&#xff0c;解压vtk-6.0.0.zip到根目录&#xff0c;将VTK6.0.0改名为vtk&#xff1b;解…

P1993 小K的农场 (差分约束)

题目描述 小K在MC里面建立很多很多的农场&#xff0c;总共n个&#xff0c;以至于他自己都忘记了每个农场中种植作物的具体数量了&#xff0c;他只记得一些含糊的信息&#xff08;共m个&#xff09;&#xff0c;以下列三种形式描述&#xff1a; 农场a比农场b至少多种植了c个单位…

将Quartz与Spring集成

在Java应用程序中调度作业时&#xff0c;Quartz是第一个考虑的工具。 Quartz是由最流行的RDBMS支持的作业调度程序。 这真的很方便&#xff0c;并且很容易与spring集成。 为了创建石英模式&#xff0c;您必须下载石英发行版并解压缩位于crystal-2.2.3 / docs / dbTables /中的…

linux下md5sum的使用

在linux或Unix上&#xff0c;md5sum是用来计算和校验文件报文摘要的工具程序。一般来说&#xff0c;安装了Linux后&#xff0c;就会有md5sum这个工具&#xff0c;直接在命令行终端直接运行。1、使用md5sum来产生指纹&#xff08;报文摘要&#xff09;命令如下&#xff1a;md5su…