DynamoDBScanItems.java - AWS Code Sample


DynamoDBScanItems demonstrates how to return one or more items and item attributes by accessing every item in a table.

/** * Copyright 2018-2020 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 aws.example.dynamodb; import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder; import com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException; import com.amazonaws.services.dynamodbv2.model.AttributeValue; import com.amazonaws.services.dynamodbv2.model.ScanRequest; import com.amazonaws.services.dynamodbv2.model.ScanResult; import java.util.Map; import java.util.Set; public class DynamoDBScanItems { public static void main(String[] args) { if (args.length < 1) { System.out.println("Please specify a table name"); System.exit(1); } String tableName = args[0]; AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build(); try { ScanRequest scanRequest = new ScanRequest() .withTableName(tableName); ScanResult result = client.scan(scanRequest); for (Map<String, AttributeValue> item : result.getItems()) { Set<String> keys = item.keySet(); for (String key : keys) { System.out.println ("The key name is "+key +"\n" ); System.out.println("The value is "+item.get(key).getS()); } } } catch (AmazonDynamoDBException e) { e.getStackTrace(); } } }

Sample Details

Service: dynamodb

Last tested: 2020-01-27

Author: AWS-scmacdon

Type: full-example