Skip to content

Commit

Permalink
Merge pull request awsdocs#1736 from scmacdon/master
Browse files Browse the repository at this point in the history
Add the  Creating an Amazon Web Services Lambda function that tags digital assets located in Amazon S3 buckets tutorial
LGTM
  • Loading branch information
irenepsmith authored Apr 5, 2021
2 parents c2f31d2 + de95241 commit 1e58259
Show file tree
Hide file tree
Showing 24 changed files with 1,484 additions and 9 deletions.
4 changes: 3 additions & 1 deletion javav2/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ In the **use_cases** folder, find step-by-step development tutorials that use mu

+ **Building an AWS Workflow that sends notifications over multiple channels** - A tutorial that discusses how to develop an AWS serverless workflow that sends notifications over multiple channels. In this AWS tutorial, you create an AWS serverless workflow by using AWS Step Functions, the AWS SDK for Java, and Lambda functions. Each workflow step is implemented by using an AWS Lambda function.

+ **Building a Spring Boot web application that Streams Amazon S3 content over HTTP** - A tutorial that discusses how to create a web application that streams Amazon S3 video content over HTTP. The video is displayed in the application’s view. In this tutorial, the Spring Framework along with AWS SDK for Java API is used to create the application.
+ **Building a Spring Boot web application that Streams Amazon S3 content over HTTP** - A tutorial that discusses how to create a web application that streams Amazon S3 video content over HTTP. The video is displayed in the application’s view. In this tutorial, the Spring Framework along with AWS SDK for Java API is used to create the application.

+ **Creating Lambda functions that tags digital assets located in Amazon S3 buckets** - A tutorial that discusses how to create a Lambda function that automatically tags digital assets located in an Amazon Simple Storage Service (Amazon S3) bucket.


AWS service examples
Expand Down
3 changes: 3 additions & 0 deletions javav2/example_code/iam/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ files:
- path: src/main/java/com/example/iam/CreatePolicy.java
services:
- iam
- path: src/main/java/com/example/iam/CreateRole.java
services:
- iam
- path: src/main/java/com/example/iam/CreateUser.java
services:
- iam
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
//snippet-sourcedescription:[CreateRole.java demonstrates how to create an AWS Identity and Access Management (IAM) role.]
//snippet-keyword:[AWS SDK for Java v2]
//snippet-keyword:[Code Sample]
//snippet-service:[IAM]
//snippet-sourcetype:[full-example]
//snippet-sourcedate:[11/02/2020]
//snippet-sourceauthor:[scmacdon-aws]

/*
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package com.example.iam;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import software.amazon.awssdk.services.iam.model.*;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.iam.IamClient;

import java.io.FileReader;

/*
This example requires a trust policy document. For more information, see:
https://aws.amazon.com/blogs/security/how-to-use-trust-policies-with-iam-roles/
*/


public class CreateRole {

public static void main(String[] args) {

final String USAGE = "\n" +
"Usage:\n" +
" CreateRole <rolename> <fileLocation> \n\n" +
"Where:\n" +
" rolename - the name of the role to create. \n\n" +
" fileLocation - the location of the JSON document that represents the trust policy. \n\n" ;

if (args.length != 2) {
System.out.println(USAGE);
System.exit(1);
}

String rolename = args[0];
String fileLocation = args[1];
Region region = Region.AWS_GLOBAL;
IamClient iam = IamClient.builder()
.region(region)
.build();

String result = createIAMRole(iam, rolename, fileLocation) ;
System.out.println("Successfully created user: " +result);
iam.close();
}

public static String createIAMRole(IamClient iam, String rolename, String fileLocation ) {

try {

JSONObject jsonObject = (JSONObject) readJsonSimpleDemo(fileLocation);

CreateRoleRequest request = CreateRoleRequest.builder()
.roleName(rolename)
.assumeRolePolicyDocument(jsonObject.toJSONString())
.description("Created using the AWS SDK for Java")
.build();

CreateRoleResponse response = iam.createRole(request);
System.out.println("The ARN of the role is "+response.role().arn());

} catch (IamException e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
} catch (Exception e) {
e.printStackTrace();
}
return "";
}

public static Object readJsonSimpleDemo(String filename) throws Exception {
FileReader reader = new FileReader(filename);
JSONParser jsonParser = new JSONParser();
return jsonParser.parse(reader);
}
}
20 changes: 12 additions & 8 deletions javav2/example_code/redshift/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,19 @@
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bom</artifactId>
<version>2.16.29</version>
<version>2.15.14</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<dependency>
<groupId>com.amazon.redshift</groupId>
<artifactId>redshift-jdbc41</artifactId>
<version>1.2.1.1001</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.4.2</version>
Expand All @@ -69,11 +74,6 @@
<version>1.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.amazon.redshift</groupId>
<artifactId>redshift-jdbc41</artifactId>
<version>1.2.12.1017</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
Expand All @@ -88,5 +88,9 @@
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>redshiftdata</artifactId>
</dependency>
</dependencies>
</project>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
//snippet-sourcedescription:[ListDatabases.java demonstrates how to list databases and tables that are part of a cluster by using a RedshiftDataClient object.]
//snippet-keyword:[AWS SDK for Java v2]
//snippet-keyword:[Code Sample]
//snippet-service:[Amazon Redshift ]
//snippet-sourcetype:[full-example]
//snippet-sourcedate:[04/05/2021]
//snippet-sourceauthor:[scmacdon - aws]

