This is the fourth day of my participation in the More text Challenge. For details, see more text Challenge

This article is participating in the “Java Theme Month – Java Development in action”. See the link to the event for more details


Related articles

Redis combat summary: Redis combat


preface

Many blogs cover only the five basic types, which, indeed, are the most commonly used, and 90 percent of programmers in Redis are limited to the most commonly used type, String. However, I personally think that since Redis officially provides other data types, there must be corresponding considerations, in some special business scenarios, these special types can still give us an additional solution! Then the study was worth it!!

1. Geospatial:The geographical position

City latitude and longitude query: latitude and longitude query note 1: the poles can not be directly added, we will generally download city data, directly through the Java program one-time import! Note 2: The valid longitude ranges from -180 degrees to 180 degrees. Note 3: The valid latitude ranges from -85.05112878 degrees to 85.05112878 degrees. Note 4: m is meters. Km is kilometers. Mi is miles. Ft is feet. ① Geoadd (add), geopos (view), geodist (calculate distance) operations

127.0.0.1:6379> geoadd city 118.8921 31.32751 nanjing 197.30794 31.79322  
# when one or both of the latitude and longitude exceeds the limit values, error:
(error) ERR syntax error. Try GEOADD key [x1] [y1] [name1] [x2] [y2] [name2] ...
Geoadd key name +++
# Return the value of the number of successful additions
127.0.0.1:6379> geoadd city 118.8921 31.32751 nanjing 117.30794 31.79322 hefei 102.82147 24.88554 kunming 91.13775 29.65262 lasa 116.23128 40.22077 beijing 106.54041 29.40268 chongqing  
(integer) 6 127.0.0.1:6379> ZRANGE city 0-1# geo = zset; # geo = zset;
# Geo is essentially a collection, but Redis has officially repackaged it
1) "lasa"
2) "kunming"
3) "chongqing"
4) "hefei"
5) "nanjing"
6) "beijing"127.0.0.1:6379 > geopos city workingSelect * from city where latitude and longitude are specified
1) 1) "118.89209836721420288"
   2) "31.32750976275760735"
127.0.0.1:6379> geopos city nanjing beijing  Check the latitude and longitude information of multiple cities
1) 1) "118.89209836721420288"
   2) "31.32750976275760735"
2) 1) "116.23128265142440796"
   2) "40.22076905438526495"
127.0.0.1:6379> geodist city nanjing beijing   Calculate the distance between Nanjing and Beijing, default return unit is m
"1017743.1413"
127.0.0.1:6379> geodist city nanjing beijing km  # km kilometers
"1017.7431"
127.0.0.1:6379> geodist city nanjing beijing mi  # mi miles
"632.3978"
127.0.0.1:6379> geodist city nanjing beijing ft  # ft feet
"3339052.3010"
Copy the code

② Georadius (query nearby location) operation

127.0.0.1:6379> ZRANGE city 0-1# Check cities
1) "lasa"
2) "kunming"
3) "chongqing"
4) "hefei"
5) "nanjing"
6) "beijing"
Check which cities are within 1000 km of the specified location
127.0.0.1:6379> georadius city 120 38 1000 km  
1) "beijing"
2) "hefei"
3) "nanjing"
127.0.0.1:6379> georadius city 120 38 400 km  Check which cities are within 400 kilometers of the designated location
(empty array)
127.0.0.1:6379> georadius city 120 38 550 km  Check which cities are within 550 km of the designated location
1) "beijing"
Withcoord specifies the name of the city to return to
127.0.0.1:6379> georadius city 120 38 1000 km withcoord
1) 1) "beijing"
   2) 1) "116.23128265142440796"
      2) "40.22076905438526495"
2) 1) "hefei"
   2) 1) "117.30793744325637817"
      2) "31.79321915080526395"
3) 1) "nanjing"
   2) 1) "118.89209836721420288"
      2) "31.32750976275760735"
# Check which cities are within 550 km of the specified location. Withdist specifies the value of the longitude and latitude returned to the city
127.0.0.1:6379> georadius city 120 38 1000 km withcoord withdist
1) 1) "beijing"
   2) "408.3496"
   3) 1) "116.23128265142440796"
      2) "40.22076905438526495"
2) 1) "hefei"
   2) "732.6371"
   3) 1) "117.30793744325637817"
      2) "31.79321915080526395"
3) 1) "nanjing"
   2) "749.0265"
   3) 1) "118.89209836721420288"
      2) "31.32750976275760735"
# Check which cities are within 550 km of the specified location. Withhash specifies the hash value to return the longitude and latitude of the city
The more similar the hash values of two cities are, the closer the cities are!
127.0.0.1:6379> georadius city 120 38 1000 km withcoord withdist withhash
1) 1) "beijing"
   2) "408.3496"(3)integer), 4069896088584598 (4) 1)"116.23128265142440796"
      2) "40.22076905438526495"
