在这篇文章中,我们将使用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 key
indexKeySchema.add(new KeySchemaElement()
.withAttributeName("factory")
.withKeyType(KeyType.RANGE)); //Sort key
GlobalSecondaryIndex 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