Monday, November 28, 2022
HomeJavaUse Redis GeoHash with Spring boot - Java Code Geeks

Use Redis GeoHash with Spring boot – Java Code Geeks

One very helpful Knowledge Construction on the subject of Redis is the GeoHash Knowledge construction. Basically it’s a sorted set that generates a rating primarily based on the longitude and latitude.

We are going to spin up a Redis database utilizing Compose

    picture: redis
      - 6379:6379

Could be run like this

You’ll find extra on Compose on the Builders Important Information to Docker Compose.

Let’s add our dependencies

<?xml model="1.0" encoding="UTF-8"?>
<mission xmlns=""




We will begin with our Configuration. For comfort on injecting we will create a GeoOperations<String,String> bean.

bundle org.location;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.information.redis.core.GeoOperations;
import org.springframework.information.redis.core.RedisTemplate;

public class RedisConfiguration {

    public GeoOperations<String,String> geoOperations(RedisTemplate<String,String> template) {
        return template.opsForGeo();


Our mannequin can be this one

bundle org.location;

import lombok.Knowledge;

public class Location {

    personal String title;
    personal Double lat;
    personal Double lng;


This easy service will persist venue areas and in addition fetch venues close by of a location.

bundle org.location;

import java.util.Checklist;

import org.springframework.information.geo.Circle;
import org.springframework.information.geo.Distance;
import org.springframework.information.geo.GeoResult;
import org.springframework.information.geo.GeoResults;
import org.springframework.information.geo.Metrics;
import org.springframework.information.geo.Level;
import org.springframework.information.redis.connection.RedisGeoCommands;
import org.springframework.information.redis.core.GeoOperations;
import org.springframework.information.redis.area.geo.GeoLocation;
import org.springframework.stereotype.Service;

public class GeoService {

    public static remaining String VENUS_VISITED = "venues_visited";
    personal remaining GeoOperations<String, String> geoOperations;

    public GeoService(GeoOperations<String, String> geoOperations) {
        this.geoOperations = geoOperations;

    public void add(Location location) {
        Level level = new Level(location.getLng(), location.getLat());
        geoOperations.add(VENUS_VISITED, level, location.getName());


    public Checklist<String> nearByVenues(Double lng, Double lat, Double kmDistance) {
        Circle circle = new Circle(new Level(lng, lat), new Distance(kmDistance, Metrics.KILOMETERS));
        GeoResults<RedisGeoCommands.GeoLocation<String>> res = geoOperations.radius(VENUS_VISITED, circle);
        return res.getContent().stream()


We will additionally add a controller

bundle org.location;

import java.util.Checklist;

import org.springframework.http.ResponseEntity;

public class LocationController {

    personal remaining GeoService geoService;

    public LocationController(GeoService geoService) {
        this.geoService = geoService;

    public ResponseEntity<String> addLocation(@RequestBody Location location) {
        return ResponseEntity.okay("Success");

    @GetMapping("/location/close by")
    public ResponseEntity<Checklist<String>> areas(Double lng, Double lat, Double km) {
        Checklist<String> areas = geoService.nearByVenues(lng, lat, km);
        return ResponseEntity.okay(areas);


Then let’s add a component.

curl --location --request POST 'localhost:8080/location' 
--header 'Content material-Kind: software/json' 
--data-raw '{
	"lng": 51.5187516,
	"title": "liverpool-street"

Let’s retrieve the factor of the api

curl --location --request GET 'localhost:8080/location/close by?lng=51.4595573&lat=0.24949&km=100'
> [

And likewise let’s examine redis

ZRANGE venues_visited 0 -1 WITHSCORES
1) "liverpool-street"
2) "2770072452773375"

We did it, fairly handy for our each day distance use instances.

Revealed on Java Code Geeks with permission by Emmanouil Gkatziouras, companion at our JCG program. See the unique article right here: Use Redis GeoHash with Spring boot

Opinions expressed by Java Code Geeks contributors are their very own.



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments