forked from awsdocs/aws-doc-sdk-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request awsdocs#1736 from scmacdon/master
Add the Creating an Amazon Web Services Lambda function that tags digital assets located in Amazon S3 buckets tutorial LGTM
- Loading branch information
Showing
24 changed files
with
1,484 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
javav2/example_code/iam/src/main/java/com/example/iam/CreateRole.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
javav2/example_code/redshift/src/main/java/com/example/redshiftdata/ListDatabases.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
143 changes: 143 additions & 0 deletions
143
javav2/example_code/redshift/src/main/java/com/example/redshiftdata/RetrieveData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
Oops, something went wrong.