Learn how to programmatically create DynamoDB tables using the AWS SDK for Java.
In this code-along, you'll learn the fundamentals of creating DynamoDB tables programmatically using the AWS SDK for Java. This is an essential skill for applications that need to manage their own database infrastructure.
// Creating a DynamoDB client
DynamoDbClient dynamoDbClient = DynamoDbClient.builder()
.region(Region.US_WEST_2)
.build();
// Defining the table schema
CreateTableRequest request = CreateTableRequest.builder()
.attributeDefinitions(
AttributeDefinition.builder()
.attributeName("artist")
.attributeType(ScalarAttributeType.S)
.build(),
AttributeDefinition.builder()
.attributeName("songTitle")
.attributeType(ScalarAttributeType.S)
.build()
)
.keySchema(
KeySchemaElement.builder()
.attributeName("artist")
.keyType(KeyType.HASH)
.build(),
KeySchemaElement.builder()
.attributeName("songTitle")
.keyType(KeyType.RANGE)
.build()
)
.billingMode(BillingMode.PAY_PER_REQUEST)
.tableName("Music")
.build();
// Create the table
CreateTableResponse response = dynamoDbClient.createTable(request);
System.out.println("Table created: " + response.tableDescription().tableName());
By completing this code-along, you'll gain practical experience with DynamoDB table creation, which is a fundamental skill for working with AWS's NoSQL database service.
Practice making HTTP requests to RESTful APIs using different HTTP methods (GET, POST, PUT, DELETE).
In this code-along, you'll explore how to interact with RESTful APIs by making HTTP requests using different methods. You'll learn how to perform the full range of CRUD operations (Create, Read, Update, Delete) through HTTP.
// Setting up the HTTP client
HttpClient client = HttpClient.newHttpClient();
// Making a GET request
HttpRequest getRequest = HttpRequest.newBuilder()
.uri(URI.create("https://api.example.com/resources/123"))
.header("Accept", "application/json")
.GET()
.build();
HttpResponse<String> getResponse = client.send(getRequest, HttpResponse.BodyHandlers.ofString());
// Making a POST request
String jsonBody = "{\"name\":\"New Resource\",\"description\":\"This is a new resource\"}";
HttpRequest postRequest = HttpRequest.newBuilder()
.uri(URI.create("https://api.example.com/resources"))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(jsonBody))
.build();
HttpResponse<String> postResponse = client.send(postRequest, HttpResponse.BodyHandlers.ofString());
// Handling responses
if (postResponse.statusCode() == 201) {
System.out.println("Resource created successfully!");
System.out.println("Response body: " + postResponse.body());
} else {
System.out.println("Error: " + postResponse.statusCode());
System.out.println("Error message: " + postResponse.body());
}
This code-along will give you practical experience making API requests, which is essential for building modern applications that interact with external services or microservices.