Skip to content

Commit

Permalink
Add Geospatial data type example (#3487)
Browse files Browse the repository at this point in the history
* Add Geospatial data type example

* Add missing REMOVE_END
  • Loading branch information
bsbodden authored Aug 8, 2023
1 parent 983b677 commit 4ddc215
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions src/test/java/io/redis/examples/GeoExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//EXAMPLE: geo_tutorial
package io.redis.examples;

// REMOVE_START
import org.junit.Test;
// REMOVE_END
import redis.clients.jedis.GeoCoordinate;
import redis.clients.jedis.UnifiedJedis;
import redis.clients.jedis.args.GeoUnit;
import redis.clients.jedis.resps.GeoRadiusResponse;

import java.util.List;
import java.util.stream.Collectors;

import static org.junit.Assert.assertEquals;

public class GeoExample {
@Test
public void run() {
try (UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379")) {
// REMOVE_START
jedis.del("bikes:rentable");
// REMOVE_END

// STEP_START geoadd
long res1 = jedis.geoadd("bikes:rentable", -122.27652, 37.805186, "station:1");
System.out.println(res1); // 1

long res2 = jedis.geoadd("bikes:rentable", -122.2674626, 37.8062344, "station:2");
System.out.println(res2); // 1

long res3 = jedis.geoadd("bikes:rentable", -122.2469854, 37.8104049, "station:3");
System.out.println(res2); // 1
// STEP_END

// REMOVE_START
assertEquals(1, res1);
assertEquals(1, res1);
assertEquals(1, res1);
// REMOVE_END

// STEP_START geosearch
List<GeoRadiusResponse> res4 = jedis.geosearch(
"bikes:rentable",
new GeoCoordinate(-122.27652, 37.805186),
5,
GeoUnit.KM
);
List<String> members = res4.stream() //
.map(GeoRadiusResponse::getMemberByString) //
.collect(Collectors.toList());
System.out.println(members); // [station:1, station:2, station:3]
// STEP_END

// REMOVE_START
assertEquals("[station:1, station:2, station:3]", members.toString());
// REMOVE_END
}
}
}

0 comments on commit 4ddc215

Please sign in to comment.