Search in sources :

Example 1 with EnhancedGlobalSecondaryIndex

use of software.amazon.awssdk.enhanced.dynamodb.model.EnhancedGlobalSecondaryIndex in project aws-sdk-java-v2 by aws.

the class CreateTableOperationTest method generateRequest_withLsiAndGsi.

@Test
public void generateRequest_withLsiAndGsi() {
    Projection projection1 = Projection.builder().projectionType(ProjectionType.ALL).build();
    Projection projection2 = Projection.builder().projectionType(ProjectionType.KEYS_ONLY).build();
    Projection projection3 = Projection.builder().projectionType(ProjectionType.INCLUDE).nonKeyAttributes("key1", "key2").build();
    ProvisionedThroughput provisionedThroughput1 = ProvisionedThroughput.builder().readCapacityUnits(1L).writeCapacityUnits(2L).build();
    ProvisionedThroughput provisionedThroughput2 = ProvisionedThroughput.builder().readCapacityUnits(3L).writeCapacityUnits(4L).build();
    List<EnhancedGlobalSecondaryIndex> globalSecondaryIndexList = Arrays.asList(EnhancedGlobalSecondaryIndex.builder().indexName("gsi_1").projection(projection1).provisionedThroughput(provisionedThroughput1).build(), EnhancedGlobalSecondaryIndex.builder().indexName("gsi_2").projection(projection2).provisionedThroughput(provisionedThroughput2).build());
    CreateTableOperation<FakeItemWithIndices> operation = CreateTableOperation.create(CreateTableEnhancedRequest.builder().globalSecondaryIndices(globalSecondaryIndexList).localSecondaryIndices(Collections.singletonList(EnhancedLocalSecondaryIndex.create("lsi_1", projection3))).build());
    CreateTableRequest request = operation.generateRequest(FakeItemWithIndices.getTableSchema(), PRIMARY_CONTEXT, null);
    assertThat(request.tableName(), is(TABLE_NAME));
    assertThat(request.keySchema(), containsInAnyOrder(KeySchemaElement.builder().attributeName("id").keyType(HASH).build(), KeySchemaElement.builder().attributeName("sort").keyType(RANGE).build()));
    software.amazon.awssdk.services.dynamodb.model.GlobalSecondaryIndex expectedGsi1 = software.amazon.awssdk.services.dynamodb.model.GlobalSecondaryIndex.builder().indexName("gsi_1").keySchema(KeySchemaElement.builder().attributeName("gsi_id").keyType(HASH).build(), KeySchemaElement.builder().attributeName("gsi_sort").keyType(RANGE).build()).projection(projection1).provisionedThroughput(provisionedThroughput1).build();
    software.amazon.awssdk.services.dynamodb.model.GlobalSecondaryIndex expectedGsi2 = software.amazon.awssdk.services.dynamodb.model.GlobalSecondaryIndex.builder().indexName("gsi_2").keySchema(KeySchemaElement.builder().attributeName("gsi_id").keyType(HASH).build()).projection(projection2).provisionedThroughput(provisionedThroughput2).build();
    assertThat(request.globalSecondaryIndexes(), containsInAnyOrder(matchesGsi(expectedGsi1), matchesGsi(expectedGsi2)));
    software.amazon.awssdk.services.dynamodb.model.LocalSecondaryIndex expectedLsi = software.amazon.awssdk.services.dynamodb.model.LocalSecondaryIndex.builder().indexName("lsi_1").keySchema(KeySchemaElement.builder().attributeName("id").keyType(HASH).build(), KeySchemaElement.builder().attributeName("lsi_sort").keyType(RANGE).build()).projection(projection3).build();
    assertThat(request.localSecondaryIndexes(), containsInAnyOrder(expectedLsi));
    assertThat(request.attributeDefinitions(), containsInAnyOrder(AttributeDefinition.builder().attributeName("id").attributeType(ScalarAttributeType.S).build(), AttributeDefinition.builder().attributeName("sort").attributeType(ScalarAttributeType.S).build(), AttributeDefinition.builder().attributeName("lsi_sort").attributeType(ScalarAttributeType.S).build(), AttributeDefinition.builder().attributeName("gsi_id").attributeType(ScalarAttributeType.S).build(), AttributeDefinition.builder().attributeName("gsi_sort").attributeType(ScalarAttributeType.S).build()));
}
Also used : Projection(software.amazon.awssdk.services.dynamodb.model.Projection) ProvisionedThroughput(software.amazon.awssdk.services.dynamodb.model.ProvisionedThroughput) EnhancedGlobalSecondaryIndex(software.amazon.awssdk.enhanced.dynamodb.model.EnhancedGlobalSecondaryIndex) FakeItemWithIndices(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItemWithIndices) CreateTableRequest(software.amazon.awssdk.services.dynamodb.model.CreateTableRequest) Test(org.junit.Test)

