ในบทเรียนนี้ เราจะเรียนรู้วิธีการทำ CRUD operation บน geo value โดยใช้ Jedis library
Jedis Library
Jedis เป็น Java client library สำหรับ Redis datastore มีขนาดเล็ก ใช้งานง่าย และรองรับ Redis 2.8.x, 3.x.x และขึ้นไป
Geo Value
Geospatial value ประกอบด้วยพิกัด longitude และ latitude ของตำแหน่งบนโลก ใน Redis geospatial elements ถูกเก็บใน Sorted Set value ที่ key
Project Setup
สร้าง Maven project และเพิ่ม dependency ใน pom.xml:
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.0.1</version> </dependency>
Redis Installation
ติดตั้ง Redis เวอร์ชันล่าสุด
Jedis Connection
Jedis jedis = new Jedis();
Create & Add
geoadd เพิ่มสมาชิก geospatial หนึ่งหรือหลายรายการใน geospatial value (Sorted Set) ที่ key:
jedis.geoadd("geo-1", 51.5074, 0.1278, "London");
Map<String,GeoCoordinate> map = new HashMap<>();
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 ดึง GeoCoordinate (longitude, latitude) ของสมาชิก:
List geoCoordinates = jedis.geopos("geo-1", "London", "Paris");
Get GeoHash
geohash ดึง Geohash string ที่ถูกต้องของสมาชิก:
List geoStrings = jedis.geohash("geo-1", "London", "Paris");
Distance between two members
geodist หาระยะห่างระหว่างสองสมาชิกในหน่วยที่ระบุ (m, km, mi, ft):
Double distance = jedis.geodist("geo-1", "London", "Paris", GeoUnit.KM);
อ้างอิง
- Geo Command Docs
ขอจบบทเรียนสำหรับวิธีทำ CRUD operation บน geo value โดยใช้ Jedis library หากชอบบทเรียนนี้ กรุณาแบ่งปันความคิดเห็นและแชร์ให้ผู้อื่นด้วยครับ