AWS Mobile Blog

Geo Library for Amazon DynamoDB – Part 4: Put Geo Point

by Yosuke Matsuda | on | Permalink | Comments |  Share

In this post, I’m going to show you how to put a Geo point into an Amazon DynamoDB table using the Geo Library. In order to put a Geo point, you need to first set up GeoDataManagerConfiguration, GeoDataManager, and an Amazon DynamoDB table. Please read GeoDataManagerConfiguration and Creating Amazon DynamoDB Tables for the details.

Creating GeoPoint

First, you need to create an instance of GeoPoint, which is a latitude and longitude pair representing a Geo point on the earth.

double latitude = 47.61121;
double longitude = -122.31846;

GeoPoint geoPoint = new GeoPoint(latitude, longitude);

Creating a Range Key AttributeValue

The range key is a user-generated value that uniquely identifies the item in a DynamoDB table. We need to create an instance of AttributeValue for the range key by calling the following:

AttributeValue rangeKeyValue = new AttributeValue().withS("your-unique-range-key-value");

Note that the range key data type has to match with the data type of your DynamoDB table range key. If the data type of your range key is Number, you can create AttributeValue as follows:

AttributeValue rangeKeyValue = new AttributeValue().withN("0123456789");

Creating PutPointRequest

Once you have an instance of GeoPoint that represents the Geo location and an instance of AttributeValue that uniquely identifies the Geo point, you can create PutPointRequest:

PutPointRequest putPointRequest = new PutPointRequest(geoPoint, rangeKeyValue);

Putting a Geo point

You can simply call putPoint on an instance of GeoDataManager:

GeoDataManager geoDataManager = // Instantiate GeoDataManager

double latitude = 47.61121;
double longitude = -122.31846;
GeoPoint geoPoint = new GeoPoint(latitude, longitude);

AttributeValue rangeKeyValue = new AttributeValue().withS("unique-range-key-value");

PutPointRequest putPointRequest = new PutPointRequest(geoPoint, rangeKeyValue);
PutPointResult putPointResult = geoDataManager.putPoint(putPointRequest);

Adding additional attributes

In addition to the geo-location information that GeoPoint represents, items on the DynamoDB tables can have your own custom attributes. For example, you may want to store a school name and zip code along with the school location.

You can do this by customizing PutItemRequest. PutPointRequest internally has an instance of PutItemRequest, which is defined in the AWS SDK for Java and is used by the Geo Library to put a DynamoDB item into a table. You have access to this low-level object once you construct PutPointRequest, and you can modify it as follows in order to add your custom attributes:


PutPointRequest putPointRequest = new PutPointRequest(geoPoint, rangeKeyValue);

AttributeValue schoolNameValue = new AttributeValue().withS("Custom School Name");
AttributeValue schoolZipValue = new AttributeValue().withN("98765");

PutItemRequest putItemRequest = putPointRequest.getPutItemRequest();
putItemRequest.addItemEntry("schoolName", schoolNameValue);
putItemRequest.addItemEntry("schoolZip", schoolZipValue);

PutPointResult putPointResult = geoDataManager.putPoint(putPointRequest);

The above code adds the following two custom attributes:

Attribute Name Attribute Data Type Attribute Value
schoolName String Custom School Name
schoolZip Number 98765

Customizing PutItemRequest

You can further customize PutItemRequest to perform additional tasks. For instance, you can tell PutItemRequest to retrieve Write Capacity Units consumed by the Put request:

PutItemRequest putItemRequest = putPointRequest.getPutItemRequest();

PutPointResult putPointResult = geoDataManager.putPoint(putPointRequest);

PutItemResult putItemResult = putPointResult.getPutItemResult();
double consumedCapacity = putItemResult.getConsumedCapacity().getCapacityUnits();

PutPointResult contains PutItemResult, and you can retrieve consumed Write Capacity Units easily.

Please read PutItemRequest API Reference and PutItemResult API Reference for the full list of customizable fields. Please note that the table name and hash key, geohash, and geoJson attribute are not customizable and will be overwritten by GeoDataManagerConfiguration values. Also, once put these four values are not modifiable. In order to update these fields, you need to delete the Geo point and put a new point.

As always, please leave a comment below if you have questions.

Further Reading