Skip to content

Commit

Permalink
Merge pull request #1273 from scmacdon/master
Browse files Browse the repository at this point in the history
Add Java V2 Examples for the AWS Firehose service
  • Loading branch information
pccornel committed Jul 16, 2020
2 parents 67f2ff4 + 44e9238 commit 48122cb
Show file tree
Hide file tree
Showing 13 changed files with 1,016 additions and 0 deletions.
83 changes: 83 additions & 0 deletions javav2/example_code/firehose/Readme.md
@@ -0,0 +1,83 @@
# Amazon Kinesis Data Firehose Java code examples

This README discusses how to run and test the Java code examples for Amazon Kinesis Data Firehose.

## Running the Amazon Kinesis Data Firehose Java files

**IMPORTANT**

The Java examples perform AWS operations for the account and AWS Region for which you've specified credentials, and you may incur AWS service charges by running them. See the [AWS Pricing page](https://aws.amazon.com/pricing/) for details about the charges you can expect for a given service and operation.

Some of these examples perform *destructive* operations on AWS resources, such as deleting a delivery stream by running the **DeleteStream** example. **Be very careful** when running an operation that deletes or modifies AWS resources in your account. It's best to create separate test-only resources when experimenting with these examples.

To run these examples, you'll need the AWS SDK for Java libraries in your **CLASSPATH**.

export CLASSPATH=target/sdk-examples-1.0.jar:/path/to/aws-java-sdk/<jar-file-name>.jar

Here **/path/to/aws-java-sdk/<jar-file-name>.jar** is the path to where you extracted or built the AWS SDK for Java JAR file.

For systems with Bash support, once you set the **CLASSPATH**, you can run a particular example as follows.

java com.example.firehose.ListDeliveryStreams


## Testing the Kinesis Data Firehose Java files

