Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Three special data types in redis

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article focuses on "three special data types in redis". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "three special data types in redis".

Three special data types of redis

Geospatial geographical location

Hyperloglog cardinality statistics

Bitmap bitmap scene

Geospatial geographical location

Geospatial has been launched in redis version 3.2.

The usage can be seen in detail in the official documentation:

Https://www.redis.net.cn/order/3685.html

Geospatial can be used in the following scenarios:

People in the neighborhood

Calculate the distance by taxi

Friend positioning

Wait for a series of scenes related to positioning.

Geospatial has only six commands

GEOADD

GEODIST

GEOHASH

GEOPOS

GEORADIUS

GEORADIUSBYMEMBER

GEOADD

Add geolocation

Valid longitude ranges from-180 degrees to 180 degrees.

Valid latitudes range from-85.05112878 to 85.05112878 degrees.

When the coordinate location is outside the specified range, the command will return an error.

GEOADD key [NX | XX] [CH] longitude latitude member [longitude latitude member...]

Add latitude and longitude, city name

127.0.0.1 changsha (integer) 1127.0.1 changsha (integer) 1127.0.1 shenzhen (integer) 1127.0.0.1 chengdu (integer) 1127.0.0.1 nanjing (integer) 1127.0.0.1 nanjing (integer) 1127.0.0.1 nanjing 6379 > GEOADD city 106.558434 29.568996 chongqing (integer) 1127.0.0.1 nanjing (integer) 1127.0.1 .195908 shanghai (integer) 1127.0.0.1 GEOADD city 117.208093 39.091103 tianjin (integer) 1GEOPOS

GEOPOS key member [member...]

Get the longitude and latitude information of the specified city

1) "113.08755666017532349" 2) "28.251827470789386" 127.0.0.1 GEOPOS city changsha1 6379 > GEOPOS city tianjin1) 1) "117.20809489488601685" 2) "39.091021322545551" GEODIST

GEODIST key member1 member2 [m | km | ft | mi]

There are four units as follows:

M: M

Km: km

Ft: feet

Mi: miles

Get the distance between two cities

127.0.0.1 GEODIST city changsha shenzhen km 6379 > GEODIST city changsha tianjin "1264101.6876" 127.0.0.1 GEODIST city changsha shenzhen km "641.9034" GEOHASH

GEOHASH key member [member...]

Returns one or more elements represented by GEOHASH and 11-character Geohash strings

127.0.0.1 GEOHASH city changsha beijing1) "wt02tr5fg00" 2) (nil) 127.0.0.1 GEOHASH city changsha beijing1 > GEOHASH city changsha beijing tianjin chongqing1) "wt02tr5fg00" 2) (nil) 3) "wwgq7hk64t0" 4) "wm7b0yc7zk0" GEORADIUS

GEORADIUS key longitude latitude radius m | km | ft | mi [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count [ANY]] [ASC | DESC] [STORE key] [STOR

Specify latitude and longitude as the origin, specify the radius, query the cities within the specified range, these cities are all in our own collection

127.0.0.1 GEORADIUS city 30500 m (empty array) 127.0.0.1 GEORADIUS city 30500 km1) "chongqing" 2) "changsha" 127.0.0.1 > GEORADIUS city 11030 1000 km1) "chongqing" 2) "chengdu" 3) "shenzhen" 4) "changsha" 5) "nanjing" 127.0.0.1 > GEORADIUS city 110 30 700 km withcoord1) 1) "chongqing" 2) 1) .55843228101730347 "2)" 29.56899626404301529 "2) 1)" chengdu "2) 1)" 104.08704489469528198 "2)" 30.6664164635846177 "3)" changsha "2) 1)" 113.08755666017532349 "2)" 28.25181827470789386 "127.0.0.1Ze6379 > GEORADIUS city 11030700 km withdist1) 1)" chongqing "2) 1)" chengdu "2) . 3911 "3) 1)" changsha "2)" 357.4711 "127.0.0.1 km withhash1) 1)" chongqing "2) (integer) 40260594356999312) 1)" chengdu "2) (integer) 40261378317985063) 1)" changsha "2) (integer) 4050903792284309