Example 2 with EnhancedGlobalSecondaryIndex

use of software.amazon.awssdk.enhanced.dynamodb.model.EnhancedGlobalSecondaryIndex in project aws-sdk-java-v2 by aws.

the class CreateTableOperationTest method generateRequest_validLsiAsGsiReference.

@Test
public void generateRequest_validLsiAsGsiReference() {
    List<EnhancedGlobalSecondaryIndex> validLsiList = Collections.singletonList(EnhancedGlobalSecondaryIndex.builder().indexName("lsi_1").projection(p -> p.projectionType(ProjectionType.ALL)).provisionedThroughput(p -> p.readCapacityUnits(1L).writeCapacityUnits(1L)).build());
    CreateTableOperation<FakeItemWithIndices> operation = CreateTableOperation.create(CreateTableEnhancedRequest.builder().globalSecondaryIndices(validLsiList).build());
    CreateTableRequest request = operation.generateRequest(FakeItemWithIndices.getTableSchema(), PRIMARY_CONTEXT, null);
    assertThat(request.globalSecondaryIndexes().size(), is(1));
    software.amazon.awssdk.services.dynamodb.model.GlobalSecondaryIndex globalSecondaryIndex = request.globalSecondaryIndexes().get(0);
    assertThat(globalSecondaryIndex.indexName(), is("lsi_1"));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) FakeItemWithIndices(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItemWithIndices) Arrays(java.util.Arrays) FakeItemWithByteBufferKey(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItemWithByteBufferKey) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) FakeItemWithNumericSort(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItemWithNumericSort) ProvisionedThroughput(software.amazon.awssdk.services.dynamodb.model.ProvisionedThroughput) RANGE(software.amazon.awssdk.services.dynamodb.model.KeyType.RANGE) CreateTableRequest(software.amazon.awssdk.services.dynamodb.model.CreateTableRequest) FakeItem(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ScalarAttributeType(software.amazon.awssdk.services.dynamodb.model.ScalarAttributeType) DefaultSdkAutoConstructList(software.amazon.awssdk.core.util.DefaultSdkAutoConstructList) Description(org.hamcrest.Description) DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient) Matchers.empty(org.hamcrest.Matchers.empty) ProjectionType(software.amazon.awssdk.services.dynamodb.model.ProjectionType) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) HASH(software.amazon.awssdk.services.dynamodb.model.KeyType.HASH) AttributeDefinition(software.amazon.awssdk.services.dynamodb.model.AttributeDefinition) EnhancedLocalSecondaryIndex(software.amazon.awssdk.enhanced.dynamodb.model.EnhancedLocalSecondaryIndex) TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) Mockito.verify(org.mockito.Mockito.verify) List(java.util.List) TableMetadata(software.amazon.awssdk.enhanced.dynamodb.TableMetadata) KeySchemaElement(software.amazon.awssdk.services.dynamodb.model.KeySchemaElement) CreateTableEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.CreateTableEnhancedRequest) BillingMode(software.amazon.awssdk.services.dynamodb.model.BillingMode) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) Matchers.sameInstance(org.hamcrest.Matchers.sameInstance) OperationContext(software.amazon.awssdk.enhanced.dynamodb.OperationContext) Projection(software.amazon.awssdk.services.dynamodb.model.Projection) CreateTableResponse(software.amazon.awssdk.services.dynamodb.model.CreateTableResponse) Matchers.is(org.hamcrest.Matchers.is) EnhancedGlobalSecondaryIndex(software.amazon.awssdk.enhanced.dynamodb.model.EnhancedGlobalSecondaryIndex) Collections(java.util.Collections) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) ArgumentMatchers.same(org.mockito.ArgumentMatchers.same) FakeItemWithBinaryKey(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItemWithBinaryKey) EnhancedGlobalSecondaryIndex(software.amazon.awssdk.enhanced.dynamodb.model.EnhancedGlobalSecondaryIndex) FakeItemWithIndices(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItemWithIndices) CreateTableRequest(software.amazon.awssdk.services.dynamodb.model.CreateTableRequest) Test(org.junit.Test)

