December 4, 2022

Robotic Notes

All technology News

Use Redis GeoHash with Spring boot – Java Code Geeks

3 min read

A very handy data structure when it comes to Redis is the GeoHash data structure. It is essentially a sorted array that generates a result based on the longitude and latitude.

We will spin up a Redis database using Compose

    image: redis
      - 6379:6379

It can be managed like this

You can find more about Compose in the Basic Docker Compose Developer Guide.

Let’s add our dependencies

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=""




We’ll start with our configuration. For injection convenience, we’ll create GeoOperations bean.

package org.location;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

public class RedisConfiguration 

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

Our model will be this

package org.location;

import lombok.Data;

public class Location 

    private String name;
    private Double lat;
    private Double lng;

This simple service will store the locations of places and retrieve places near a location.

package org.location;

import java.util.List;

import org.springframework.stereotype.Service;

public class GeoService 

    public static final String VENUS_VISITED = "venues_visited";
    private final GeoOperations<String, String> geoOperations;

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

    public void add(Location location) 
        Point point = new Point(location.getLng(), location.getLat());
        geoOperations.add(VENUS_VISITED, point, location.getName());


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

We will also add a controller

package org.location;

import java.util.List;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

public class LocationController 

    private final GeoService geoService;

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

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

    public ResponseEntity<List<String>> locations(Double lng, Double lat, Double km) 
        List<String> locations = geoService.nearByVenues(lng, lat, km);
        return ResponseEntity.ok(locations);

Then let’s add an element.

curl --location --request POST 'localhost:8080/location' \
--header 'Content-Type: application/json' \
--data-raw '
	"lng": 51.5187516,
	"name": "liverpool-street"

Let’s retrieve the element from the api

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

And also let’s check redis

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

We did, quite handy for our daily remote use cases.

Posted on Java Code Geeks with permission from Emmanouil Gkatziouras, a partner in our JCG program. See the original article here: Use Redis GeoHash with Spring boot

The opinions expressed by Java Code Geeks contributors are their own.

Source link