使用Java创建DynamoDB表
在這篇文章中,我們將使用java方法在DynamoDB數(shù)據(jù)庫上創(chuàng)建表。 在開始之前,我們需要安裝本地dynamodb,因為我們要避免使用dynamodb的任何費用。 有一個以前的崗位上本地dynamodb。
如果您使用docker,則可以找到本地dynamodb映像,也可以按照此處所述自行創(chuàng)建一個。 dynamodb java sdk使我們能夠使用java代碼創(chuàng)建dynamodb表。
最基本的操作是使用哈希鍵創(chuàng)建表。 在這種情況下,用戶的電子郵件將是哈希密鑰。
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);我們所做的是使用他的電子郵件作為哈希鍵創(chuàng)建Users表。 下表稱為“登錄名”。 每次用戶登錄時,登錄都應(yīng)保持跟蹤。除了使用哈希鍵之外,我們還將使用范圍鍵。
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);通過使用電子郵件作為哈希鍵,我們可以查詢特定用戶的登錄名。 通過使用登錄發(fā)生的日期作為范圍鍵,可以查找登錄條目的排序或基于特定用戶的登錄日期執(zhí)行高級查詢。
但是,在大多數(shù)情況下,哈希鍵和范圍鍵不足以滿足我們的需求。 DynamoDB為我們提供了全局二級索引和本地二級索引。
我們將創(chuàng)建表SupervisorS。 Supervisor的哈希鍵將是他的名字。 主管將為公司工作。 該公司將成為我們的全球二級指數(shù)。 由于公司擁有多個工廠,因此實地工廠將成為范圍的關(guān)鍵。
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);下一個表將是公司表。 哈希鍵將是母公司,范圍鍵將是子公司。 每個公司都有一位首席執(zhí)行官。 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
總結(jié)
以上是生活随笔為你收集整理的使用Java创建DynamoDB表的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ddos怎么查看ip(ddos怎么查看)
- 下一篇: FizzBuzz Kata与Java