-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
CreateTableCompositeKey.java
executable file
·95 lines (84 loc) · 3.95 KB
/
CreateTableCompositeKey.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package com.example.dynamodb;
// snippet-start:[dynamodb.java2.create_table_composite_key.main]
// snippet-start:[dynamodb.java2.create_table_composite_key.import]
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.dynamodb.model.DynamoDbException;
import software.amazon.awssdk.services.dynamodb.model.AttributeDefinition;
import software.amazon.awssdk.services.dynamodb.model.CreateTableRequest;
import software.amazon.awssdk.services.dynamodb.model.CreateTableResponse;
import software.amazon.awssdk.services.dynamodb.model.KeySchemaElement;
import software.amazon.awssdk.services.dynamodb.model.KeyType;
import software.amazon.awssdk.services.dynamodb.model.ProvisionedThroughput;
import software.amazon.awssdk.services.dynamodb.model.ScalarAttributeType;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
// snippet-end:[dynamodb.java2.create_table_composite_key.import]
/**
* Before running this Java V2 code example, set up your development
* environment, including your credentials.
*
* For more information, see the following documentation topic:
*
* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
*/
public class CreateTableCompositeKey {
public static void main(String[] args) {
final String usage = """
Usage:
<tableName>
Where:
tableName - The Amazon DynamoDB table to create (for example, Music3).
""";
if (args.length != 1) {
System.out.println(usage);
System.exit(1);
}
String tableName = args[0];
Region region = Region.US_EAST_1;
DynamoDbClient ddb = DynamoDbClient.builder()
.region(region)
.build();
System.out.format("Creating Amazon DynamoDB table %s\n with a composite primary key:\n", tableName);
System.out.format("* Language - partition key\n");
System.out.format("* Greeting - sort key\n");
String tableId = createTableComKey(ddb, tableName);
System.out.println("The Amazon DynamoDB table Id value is " + tableId);
ddb.close();
}
public static String createTableComKey(DynamoDbClient ddb, String tableName) {
CreateTableRequest request = CreateTableRequest.builder()
.attributeDefinitions(AttributeDefinition.builder()
.attributeName("Language")
.attributeType(ScalarAttributeType.S)
.build(),
AttributeDefinition.builder()
.attributeName("Greeting")
.attributeType(ScalarAttributeType.S)
.build())
.keySchema(KeySchemaElement.builder()
.attributeName("Language")
.keyType(KeyType.HASH)
.build(),
KeySchemaElement.builder()
.attributeName("Greeting")
.keyType(KeyType.RANGE)
.build())
.provisionedThroughput(ProvisionedThroughput.builder()
.readCapacityUnits(10L)
.writeCapacityUnits(10L).build())
.tableName(tableName)
.build();
String tableId;
try {
CreateTableResponse result = ddb.createTable(request);
tableId = result.tableDescription().tableId();
return tableId;
} catch (DynamoDbException e) {
System.err.println(e.getMessage());
System.exit(1);
}
return "";
}
}
// snippet-end:[dynamodb.java2.create_table_composite_key.main]