Skip to content

Commit

Permalink
Merge pull request awsdocs#554 from awsdocs/java_snippets
Browse files Browse the repository at this point in the history
Adding code snippets to IAM Java v2 examples
  • Loading branch information
jschwarzwalder authored Mar 7, 2019
2 parents 00314cc + 2702d74 commit 9ac5714
Show file tree
Hide file tree
Showing 50 changed files with 415 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
specific language governing permissions and limitations under the License.
*/
package com.example.dynamodb;
// snippet-start:[dynamodb.java.create_table.complete]
// snippet-start:[dynamodb.java.create_table.import]
import software.amazon.awssdk.services.dynamodb.model.DynamoDbException;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.AttributeDefinition;
Expand All @@ -28,7 +30,8 @@
import software.amazon.awssdk.services.dynamodb.model.KeyType;
import software.amazon.awssdk.services.dynamodb.model.ProvisionedThroughput;
import software.amazon.awssdk.services.dynamodb.model.ScalarAttributeType;


// snippet-end:[dynamodb.java.create_table.import]
/**
* Create a DynamoDB table.
*
Expand Down Expand Up @@ -57,11 +60,12 @@ public static void main(String[] args)

/* Read the name from command args */
String table_name = args[0];

System.out.format(
"Creating table \"%s\" with a simple primary key: \"Name\".\n",
table_name);

// snippet-start:[dynamodb.java.create_table.main]
CreateTableRequest request = CreateTableRequest.builder()
.attributeDefinitions(AttributeDefinition.builder()
.attributeName("Name")
Expand All @@ -87,6 +91,9 @@ public static void main(String[] args)
System.err.println(e.getMessage());
System.exit(1);
}
// snippet-end:[dynamodb.java.create_table.main]
System.out.println("Done!");
}
}

// snippet-end:[dynamodb.java.create_table.complete]
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
specific language governing permissions and limitations under the License.
*/
package com.example.dynamodb;
// snippet-start:[dynamodb.java.create_table_composite_key.complete]
// snippet-start:[dynamodb.java.create_table_composite_key.import]
import software.amazon.awssdk.services.dynamodb.model.DynamoDbException;
import software.amazon.awssdk.services.dynamodb.model.AttributeDefinition;
import software.amazon.awssdk.services.dynamodb.model.CreateTableRequest;
Expand All @@ -30,7 +32,8 @@

import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.CreateTableRequest;


// snippet-end:[dynamodb.java.create_table_composite_key.import]
/**
* Create a DynamoDB table.
*
Expand Down Expand Up @@ -64,6 +67,7 @@ public static void main(String[] args)
System.out.format("* Language - partition key\n");
System.out.format("* Greeting - sort key\n");

// snippet-start:[dynamodb.java.create_table_composite_key.main]
CreateTableRequest request = CreateTableRequest.builder()
.attributeDefinitions(
AttributeDefinition.builder()
Expand Down Expand Up @@ -98,6 +102,9 @@ public static void main(String[] args)
System.err.println(e.getMessage());
System.exit(1);
}
// snippet-end:[dynamodb.java.create_table_composite_key.main]
System.out.println("Done!");
}
}

// snippet-end:[dynamodb.java.create_table_composite_key.complete]
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@
specific language governing permissions and limitations under the License.
*/
package com.example.dynamodb;
// snippet-start:[dynamodb.java.delete_item.complete]
// snippet-start:[dynamodb.java.delete_item.import]

import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
import software.amazon.awssdk.services.dynamodb.model.DeleteItemRequest;
import software.amazon.awssdk.services.dynamodb.model.DeleteItemResponse;
import software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient;
import software.amazon.awssdk.services.dynamodb.model.DynamoDbException;
import java.util.HashMap;


// snippet-end:[dynamodb.java.delete_item.import]
/**
* Delete an item from a DynamoDB table.
*
Expand Down Expand Up @@ -63,6 +66,7 @@ public static void main(String[] args)

System.out.format("Deleting item \"%s\" from %s\n", name, table_name);

// snippet-start:[dynamodb.java.delete_item.main]
HashMap<String,AttributeValue> key_to_get =
new HashMap<String,AttributeValue>();

Expand All @@ -84,6 +88,9 @@ public static void main(String[] args)
System.exit(1);
}

// snippet-end:[dynamodb.java.delete_item.main]
System.out.println("Done!");
}
}

// snippet-end:[dynamodb.java.delete_item.complete]
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@
specific language governing permissions and limitations under the License.
*/
package com.example.dynamodb;
// snippet-start:[dynamodb.java.delete_table.complete]
// snippet-start:[dynamodb.java.delete_table.import]
import software.amazon.awssdk.services.dynamodb.model.DynamoDbException;

