
上QQ阅读APP看书,第一时间看更新
Low-level interface
AWS SDK programming languages provide low-level interfaces for DynamoDB. These SDKs provide methods that are similar to low-level DynamoDB API requests.
The following example uses the Java language for the low-level interface of AWS SDKs. Here you can use Eclipse IDE for the example.
In this Java program, we request getItem from the Movies table, pass the movie name as an attribute, and print the movie release year:
- Let's create the MovieLowLevelExample file. We have to import a few classes to work with the DynamoDB.
AmazonDynamoDBClient is used to create the DynamoDB client instance. AttributeValue is used to construct the data. In AttributeValue, name is datatype and value is data:
-
- GetItemRequest is the input of GetItem
- GetItemResult is the output of GetItem
- The following code will create the dynamoDB client instance. You have to assign the credentials and region to this instance:
Static AmazonDynamoDBClient dynamoDB;
- In the code, we have created HashMap, passing the value parameter as AttributeValue().withS(). It contains actual data and withS is the attribute of String:
String tableName = "Movies";
HashMap<String, AttributeValue> key = new HashMap<String, AttributeValue>();
key.put("name", new AttributeValue().withS("Airplane"));
- GetItemRequest will create a request object, passing the table name and key as a parameter. It is the input of GetItem:
GetItemRequest request = new GetItemRequest()
.withTableName(tableName).withKey(key);
- GetItemResult will create the result object. It is the output of getItem where we are passing request as an input:
GetItemResult result = dynamoDB.getItem(request);
- It will check the getItem null condition. If getItem is not null then create the object for AttributeValue. It will get the year from the result object and create an instance for yearObj. It will print the year value from yearObj:
if (result.getItem() != null) {
AttributeValue yearObj = result.getItem().get("year");
System.out.println("The movie Released in " + yearObj.getN());
} else {
System.out.println("No matching movie was found");
}