You can test the Java code examples for Kinesis Data Firehose by running a test file named **AmazonFirehoseServiceIntegrationTest**. This file uses JUnit 5 to run the JUnit tests and is located in the **src/test/java** folder. For more information, see [https://junit.org/junit5/](https://junit.org/junit5/).

You can run the JUnit tests from a Java IDE, such as IntelliJ, or from the command line by using Maven. As each test runs, you can view messages that inform you if the various tests succeed or fail. For example, the following message informs you that Test 3 passed.

Test 3 passed

**WARNING**: _Running these JUnit tests manipulates real Amazon resources and may incur charges on your account._

### Properties file
Before running the Kinesis Data Firehose JUnit tests, you must define values in the **config.properties** file located in the **resources** folder. This file contains values that are required to run the JUnit tests. For example, you define a delivery stream name used in the tests. If you do not define all values, the JUnit tests fail.

Define these values to successfully run the JUnit tests:

- **bucketARN** - The Amazon Resource Name (ARN) of a bucket where data streams are written.
- **roleARN** - An Amazon Resource Name (ARN) of an iAM Role that has permissions that Kinesis Data Firehose needs .
- **newStream** - The name of a data stream to create.
- **existingStream** - An existing data stream.
- **textValue** - Text to use as a record for the **PutRecord** test.

### Command line
To run the JUnit tests from the command line, you can use the following command.

mvn test

You will see output from the JUnit tests, as shown here.

[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running AmazonFirehoseServiceIntegrationTest
Test 1 passed
Test 2 passed
...
Done!
[INFO] Results:
[INFO]
[INFO] Tests run: 7, Failures: 0, Errors: 0, Skipped: 0
[INFO]
INFO] --------------------------------------------
[INFO] BUILD SUCCESS
[INFO]--------------------------------------------
[INFO] Total time: 12.003 s
[INFO] Finished at: 2020-02-10T14:25:08-05:00
[INFO] --------------------------------------------

### Unsuccessful tests

If you do not define the correct values in the properties file, your JUnit tests are not successful. You will see an error message such as the following. You need to double-check the values that you set in the properties file and run the tests again.

[INFO]
[INFO] --------------------------------------
[INFO] BUILD FAILURE
[INFO] --------------------------------------
[INFO] Total time: 19.038 s
[INFO] Finished at: 2020-02-10T14:41:51-05:00
[INFO] ---------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.1:test (default-test) on project S3J2Project: There are test failures.
[ERROR];
20 changes: 20 additions & 0 deletions javav2/example_code/firehose/metadata.yaml
@@ -0,0 +1,20 @@
---
files:
- path: src/main/java/com/example/firehose/CreateDeliveryStream.java
services:
- firehose
- path: src/main/java/com/example/firehose/DeleteStream.java
services:
- firehose
- path: src/main/java/com/example/firehose/ListDeliveryStreams.java
services:
- firehose
- path: src/main/java/com/example/firehose/PutBatchRecords.java
services:
- firehose
- path: src/test/java/example/firehose/PutRecord.java
services:
- firehose
- path: src/test/java/AmazonFirehoseServiceIntegrationTest.java
services:
- firehose
86 changes: 86 additions & 0 deletions javav2/example_code/firehose/pom.xml
@@ -0,0 +1,86 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>FirehoseJ2</groupId>
<artifactId>FirehoseJ2</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bom</artifactId>
<version>2.11.11</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>dynamodb-enhanced</artifactId>
<version>2.11.4-PREVIEW</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.4.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.4.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-commons</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>firehose</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,86 @@
//snippet-sourcedescription:[CreateDeliveryStream.java demonstrates how to create a delivery stream.]
//snippet-keyword:[SDK for Java 2.0]
//snippet-keyword:[Code Sample]
//snippet-service:[Amazon Kinesis Data Firehose]
//snippet-sourcetype:[full-example]
//snippet-sourcedate:[7/6/2020]
//snippet-sourceauthor:[scmacdon - aws]

/*
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
This file is licensed under the Apache License, Version 2.0 (the "License").
You may not use this file except in compliance with the License. A copy of
the License is located at
http://aws.amazon.com/apache2.0/
This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
*/

package com.example.firehose;

// snippet-start:[firehose.java2.create_stream.import]
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.firehose.FirehoseClient;
import software.amazon.awssdk.services.firehose.model.FirehoseException;
import software.amazon.awssdk.services.firehose.model.CreateDeliveryStreamRequest;
import software.amazon.awssdk.services.firehose.model.ExtendedS3DestinationConfiguration;
import software.amazon.awssdk.services.firehose.model.CreateDeliveryStreamResponse;
// snippet-end:[firehose.java2.create_stream.import]

public class CreateDeliveryStream {

public static void main(String[] args) {

final String USAGE = "\n" +
"Usage:\n" +
" CreateDeliveryStream <bucketARN> <roleARN> <streamName> \n\n" +
"Where:\n" +
" bucketARN - The Amazon Resource Name (ARN) of the bucket where the delivery stream is written \n\n" +
" roleARN - The ARN of the role that has the permissions that Amazon Kinesis Data Firehose needs \n" +
" streamName - The delivery stream name \n";

if (args.length < 3) {
System.out.println(USAGE);
System.exit(1);
}

String bucketARN = args[0];
String roleARN = args[1];
String streamName = args[2];

Region region = Region.US_WEST_2;
FirehoseClient firehoseClient = FirehoseClient.builder()
.region(region)
.build();

createStream(firehoseClient, bucketARN, roleARN, streamName) ;
}

// snippet-start:[firehose.java2.create_stream.main]
public static void createStream(FirehoseClient firehoseClient, String bucketARN, String roleARN, String streamName) {

try {

ExtendedS3DestinationConfiguration destinationConfiguration = ExtendedS3DestinationConfiguration.builder()
.bucketARN(bucketARN)
.roleARN(roleARN)
.build();

CreateDeliveryStreamRequest deliveryStreamRequest = CreateDeliveryStreamRequest.builder()
.deliveryStreamName(streamName)
.extendedS3DestinationConfiguration(destinationConfiguration)
.deliveryStreamType("DirectPut")
.build();

CreateDeliveryStreamResponse streamResponse = firehoseClient.createDeliveryStream(deliveryStreamRequest);

System.out.println("Delivery stream ARN is "+streamResponse.deliveryStreamARN());

} catch (FirehoseException e) {
System.out.println(e.getLocalizedMessage());
System.exit(1);
}
// snippet-end:[firehose.java2.create_stream.main]
}
}
@@ -0,0 +1,71 @@
//snippet-sourcedescription:[DeleteStream.java demonstrates how to delete a delivery stream.]
//snippet-keyword:[SDK for Java 2.0]
//snippet-keyword:[Code Sample]
//snippet-service:[Amazon Kinesis Data Firehose]
//snippet-sourcetype:[full-example]
//snippet-sourcedate:[7/6/2020]
//snippet-sourceauthor:[scmacdon - aws]

/*
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
This file is licensed under the Apache License, Version 2.0 (the "License").
You may not use this file except in compliance with the License. A copy of
the License is located at
http://aws.amazon.com/apache2.0/
This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
*/

package com.example.firehose;

// snippet-start:[firehose.java2.delete_stream.import]
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.firehose.FirehoseClient;
import software.amazon.awssdk.services.firehose.model.FirehoseException;
import software.amazon.awssdk.services.firehose.model.DeleteDeliveryStreamRequest;
// snippet-end:[firehose.java2.delete_stream.import]

public class DeleteStream {

public static void main(String[] args) {

final String USAGE = "\n" +
"Usage:\n" +
" DeleteStream <streamName> \n\n" +
"Where:\n" +
" streamName - The delivery stream name \n";

if (args.length < 1) {
System.out.println(USAGE);
System.exit(1);
}

String streamName = args[0];

Region region = Region.US_WEST_2;
FirehoseClient firehoseClient = FirehoseClient.builder()
.region(region)
.build();

delStream(firehoseClient, streamName) ;
}

// snippet-start:[firehose.java2.delete_stream.main]
public static void delStream(FirehoseClient firehoseClient, String streamName) {

try {
DeleteDeliveryStreamRequest deleteDeliveryStreamRequest = DeleteDeliveryStreamRequest.builder()
.deliveryStreamName(streamName)
.build();

firehoseClient.deleteDeliveryStream(deleteDeliveryStreamRequest);
System.out.println("Delivery stream "+streamName +" is deleted");

} catch (FirehoseException e) {
System.out.println(e.getLocalizedMessage());
System.exit(1);
}
// snippet-end:[firehose.java2.delete_stream.main]
}
}

0 comments on commit 48122cb

Please sign in to comment.