import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.DeleteTableRequest;


// snippet-end:[dynamodb.java.delete_table.import]
/**
* Delete a DynamoDB table.
*
Expand Down Expand Up @@ -54,9 +57,10 @@ public static void main(String[] args)
}

String table_name = args[0];

System.out.format("Deleting table %s...\n", table_name);

// snippet-start:[dynamodb.java.delete_table.main]
DynamoDbClient ddb = DynamoDbClient.create();

DeleteTableRequest request = DeleteTableRequest.builder()
Expand All @@ -69,6 +73,9 @@ public static void main(String[] args)
System.err.println(e.getMessage());
System.exit(1);
}
// snippet-end:[dynamodb.java.delete_table.main]
System.out.println("Done!");
}
}

// snippet-end:[dynamodb.java.delete_table.complete]
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@
specific language governing permissions and limitations under the License.
*/
package com.example.dynamodb;
// snippet-start:[dynamodb.java.describe_table.complete]
// snippet-start:[dynamodb.java.describe_table.import]
import software.amazon.awssdk.services.dynamodb.model.DynamoDbException;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.AttributeDefinition;
import software.amazon.awssdk.services.dynamodb.model.DescribeTableRequest;
import software.amazon.awssdk.services.dynamodb.model.ProvisionedThroughputDescription;
import software.amazon.awssdk.services.dynamodb.model.TableDescription;
import java.util.List;


// snippet-end:[dynamodb.java.describe_table.import]
/**
* Get information about (describe) a DynamoDB table.
*
Expand Down Expand Up @@ -55,6 +58,7 @@ public static void main(String[] args)
String table_name = args[0];
System.out.format("Getting description for %s\n\n", table_name);

// snippet-start:[dynamodb.java.describe_table.main]
DynamoDbClient ddb = DynamoDbClient.create();

DescribeTableRequest request = DescribeTableRequest.builder()
Expand Down Expand Up @@ -97,6 +101,9 @@ public static void main(String[] args)
System.err.println(e.getMessage());
System.exit(1);
}
// snippet-end:[dynamodb.java.describe_table.main]
System.out.println("\nDone!");
}
}

// snippet-end:[dynamodb.java.describe_table.complete]
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@
specific language governing permissions and limitations under the License.
*/
package com.example.dynamodb;
// snippet-start:[dynamodb.java.get_item.complete]
// snippet-start:[dynamodb.java.get_item.import]
import software.amazon.awssdk.services.dynamodb.model.DynamoDbException;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
import software.amazon.awssdk.services.dynamodb.model.GetItemRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;


// snippet-end:[dynamodb.java.get_item.import]
/**
* Get an item from a DynamoDB table.
*
Expand Down Expand Up @@ -72,6 +75,7 @@ public static void main(String[] args)
System.out.format("Retrieving item \"%s\" from \"%s\"\n",
name, table_name);

// snippet-start:[dynamodb.java.get_item.main]
HashMap<String,AttributeValue> key_to_get =
new HashMap<String,AttributeValue>();

Expand Down Expand Up @@ -110,5 +114,8 @@ public static void main(String[] args)
System.err.println(e.getMessage());
System.exit(1);
}
// snippet-end:[dynamodb.java.get_item.main]
}
}

// snippet-end:[dynamodb.java.get_item.complete]
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@
specific language governing permissions and limitations under the License.
*/
package com.example.dynamodb;
// snippet-start:[dynamodb.java.list_tables.complete]
// snippet-start:[dynamodb.java.list_tables.import]
import software.amazon.awssdk.services.dynamodb.model.DynamoDbException;
import software.amazon.awssdk.services.dynamodb.model.ListTablesResponse;
import software.amazon.awssdk.services.dynamodb.model.ListTablesRequest;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;

import java.util.List;