/*
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package com.example.redshiftdata;

import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.redshiftdata.model.*;
import software.amazon.awssdk.services.redshiftdata.RedshiftDataClient;
import software.amazon.awssdk.services.redshiftdata.model.ListDatabasesRequest;
import software.amazon.awssdk.services.redshiftdata.model.ListTablesRequest;
import java.util.List;

public class ListDatabases {

public static void main(String[] args) {

final String USAGE = "\n" +
"Usage:\n" +
" ListDatabases <database> <dbUser> <sqlStatement> <clusterId> \n\n" +
"Where:\n" +
" database - the name of the database (for example, dev) \n" +
" dbUser - the master user name \n" +
" clusterId - the id of the Redshift cluster (for example, redshift-cluster) \n";

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

String database = args[0];
String dbUser = args[1];
String clusterId = args[2];

Region region = Region.US_WEST_2;
RedshiftDataClient redshiftDataClient = RedshiftDataClient.builder()
.region(region)
.build();

listAllDatabases(redshiftDataClient,clusterId, dbUser, database) ;
listAllTables(redshiftDataClient,clusterId, dbUser, database);
redshiftDataClient.close();
}

public static void listAllDatabases(RedshiftDataClient redshiftDataClient,String clusterId, String dbUser, String database) {

try {
ListDatabasesRequest databasesRequest = ListDatabasesRequest.builder()
.clusterIdentifier(clusterId)
.dbUser(dbUser)
.database(database)
.build();

ListDatabasesResponse databasesResponse = redshiftDataClient.listDatabases(databasesRequest);
List<String> databases = databasesResponse.databases();

for (String dbName: databases) {
System.out.println("The database name is : "+dbName);
}

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

public static void listAllTables(RedshiftDataClient redshiftDataClient,String clusterId, String dbUser, String database){

try {
ListTablesRequest tablesRequest = ListTablesRequest.builder()
.clusterIdentifier(clusterId)
.database(database)
.dbUser(dbUser)
.build();

ListTablesResponse tablesResponse = redshiftDataClient.listTables(tablesRequest);
List<TableMember> tables = tablesResponse.tables();

for (TableMember table: tables) {
System.out.println("The table name is : "+table.name());
}

} catch (RedshiftDataException e) {
System.err.println(e.getMessage());
System.exit(1);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
//snippet-sourcedescription:[RetrieveData.java demonstrates how to query data and check the results by using a RedshiftDataClient object.]
//snippet-keyword:[AWS SDK for Java v2]
//snippet-keyword:[Code Sample]
//snippet-service:[Amazon Redshift ]
//snippet-sourcetype:[full-example]
//snippet-sourcedate:[04/05/2021]
//snippet-sourceauthor:[scmacdon - aws]

/*
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package com.example.redshiftdata;

import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.redshiftdata.model.*;
import software.amazon.awssdk.services.redshiftdata.RedshiftDataClient;
import software.amazon.awssdk.services.redshiftdata.model.DescribeStatementRequest;
import java.util.List;

public class RetrieveData {

public static void main(String[] args) {

final String USAGE = "\n" +
"Usage:\n" +
" RetrieveData <database> <dbUser> <sqlStatement> <clusterId> \n\n" +
"Where:\n" +
" database - the name of the database (for example, dev) \n" +
" dbUser - the master user name \n" +
" sqlStatement - the sql statement to use (for example, select * from information_schema.tables;) \n" +
" clusterId - the id of the Redshift cluster (for example, redshift-cluster) \n" ;

if (args.length != 4) {
System.out.println(USAGE);
System.exit(1);
}

String database = args[0];
String dbUser = args[1];
String sqlStatement = args[2];
String clusterId = args[3];

Region region = Region.US_WEST_2;
RedshiftDataClient redshiftDataClient = RedshiftDataClient.builder()
.region(region)
.build();

String id = performSQLStatement(redshiftDataClient, database, dbUser, sqlStatement, clusterId);
System.out.println("The identifier of the statement is "+id);
checkStatement(redshiftDataClient,id );
getResults(redshiftDataClient, id);
redshiftDataClient.close();
}

public static void checkStatement(RedshiftDataClient redshiftDataClient,String sqlId ) {

try {

DescribeStatementRequest statementRequest = DescribeStatementRequest.builder()
.id(sqlId)
.build() ;

// Wait until the sql statement processing is finished.
boolean finished = false;
String status = "";
while (!finished) {

DescribeStatementResponse response = redshiftDataClient.describeStatement(statementRequest);
status = response.statusAsString();
System.out.println("..."+status);

if (status.compareTo("FINISHED") == 0) {
break;
}
Thread.sleep(1000);
}

System.out.println("The statement is finished!");

} catch (RedshiftDataException | InterruptedException e) {
System.err.println(e.getMessage());
System.exit(1);
}
}

public static String performSQLStatement(RedshiftDataClient redshiftDataClient,
String database,
String dbUser,
String sqlStatement,
String clusterId) {

try {
ExecuteStatementRequest statementRequest = ExecuteStatementRequest.builder()
.clusterIdentifier(clusterId)
.database(database)
.dbUser(dbUser)
.sql(sqlStatement)
.build();

ExecuteStatementResponse response = redshiftDataClient.executeStatement(statementRequest);
return response.id();

} catch (RedshiftDataException e) {
System.err.println(e.getMessage());
System.exit(1);
}
return "";
}


public static void getResults(RedshiftDataClient redshiftDataClient, String statementId) {

try {

GetStatementResultRequest resultRequest = GetStatementResultRequest.builder()
.id(statementId)
.build();

GetStatementResultResponse response = redshiftDataClient.getStatementResult(resultRequest);

// Iterate through the List element where each element is a List object.
List<List<Field>> dataList = response.records();

// Print out the records.
for (List list: dataList) {

for (Object myField:list) {

Field field = (Field) myField;
String value = field.stringValue();
if (value != null)
System.out.println("The value of the field is " + value);
}
}

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

0 comments on commit 1e58259

Please sign in to comment.