GEORADIUSBYMEMBER

GEORADIUSBYMEMBER key member radius m | km | ft | mi [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count [ANY]] [ASC | DESC] [STORE key] [STOREDIST key]

Find the city around the specified element

127.0.0.1 GEORADIUSBYMEMBER city tianjin 1000 km1) "nanjing" 2) "tianjin" 3) "shanghai" 127.0.0.1 nanjing 6379 > GEORADIUSBYMEMBER city tianjin 5000 km1) "tianjin" 127.0.0.1 nanjing 6379 > GEORADIUSBYMEMBER city tianjin 5000 km1) "chongqing" 2) "chengdu" 3) "shenzhen" 4) "changsha" 5) "shanghai" 6) "nanjing" 7) "tianjin"

The underlying principle of Geospatial is realized by using Zset ordered sets. We can use the commands of Zset ordered sets to operate Geo, and we use the instructions of Zset to feel a wave.

127.0.0.1 ZRANGE city 0-11) "chongqing" 2) "chengdu" 3) "shenzhen" 4) "changsha" 5) "shanghai" 6) "nanjing" 7) "tianjin" 127.0.1 chengdu 6379 > ZCARD city (integer) 7Hyperloglog cardinality statistics

What's the cardinal number?

Cardinality, which is a number that is not repeated, for example:

A = {1, 2, 3, 4, 5}

B = {2pm 3pm 4pm 5pm 6}

Then the cardinality of the union of An and B is 6.

Brief introduction

Hyperloglog is the kind of data structure that began with redis version 2.8.9.

Redis hyperloglog cardinality statistics is also an algorithm.

For example, there are such application scenarios:

According to the statistics of the number of web page visitors, multiple visits by the same user can only be counted as 1.

The traditional way is to use the set collection to save the id, count the number of id in the set and count the number of people.

There is no problem with this method, but when there is a large amount of data, it will be very troublesome, because it is useless for us to take id, we just need to count.

Advantages

The memory space occupied by Hyperloglog is fixed, 2 ^ 16, which only takes up 12 KB of memory. From the point of view of memory, Hyperloglog is the first choice.

PFADD key element [element...]

Add one or more elements to the Hyperloglog

PFMERGE destkey sourcekey [sourcekey...]

Merge multiple Hyperloglog to get a result Hyperloglog, in which the data will not be repeated.

127.0.0.1 PFADD myhash 6379 > pfadd myhash3 12 3 4 5 6 7 7 8 (integer) 1127.0.0.1 6 6379 > pfadd myhash3 3 4 5 6 7 9 0 88 99 (integer) 1127.0.1 PFMERGE res myhash myhash3OK127.0.0.1:6379 > PFCOUNT res (integer) 12127.0.16379 > PFCOUNT myhash (integer) 8127.0.0.16379 > PFCOUNT myhash3 (integer) 10Bitmaps bitmap scene

Bitmaps bitmap, bit storage

Generally used to count user information, active, inactive

Log in, do not log in

Sign in, no sign in, etc., two states

Bitmaps bitmap, which is also a data structure, is recorded in a binary way, with only 0 and 1 states.

For example, let's take an example to record the mood every day of the week. 1 is happy, 0 is depressed.

SETBIT key offset value

Set the value of the bit bit

GETBIT key offset

Get the value of the bit bit

127.0.0.1 SETBIT week 01 (integer) 0127.0.0.1 SETBIT week 1 (integer) 0127.0.0.1 SETBIT week 6379 > integer 2 1 (integer) 0127.0.1 SETBIT week 6379 > SETBIT week 3 0 (integer) 0127.0.1 0.1 SETBIT week 4 0 (integer) 0127.0.1 SETBIT week 6 379 > SETBIT week 5 1 (integer) 0127.0.0.1 SETBIT week 6 1 (integer) 0127.0.0.1: 6379 > GETBIT week 6 (integer) 1127.0.0.1 GETBIT week 5 (integer) 1 I believe that you have a deeper understanding of the "three special data types in redis". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report