← Back to Home

Code-Alongs

Code-Along 1: Create DynamoDB Table through Java Code

Learn how to programmatically create DynamoDB tables using the AWS SDK for Java.

What You'll Learn

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.

Key Concepts Covered:

  • Setting up the AWS SDK for Java in your project
  • Creating a DynamoDB client
  • Defining table schemas with partition and sort keys
  • Configuring throughput capacity for DynamoDB tables
  • Managing table creation processes and handling responses
  • Working with table creation waiting policies

Code Example Highlights:

// 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.

Code-Along 2: HTTP Methods and REST API Calls

Practice making HTTP requests to RESTful APIs using different HTTP methods (GET, POST, PUT, DELETE).

What You'll Learn

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.

Key Concepts Covered:

  • Understanding HTTP methods (GET, POST, PUT, DELETE) and their appropriate use cases
  • Using Java's HttpClient to make API requests
  • Working with JSON request and response bodies
  • Handling different HTTP status codes and error responses
  • Implementing proper error handling for API calls
  • Testing API endpoints using different HTTP methods

Code Example Highlights:

// 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.