Example 3 with EnhancedGlobalSecondaryIndex

use of software.amazon.awssdk.enhanced.dynamodb.model.EnhancedGlobalSecondaryIndex in project aws-sdk-java-v2 by aws.

the class CreateTableOperationTest method generateRequest_invalidGsi.

@Test(expected = IllegalArgumentException.class)
public void generateRequest_invalidGsi() {
    ProvisionedThroughput provisionedThroughput = ProvisionedThroughput.builder().readCapacityUnits(1L).writeCapacityUnits(1L).build();
    List<EnhancedGlobalSecondaryIndex> invalidGsiList = Collections.singletonList(EnhancedGlobalSecondaryIndex.builder().indexName("invalid").projection(p -> p.projectionType(ProjectionType.ALL)).provisionedThroughput(provisionedThroughput).build());
    CreateTableOperation<FakeItem> operation = CreateTableOperation.create(CreateTableEnhancedRequest.builder().globalSecondaryIndices(invalidGsiList).build());
    operation.generateRequest(FakeItem.getTableSchema(), PRIMARY_CONTEXT, null);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) FakeItemWithIndices(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItemWithIndices) Arrays(java.util.Arrays) FakeItemWithByteBufferKey(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItemWithByteBufferKey) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) FakeItemWithNumericSort(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItemWithNumericSort) ProvisionedThroughput(software.amazon.awssdk.services.dynamodb.model.ProvisionedThroughput) RANGE(software.amazon.awssdk.services.dynamodb.model.KeyType.RANGE) CreateTableRequest(software.amazon.awssdk.services.dynamodb.model.CreateTableRequest) FakeItem(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ScalarAttributeType(software.amazon.awssdk.services.dynamodb.model.ScalarAttributeType) DefaultSdkAutoConstructList(software.amazon.awssdk.core.util.DefaultSdkAutoConstructList) Description(org.hamcrest.Description) DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient) Matchers.empty(org.hamcrest.Matchers.empty) ProjectionType(software.amazon.awssdk.services.dynamodb.model.ProjectionType) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) HASH(software.amazon.awssdk.services.dynamodb.model.KeyType.HASH) AttributeDefinition(software.amazon.awssdk.services.dynamodb.model.AttributeDefinition) EnhancedLocalSecondaryIndex(software.amazon.awssdk.enhanced.dynamodb.model.EnhancedLocalSecondaryIndex) TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) Mockito.verify(org.mockito.Mockito.verify) List(java.util.List) TableMetadata(software.amazon.awssdk.enhanced.dynamodb.TableMetadata) KeySchemaElement(software.amazon.awssdk.services.dynamodb.model.KeySchemaElement) CreateTableEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.CreateTableEnhancedRequest) BillingMode(software.amazon.awssdk.services.dynamodb.model.BillingMode) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) Matchers.sameInstance(org.hamcrest.Matchers.sameInstance) OperationContext(software.amazon.awssdk.enhanced.dynamodb.OperationContext) Projection(software.amazon.awssdk.services.dynamodb.model.Projection) CreateTableResponse(software.amazon.awssdk.services.dynamodb.model.CreateTableResponse) Matchers.is(org.hamcrest.Matchers.is) EnhancedGlobalSecondaryIndex(software.amazon.awssdk.enhanced.dynamodb.model.EnhancedGlobalSecondaryIndex) Collections(java.util.Collections) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) ArgumentMatchers.same(org.mockito.ArgumentMatchers.same) FakeItemWithBinaryKey(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItemWithBinaryKey) ProvisionedThroughput(software.amazon.awssdk.services.dynamodb.model.ProvisionedThroughput) EnhancedGlobalSecondaryIndex(software.amazon.awssdk.enhanced.dynamodb.model.EnhancedGlobalSecondaryIndex) FakeItem(software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItem) Test(org.junit.Test)

Example 4 with EnhancedGlobalSecondaryIndex

use of software.amazon.awssdk.enhanced.dynamodb.model.EnhancedGlobalSecondaryIndex in project serve by pytorch.

the class DDBSnapshotSerializerTest method createTable.

