Using Amazon DynamoDb for IP and co-ordinate based geo-location services part 12: querying the geolocation range to DynamoDb

Introduction

In the previous post we created the DynamoDb source file with a reduced set of geolocations from the full MaxMind data source. We saw how we could reuse the same process as before in the case of the IP and coordinate range tables.

In this final post of this series we’ll close the loop by actually extracting and geographic properties of a geoname ID. After all you’d like to know whether a visitor come from New York other than “geoname ID 3452334”.

Querying DybamoDb

The query in this case will be very simple. We just need to get a record by its ID. We already added the necessary AWS Java SDK library to our Maven project before.

The following test code will read the record with ID 3200799 – I know that it exists in the demo geolocation range table:

public void testSingleGeonameIdLookup()
{
    AmazonDynamoDBClient dynamoClient = getDynamoDbClient();
    DynamoDB dynamoDb = new DynamoDB(dynamoClient);
    String geoLocationTableName = "geo-location-range-test";
    Table locationTable = dynamoDb.getTable(geoLocationTableName);
    Item locationItem = locationTable.getItem("geoname_id", 3200799);
    if (locationItem != null)
    {
        Map<String, Object> asMap = locationItem.asMap();
        System.out.println("Found the following location: ");
        asMap.entrySet().stream().forEach((entry) ->
        {
            System.out.println(entry.getKey().concat(": ").concat(entry.getValue().toString()));
        });
    } else
    {
        System.out.println("No such location, may refer to a proxy or satellite");
    }
}

In case you haven’t seen the stream() method in Java 8 then this is a good starting point.

The above code really only extracts a record by its ID and prints all properties available. Here’s what I got in the location item property map:

Found the following location:
continent_name: Europe
subdivision_2_name: unknown
city_name: Fuzine
country_iso_code: HR
country_name: Croatia
time_zone: Europe/Zagreb
subdivision_1_name: Primorsko-Goranska Zupanija
geoname_id: 3200799

Summary

That’s it folks, we’ve closed the loop. We’ve successfully gone through the basics of building a geolocation service using Java, the Amazon Cloud a couple more libraries. We now have our own system to check the following:

  • Get the location properties that belong to an IP
  • Get the location properties that belong to pair of longitude and latitude

We’ve set up the IP and coordinate range tables. We have also populated a location range. The IP/coords range tables are connected to the location table by the geoname ID of a location. Whether you’re trying to find the location of an IP or a coordinate pair you’ll first need to find the geoname ID and then query the location range for the full property set.

Eventually you can consider caching the results to avoid unnecessary lookups. It’s unlikely that London will suddenly move from England to Chile so the coordinate pair (51.507222, -0.1275) will always refer to London.

View all posts related to Amazon Web Services and Big Data here.

Advertisement

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT TUTORIALS WITH OPEN-SOURCE PROJECTS

Once Upon a Camayoc

Bite-size insight on Cyber Security for the not too technical.

%d bloggers like this: