public static void createTable()

in javav2/example_code/dynamodb/src/main/java/com/example/dynamodb/EnhancedScanRecordsWithExpression.java [284:428]


    public static void createTable(DynamoDbClient ddb, String tableName) {

        try {
            // Attribute definitions
            ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();

            // Define attributes
            attributeDefinitions.add(AttributeDefinition.builder()
                    .attributeName("issueId")
                    .attributeType("S")
                    .build());

            attributeDefinitions.add(AttributeDefinition.builder()
                    .attributeName("title")
                    .attributeType("S")
                    .build());

            attributeDefinitions.add(AttributeDefinition.builder()
                    .attributeName("createDate")
                    .attributeType("S")
                    .build());

            attributeDefinitions.add(AttributeDefinition.builder()
                    .attributeName("dueDate")
                    .attributeType("S")
                    .build());

            ArrayList<KeySchemaElement> tableKey = new ArrayList<KeySchemaElement>();
            KeySchemaElement key = KeySchemaElement.builder()
                    .attributeName("issueId")
                    .keyType(KeyType.HASH)
                    .build();

            KeySchemaElement key2 = KeySchemaElement.builder()
                    .attributeName("title")
                    .keyType(KeyType.RANGE) // Sort
                    .build();

            // Add KeySchemaElement objects to the list.
            tableKey.add(key);
            tableKey.add(key2);

            // Create a ProvisionedThroughput object.
            ProvisionedThroughput ptIndex = ProvisionedThroughput.builder()
                    .readCapacityUnits(1L)
                    .writeCapacityUnits(1L)
                    .build();

            KeySchemaElement keyDate = KeySchemaElement.builder()
                    .attributeName("createDate")
                    .keyType(KeyType.HASH)
                    .build();

            KeySchemaElement keyIssues = KeySchemaElement.builder()
                    .attributeName("issueId")
                    .keyType(KeyType.RANGE)
                    .build();

            List<KeySchemaElement> keySchema = new ArrayList<KeySchemaElement>();
            keySchema.add(keyDate);
            keySchema.add(keyIssues);

            Projection projection =  Projection.builder()
                    .projectionType("INCLUDE")
                    .nonKeyAttributes("description", "status")
                    .projectionType("INCLUDE")
                    .build();

            GlobalSecondaryIndex createDateIndex = GlobalSecondaryIndex.builder()
                    .indexName("createDateIndex")
                    .provisionedThroughput(ptIndex)
                    .keySchema(keySchema)
                    .projection(projection)
                    .build();

            KeySchemaElement keySchemaTitle = KeySchemaElement.builder()
                    .attributeName("title")
                    .keyType(KeyType.HASH)
                    .build();

            KeySchemaElement keySchemaIssueId = KeySchemaElement.builder()
                    .attributeName("issueId")
                    .keyType(KeyType.RANGE)
                    .build();

            List<KeySchemaElement> keySchemaCol2 = new ArrayList<KeySchemaElement>();
            keySchemaCol2.add(keySchemaTitle);
            keySchemaCol2.add(keySchemaIssueId);


            GlobalSecondaryIndex titleIndex = GlobalSecondaryIndex.builder()
                    .indexName("titleIndex")
                    .provisionedThroughput(ptIndex)
                    .keySchema(keySchemaCol2)
                    .projection(Projection.builder().projectionType("KEYS_ONLY").build())
                    .build();

            KeySchemaElement keySchemaDueDate = KeySchemaElement.builder()
                    .attributeName("dueDate")
                    .keyType(KeyType.HASH)
                    .build();

            List<KeySchemaElement> keySchemaCol3 = new ArrayList<KeySchemaElement>();
            keySchemaCol3.add(keySchemaDueDate);

            GlobalSecondaryIndex dueDateIndex = GlobalSecondaryIndex.builder()
                    .indexName("dueDateIndex")
                    .provisionedThroughput(ptIndex)
                    .keySchema(keySchemaCol3)
                    .projection(projection)
                    .build();

            List<GlobalSecondaryIndex> globalIndex = new ArrayList<>();
            globalIndex.add(createDateIndex);
            globalIndex.add(dueDateIndex);
            globalIndex.add(titleIndex);

            CreateTableRequest request = CreateTableRequest.builder()
                    .keySchema(tableKey)
                    .provisionedThroughput(ProvisionedThroughput.builder()
                            .readCapacityUnits(new Long(10))
                            .writeCapacityUnits(new Long(10))
                            .build())
                    .globalSecondaryIndexes(globalIndex)
                    .attributeDefinitions(attributeDefinitions)
                    .tableName(tableName)
                    .build();

            System.out.println("Creating table " + tableName + "...");

            DynamoDbWaiter dbWaiter = ddb.waiter();
            CreateTableResponse response =  ddb.createTable(request);

            // Wait until the table is created.
            WaiterResponse<DescribeTableResponse> waiterResponse =  dbWaiter.waitUntilTableExists(r -> r.tableName(tableName));
            waiterResponse.matched().response().ifPresent(System.out::println);

            String newTable = response.tableDescription().tableName();
            System.out.println("Table " + tableName + " is created");

        } catch (DynamoDbException e) {
            System.err.println(e.getMessage());
            System.exit(1);
        }
    }