Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Redis

Redis GEOHASH: ดึง Geohash String ของสมาชิก Geospatial หลายรายการ

ในบทเรียนนี้ เราจะเรียนรู้วิธีการดึง Geohash string ของสมาชิกหนึ่งหรือหลายรายการของ geospatial value ที่เก็บใน key โดยใช้คำสั่ง Redis GEOHASH

คำสั่ง GEOHASH

คำสั่งนี้คืนค่า Geohash string ที่ถูกต้องของสมาชิกที่ระบุหนึ่งหรือหลายรายการใน geospatial value ที่เก็บใน key ค่า geospatial ถูกแทนด้วย Sorted Set value ซึ่ง populate ด้วยคำสั่ง GEOADD

Redis แทนตำแหน่ง (longitude, latitude) ของสมาชิก geospatial โดยใช้การแปลงของเทคนิค Geohash ที่ latitude และ longitude bits ถูก interleave เพื่อสร้าง unique 52-bit integer การ encode นี้แตกต่างจากมาตรฐาน เนื่องจาก min และ max coordinates เริ่มต้นที่ใช้ในการ encode และ decode ต่างกัน อย่างไรก็ตาม คำสั่งนี้คืนค่า Geohash string แบบมาตรฐาน

Geohash string แบบมาตรฐานมีคุณสมบัติดังนี้:

  1. ประกอบด้วย 11 ตัวอักษร
  2. สามารถใช้ได้กับ geohash.org
  3. String ที่มี prefix เหมือนกันจะอยู่ใกล้กัน แต่กลับกันไม่จริง อาจมี string ที่ prefix ต่างกันแต่อยู่ใกล้กันได้
  4. สามารถตัดตัวอักษรจากขวาได้ จะสูญเสีย precision แต่ยังชี้พื้นที่เดียวกัน

คืนค่า array of nil เมื่อ key ไม่มีอยู่ และคืนค่า error เมื่อ key มีอยู่แต่ค่าที่เก็บไม่ใช่ Sorted Set datatype ที่ populate ด้วย GEOADD command

Syntax

redis host:port> GEOHASH <keyname> <member> [member ...]

Output

- (array) value: แสดงรายการ Geohash string
- Error: หาก key มีอยู่แต่ค่าที่เก็บไม่ใช่ Sorted Set ที่ populate ด้วย GEOADD

ตัวอย่าง

Redis GEOHASH: ดึง Geohash String ของสมาชิก Geospatial หลายรายการ

อ้างอิง

  1. GEOHASH Command Docs

ขอจบบทเรียนสำหรับวิธีดึง Geohash string ของสมาชิก geospatial value ที่เก็บใน Redis datastore หากชอบบทเรียนนี้ กรุณาแบ่งปันความคิดเห็นและแชร์ให้ผู้อื่นด้วยครับ

<- Geo Commands