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

companies:
  redis:
    picture: redis
    ports:
      - 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="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <groupId>org.instance</groupId>
    <model>1.0-SNAPSHOT</model>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>location-service</artifactId>

    <properties>
        <maven.compiler.supply>11</maven.compiler.supply>
        <maven.compiler.goal>11</maven.compiler.goal>
        <mission.construct.sourceEncoding>UTF-8</mission.construct.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <model>2.7.5</model>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <model>1.18.24</model>
            <scope>supplied</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <model>2.7.5</model>
        </dependency>
    </dependencies>
</mission>

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;

@Configuration
public class RedisConfiguration {

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

}

Our mannequin can be this one

bundle org.location;

import lombok.Knowledge;

@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 java.util.stream.Collectors;

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;

@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()
                  .map(GeoResult::getContent)
                  .map(GeoLocation::getName)
                  .accumulate(Collectors.toList());
    }

}

We will additionally add a controller

bundle org.location;

import java.util.Checklist;

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

@RestController
public class LocationController {

    personal remaining GeoService geoService;

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

    @PostMapping("/location")
    public ResponseEntity<String> addLocation(@RequestBody Location location) {
        geoService.add(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,
	"lat":-0.0814374,
	"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'
> [
    "liverpool-street"
]

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.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments