AWS Code Sample
Catalog

The AWS Documentation website is getting a new look!
Try it now and let us know what you think. Switch to the new look >>

You can return to the original look by selecting English in the language selector above.

MoviesQuery.java

/** * Copyright 2010-2019 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.amazonaws.codesamples.gsg; import java.util.HashMap; import java.util.Iterator; import com.amazonaws.client.builder.AwsClientBuilder; import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder; import com.amazonaws.services.dynamodbv2.document.DynamoDB; import com.amazonaws.services.dynamodbv2.document.Item; import com.amazonaws.services.dynamodbv2.document.ItemCollection; import com.amazonaws.services.dynamodbv2.document.QueryOutcome; import com.amazonaws.services.dynamodbv2.document.Table; import com.amazonaws.services.dynamodbv2.document.spec.QuerySpec; public class MoviesQuery { public static void main(String[] args) throws Exception { AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard() .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://localhost:8000", "us-west-2")) .build(); DynamoDB dynamoDB = new DynamoDB(client); Table table = dynamoDB.getTable("Movies"); HashMap<String, String> nameMap = new HashMap<String, String>(); nameMap.put("#yr", "year"); HashMap<String, Object> valueMap = new HashMap<String, Object>(); valueMap.put(":yyyy", 1985); QuerySpec querySpec = new QuerySpec().withKeyConditionExpression("#yr = :yyyy").withNameMap(nameMap) .withValueMap(valueMap); ItemCollection<QueryOutcome> items = null; Iterator<Item> iterator = null; Item item = null; try { System.out.println("Movies from 1985"); items = table.query(querySpec); iterator = items.iterator(); while (iterator.hasNext()) { item = iterator.next(); System.out.println(item.getNumber("year") + ": " + item.getString("title")); } } catch (Exception e) { System.err.println("Unable to query movies from 1985"); System.err.println(e.getMessage()); } valueMap.put(":yyyy", 1992); valueMap.put(":letter1", "A"); valueMap.put(":letter2", "L"); querySpec.withProjectionExpression("#yr, title, info.genres, info.actors[0]") .withKeyConditionExpression("#yr = :yyyy and title between :letter1 and :letter2").withNameMap(nameMap) .withValueMap(valueMap); try { System.out.println("Movies from 1992 - titles A-L, with genres and lead actor"); items = table.query(querySpec); iterator = items.iterator(); while (iterator.hasNext()) { item = iterator.next(); System.out.println(item.getNumber("year") + ": " + item.getString("title") + " " + item.getMap("info")); } } catch (Exception e) { System.err.println("Unable to query movies from 1992:"); System.err.println(e.getMessage()); } } }

Sample Details

Service: dynamodb

Last tested:

Author: AWS

Type: full-example

On this page: