Redis Jedis – How to perform CRUD operations on geo value using jedis library

1 month ago Lalit Bhagtani 0

In this tutorial, we will learn about how to perform CRUD operation on a geo value using Jedis library.

Jedis Library

Jedis is a Java client library for redis datastore. It is small and very easy to use, and fully compatible with redis 2.8.x, 3.x.x and above datastore. You can find here more information about jedis library.

Geo Value

Geospatial value contains longitude and latitude coordinates of a particular location on earth, In Redis, geospatial elements can be store in a sorted set value stored at a key and various redis commands are used to store, manage and retrieved a geospatial value stored in redis database. You can find here more information about Geo commands.

Project Setup

Create a simple maven project in your favorite IDE and add below mentioned dependency in your pom.xml file.

<dependency>
  <groupId>redis.clients</groupId>
  <artifactId>jedis</artifactId>
  <version>3.0.1</version>
</dependency>

For latest version of jedis library, check this page.

Redis Installation

You will need to install latest version of Redis. Check this page for more information about installing redis.

Jedis Connection

Create an object of Jedis ( redis.clients.jedis.Jedis ) class for connecting your java code to redis.

Jedis jedis = new Jedis();

If you have started one redis service in your local machine and on default port (6379) then default constructor will just work fine. Otherwise you have to pass correct host url and port no. as an argument into the constructor.

Create & Add

geoadd method is used to add one or more geospatial member in the geospatial value ( Sorted Set ) stored at a key.

/* Insert London Geo Coordinate in <geo-1> */
jedis.geoadd("geo-1", 51.5074, 0.1278, "London");

/* Insert London and Paris Geo Coordinate in <geo-2> */
Map<String,GeoCoordinate> map = new HashMap<String, GeoCoordinate>();		
map.put("London", new GeoCoordinate(51.5074, 0.1278));
map.put("Paris", new GeoCoordinate(48.8566, 2.3522));
jedis.geoadd("geo-2", map);

Get Geo Coordinate

geopos method is used to get GeoCoordinate (longitude, latitude) of one or more members of geospatial value ( Sorted Set ) stored at a key.

/* Returns GeoCoordinate of London and Paris from <geo-1> */
List geoCoordinates = jedis.geopos("geo-1", "London", "Paris");

Get GeoHash

geohash method is used to get valid Geohash string of one or more members of geospatial value ( Stored Set ) stored at a key.

/* Returns GeoHash String of London and Paris from <geo-1> */
List geoStrings = jedis.geohash("geo-1", "London", "Paris");

Distance between two members 

geodist method is used to get the distance between two members of geospatial value ( Sorted Set ) stored at a key in specified unit. Unit can be specified in m (meter), km (kilometer), mi (miles), ft (feet). 

/* Returns Distance between London & Paris in kilometers from <geo-1> */
Double distance = jedis.geodist("geo-1", "London", "Paris", GeoUnit.KM);

References :-

  1. Geo Command Docs

That’s all for how to perform CRUD operation on a geo value using Jedis library. If you liked it, please share your thoughts in comments section and share it with others too.