// snippet-end:[dynamodb.java.list_tables.import]
/**
* List DynamoDB tables for the current AWS account.
*
Expand All @@ -38,6 +41,7 @@ public static void main(String[] args)
{
System.out.println("Your DynamoDB tables:\n");

// snippet-start:[dynamodb.java.list_tables.main]
DynamoDbClient ddb = DynamoDbClient.create();

boolean more_tables = true;
Expand Down Expand Up @@ -76,6 +80,9 @@ public static void main(String[] args)
System.exit(1);
}
}
// snippet-end:[dynamodb.java.list_tables.main]
System.out.println("\nDone!");
}
}

// snippet-end:[dynamodb.java.list_tables.complete]
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@
specific language governing permissions and limitations under the License.
*/
package com.example.dynamodb;
// snippet-start:[dynamodb.java.put_item.complete]
// snippet-start:[dynamodb.java.put_item.import]
import software.amazon.awssdk.services.dynamodb.model.DynamoDbException;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
import software.amazon.awssdk.services.dynamodb.model.PutItemRequest;
import software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;


// snippet-end:[dynamodb.java.put_item.import]
/**
* Put an item in a DynamoDB table.
*
Expand Down Expand Up @@ -81,6 +84,7 @@ public static void main(String[] args)
}
}

// snippet-start:[dynamodb.java.put_item.main]
HashMap<String,AttributeValue> item_values =
new HashMap<String,AttributeValue>();

Expand All @@ -106,6 +110,9 @@ public static void main(String[] args)
System.err.println(e.getMessage());
System.exit(1);
}
// snippet-end:[dynamodb.java.put_item.main]
System.out.println("Done!");
}
}

// snippet-end:[dynamodb.java.put_item.complete]
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
specific language governing permissions and limitations under the License.
*/
package com.example.dynamodb;
// snippet-start:[dynamodb.java.query.complete]
// snippet-start:[dynamodb.java.query.import]
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
import software.amazon.awssdk.services.dynamodb.model.ConditionalOperator;
import software.amazon.awssdk.services.dynamodb.model.QueryRequest;
Expand All @@ -30,7 +32,8 @@

import software.amazon.awssdk.services.dynamodb.model.DynamoDbException;
import software.amazon.awssdk.regions.Region;


// snippet-end:[dynamodb.java.query.import]
/**
* Query a DynamoDB table.
*
Expand Down Expand Up @@ -68,6 +71,7 @@ public static void main(String[] args)
System.out.println("");


// snippet-start:[dynamodb.java.query.main]
Region region = Region.US_WEST_2;
DynamoDbClient ddb = DynamoDbClient.builder().region(region).build();

Expand Down Expand Up @@ -95,6 +99,9 @@ public static void main(String[] args)
System.err.println(e.getMessage());
System.exit(1);
}
// snippet-end:[dynamodb.java.query.main]
System.out.println("Done!");
}
}

// snippet-end:[dynamodb.java.query.complete]
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@
* limitations under the License.
*/
package com.example.dynamodb;
// snippet-start:[dynamodb.java.sync_pagination.complete]
// snippet-start:[dynamodb.java.sync_pagination.import]

import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.ListTablesRequest;
import software.amazon.awssdk.services.dynamodb.model.ListTablesResponse;
import software.amazon.awssdk.services.dynamodb.paginators.ListTablesIterable;
// snippet-end:[dynamodb.java.sync_pagination.import]

// snippet-start:[dynamodb.java.sync_pagination.main]
public class SyncPagination {

public static void main(String[] args) {
Expand Down Expand Up @@ -89,30 +93,35 @@ private static void AutoPagination() {

ListTablesIterable responses = client.listTablesPaginator(listTablesRequest);


System.out.println("AutoPagination: using for loop");
for (final ListTablesResponse response : responses) {
System.out.println(response.tableNames());
}

// Print the table names using the responses stream

System.out.println("AutoPagination: using stream");

responses.stream().forEach(response -> System.out.println(response.tableNames()));

// Convert the stream of responses to stream of table names, then print the table names

System.out.println("AutoPagination: using flatmap to get stream of table names");

responses.stream()
.flatMap(response -> response.tableNames().stream())
.forEach(System.out::println);


System.out.println("AutoPagination: iterating directly on the table names");

Iterable<String> tableNames = responses.tableNames();
tableNames.forEach(System.out::println);
}

private static void AutoPaginationWithResume() {

System.out.println("running AutoPagination with resume in case of errors...\n");

final DynamoDbClient client = DynamoDbClient.create();
Expand All @@ -133,4 +142,5 @@ private static void AutoPaginationWithResume() {
}
}
}

// snippet-end:[dynamodb.java.sync_pagination.main]
// snippet-end:[dynamodb.java.sync_pagination.complete]
Loading

0 comments on commit 9ac5714

Please sign in to comment.