2) 1) "hefei"
   2) "732.6371"(3)integer), 4052763834193093 (4) 1)"117.30793744325637817"
      2) "31.79321915080526395"
3) 1) "nanjing"
   2) "749.0265"(3)integer), 4054278565840695 (4) 1)"118.89209836721420288"
      2) "31.32750976275760735"
Select * from 'num' where 'num' = 'num' where 'num' = 'num' where 'num' = 'num' where 'num' = 'num
127.0.0.1:6379> georadius city 120 38 1000 km withcoord withdist withhash count 2
1) 1) "beijing"
   2) "408.3496"(3)integer), 4069896088584598 (4) 1)"116.23128265142440796"
      2) "40.22076905438526495"
2) 1) "hefei"
   2) "732.6371"(3)integer), 4052763834193093 (4) 1)"117.30793744325637817"
      2) "31.79321915080526395"
Copy the code

③ georadiusbymember (find elements within the specified range of the specified element), geohash (return the hash value of latitude and longitude), zrange, zrem (operate geo with zset command)

Check the number of cities within 500 km of Nanjing
127.0.0.1:6379> georadiusbymember city nanjing 500 km
1) "hefei"
2) "nanjing"
Select * from chongqing where there are cities
127.0.0.1:6379> georadiusbymember city chongqing 1500 km
1) "lasa"
2) "kunming"
3) "chongqing"
4) "hefei"
5) "nanjing"
6) "beijing"
# Return the hash values for the longitude and latitude of Beijing and Nanjing
127.0.0.1:6379> geohash city beijing nanjing
1) "wx4sucvncn0"
2) "wtsd1qyxfx0"
# Check all city names127.0.0.1:6379> ZRANGE city 0-1 1)"lasa"
2) "kunming"
3) "chongqing"
4) "hefei"
5) "nanjing"
6) "beijing"
Select * from geo where name = g
127.0.0.1:6379> ZREM city lasa
(integer1)# Delete succeeded127.0.0.1:6379> ZRANGE city 0-1 1)"kunming"
2) "chongqing"
3) "hefei"
4) "nanjing"
5) "beijing"
Copy the code

④ Conclusion: In actual demand, we can use it to query nearby people, calculate the distance between two people, etc. Of course, the required latitude and longitude we must be combined with Java code to import, manual query and input too much time!

2. Hyperloglog:base

First of all, what is a cardinal number? At the mathematical level, it can be said that the elements in two data sets are not repeated, but in Redis, there may be certain error. The official margin of error was 0.81 percent. Hyperloglog advantages: memory is fixed, 2^64 elements, equivalent to only 12kb of memory can be. Extremely efficient! ① PFADD (add data set), PFCount (Statistical data set), PFMEGRE (Merge data set – automatic deweighting)

127.0.0.1:6379> pfadd dataList 12 3 4 5 6 7# Add data set
(integer) 1
127.0.0.1:6379> pfcount dataList  # Elements in a statistical data set
(integer) 7
127.0.0.1:6379> pfadd dataList1 4 5 6 7 8 9 10  # Add data set
(integer) 1
127.0.0.1:6379> pfcount dataList1  # Elements in a statistical data set
(integer7)# Merge the dataList and dataList1 datasets into a new newData dataset and automatically deduplicate it
127.0.0.1:6379> pfmerge newdata dataList dataList1  
OK
127.0.0.1:6379> pfcount newdata
(integer10)Copy the code

② Conclusion: if in the actual business, allow a certain error value, we can use the base statistics to calculate ~ very high efficiency! For example: website visits, you can use Hyperloglog to calculate statistics!

3. Bitmap:A storage

Bitmap data structure Both operate binary bits to record, there are only 0 and 1 two states! (1) Setbit, getSet, and bitCount operations

127.0.0.1:6379> setbit login 1 1   # Add Monday logged in as 1
(integer) 0
127.0.0.1:6379> setbit login 2 1
(integer) 0
127.0.0.1:6379> setbit login 3 1
(integer) 0
127.0.0.1:6379> setbit login 4 0  # Add Thursday already logged to 0
(integer) 0
127.0.0.1:6379> setbit login 5 0
(integer) 0
127.0.0.1:6379> setbit login 6 1
(integer) 0
127.0.0.1:6379> setbit login 7 0
(integer) 0
127.0.0.1:6379> getbit login 1  Get whether to log in on Monday
(integer) 1
127.0.0.1:6379> getbit login 4  Get whether to log in on Thursday
(integer) 0
127.0.0.1:6379> bitcount login  # Count the number of days logged in this week
(integer4)Copy the code

(2) Summary: In actual demand, we may need to count the login information of users, employees’ clocking information and so on. If a transaction has only two states, we can use Bitmap to manipulate it!!


There is a long way to go, but I will continue to search for more and more. This is the end of three special types of Redis, if you think I blogger has done a good job! Writing is not easy, please like, follow, comment to give the blogger a encouragement ~