private void createTable() {
    ProvisionedThroughput provThroughput = ProvisionedThroughput.builder().readCapacityUnits(10L).writeCapacityUnits(5L).build();
    Projection projectAll = Projection.builder().projectionType(ProjectionType.ALL).build();
    EnhancedGlobalSecondaryIndex gsi = EnhancedGlobalSecondaryIndex.builder().indexName("createdOnMonth-index").projection(projectAll).provisionedThroughput(provThroughput).build();
    CreateTableEnhancedRequest createTableReq = CreateTableEnhancedRequest.builder().globalSecondaryIndices(Arrays.asList(gsi)).build();
    snapshotTable.createTable(createTableReq);
}
Also used : Projection(software.amazon.awssdk.services.dynamodb.model.Projection) ProvisionedThroughput(software.amazon.awssdk.services.dynamodb.model.ProvisionedThroughput) EnhancedGlobalSecondaryIndex(software.amazon.awssdk.enhanced.dynamodb.model.EnhancedGlobalSecondaryIndex) CreateTableEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.CreateTableEnhancedRequest)

Example 5 with EnhancedGlobalSecondaryIndex

use of software.amazon.awssdk.enhanced.dynamodb.model.EnhancedGlobalSecondaryIndex in project micronaut-aws-sdk by agorapulse.

the class DefaultDynamoDbService method createTable.

@Override
public void createTable() {
    Map<String, ProjectionType> types = getProjectionTypes();
    TableMetadata tableMetadata = table.tableSchema().tableMetadata();
    table.createTable(b -> {
        List<EnhancedLocalSecondaryIndex> localSecondaryIndices = new ArrayList<>();
        List<EnhancedGlobalSecondaryIndex> globalSecondaryIndices = new ArrayList<>();
        tableMetadata.indices().forEach(i -> {
            if (TableMetadata.primaryIndexName().equals(i.name())) {
                return;
            }
            ProjectionType type = types.getOrDefault(i.name(), ProjectionType.KEYS_ONLY);
            if (tableMetadata.primaryPartitionKey().equals(tableMetadata.indexPartitionKey(i.name()))) {
                localSecondaryIndices.add(EnhancedLocalSecondaryIndex.create(i.name(), Projection.builder().projectionType(type).build()));
            } else {
                globalSecondaryIndices.add(EnhancedGlobalSecondaryIndex.builder().indexName(i.name()).projection(Projection.builder().projectionType(type).build()).build());
            }
        });
        if (!localSecondaryIndices.isEmpty()) {
            b.localSecondaryIndices(localSecondaryIndices);
        }
        if (!globalSecondaryIndices.isEmpty()) {
            b.globalSecondaryIndices(globalSecondaryIndices);
        }
    });
}
Also used : TableMetadata(software.amazon.awssdk.enhanced.dynamodb.TableMetadata) EnhancedLocalSecondaryIndex(software.amazon.awssdk.enhanced.dynamodb.model.EnhancedLocalSecondaryIndex) ArrayList(java.util.ArrayList) EnhancedGlobalSecondaryIndex(software.amazon.awssdk.enhanced.dynamodb.model.EnhancedGlobalSecondaryIndex) ProjectionType(software.amazon.awssdk.services.dynamodb.model.ProjectionType)

Aggregations

EnhancedGlobalSecondaryIndex (software.amazon.awssdk.enhanced.dynamodb.model.EnhancedGlobalSecondaryIndex)5 Projection (software.amazon.awssdk.services.dynamodb.model.Projection)4 ProvisionedThroughput (software.amazon.awssdk.services.dynamodb.model.ProvisionedThroughput)4 Test (org.junit.Test)3 TableMetadata (software.amazon.awssdk.enhanced.dynamodb.TableMetadata)3 FakeItemWithIndices (software.amazon.awssdk.enhanced.dynamodb.functionaltests.models.FakeItemWithIndices)3 CreateTableEnhancedRequest (software.amazon.awssdk.enhanced.dynamodb.model.CreateTableEnhancedRequest)3 EnhancedLocalSecondaryIndex (software.amazon.awssdk.enhanced.dynamodb.model.EnhancedLocalSecondaryIndex)3 CreateTableRequest (software.amazon.awssdk.services.dynamodb.model.CreateTableRequest)3 ProjectionType (software.amazon.awssdk.services.dynamodb.model.ProjectionType)3 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 List (java.util.List)2 Description (org.hamcrest.Description)2 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)2 Matchers.containsInAnyOrder (org.hamcrest.Matchers.containsInAnyOrder)2 Matchers.empty (org.hamcrest.Matchers.empty)2 Matchers.is (org.hamcrest.Matchers.is)2 Matchers.sameInstance (org.hamcrest.Matchers.sameInstance)2 TypeSafeMatcher (org.hamcrest.TypeSafeMatcher)2