广州行业网站建设,百家号如何给网站做推广,广告页面模板网站,缙云建设局网上协同办公oa网站在这篇文章中#xff0c;我们将使用java方法在DynamoDB数据库上创建表。 在开始之前#xff0c;我们需要安装本地dynamodb#xff0c;因为我们要避免使用dynamodb的任何费用。 有一个以前的岗位上本地dynamodb。 如果您使用docker#xff0c;则可以找到本地dynamodb映像我们将使用java方法在DynamoDB数据库上创建表。 在开始之前我们需要安装本地dynamodb因为我们要避免使用dynamodb的任何费用。 有一个以前的岗位上本地dynamodb。 如果您使用docker则可以找到本地dynamodb映像也可以按照此处所述自行创建一个。 dynamodb java sdk使我们能够使用java代码创建dynamodb表。 最基本的操作是使用哈希键创建表。 在这种情况下用户的电子邮件将是哈希密钥。 ListKeySchemaElement elements new ArrayListKeySchemaElement();KeySchemaElement keySchemaElement new KeySchemaElement().withKeyType(KeyType.HASH).withAttributeName(email);elements.add(keySchemaElement);ListAttributeDefinition 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表。 下表称为“登录名”。 每次用户登录时登录都应保持跟踪。除了使用哈希键之外我们还将使用范围键。 ListKeySchemaElement elements new ArrayListKeySchemaElement();KeySchemaElement hashKey new KeySchemaElement().withKeyType(KeyType.HASH).withAttributeName(email);KeySchemaElement rangeKey new KeySchemaElement().withKeyType(KeyType.RANGE).withAttributeName(timestamp);elements.add(hashKey);elements.add(rangeKey);ListAttributeDefinition 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的哈希键将是他的名字。 主管将为公司工作。 该公司将成为我们的全球二级指数。 由于公司拥有多个工厂因此实地工厂将成为范围的关键。 ListKeySchemaElement elements new ArrayList();KeySchemaElement hashKey new KeySchemaElement().withKeyType(KeyType.HASH).withAttributeName(name);elements.add(hashKey);ListGlobalSecondaryIndex globalSecondaryIndices new ArrayList();ArrayListKeySchemaElement 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);ListAttributeDefinition 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将是本地二级索引的范围键。 ListKeySchemaElement 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);ListLocalSecondaryIndex localSecondaryIndices new ArrayList();ArrayListKeySchemaElement 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);ListAttributeDefinition 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