In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to use the special data type of Redis". In the daily operation, I believe that many people have doubts about how to use the special data type of Redis. The editor has consulted all kinds of information and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubt of "how to use the special data type of Redis". Next, please follow the editor to study!
1. HyperLogLog cardinality Statistics 1.1.What is the cardinality?
We can understand what cardinality statistics is directly through an example, for example, the data set {1pr 2, 3,5,}, then the cardinal set of this dataset is {1 recorder 2, 3 recorder 5}, and the cardinality (non-repeating elements) is 4. That is to say, the number of non-repeating elements.
1.2 benefits of using cardinality statistics
Each HyperLogLog key costs only 12 KB of memory to calculate the cardinality of nearly 2 ^ 64 different elements. This is in sharp contrast to collections that consume more memory with more elements when calculating the cardinality. Hyperloglog is the first choice if you want to compare from a memory perspective.
1.3 Application scenario
The uv of a web page (a person visits a website many times, but still counts as a person)
The traditional way: set (because set does not allow repetition, overwrite if repeated) to save the user's id, and then you can count the number of elements in set as a standard judgment, this way if you save a large number of user id, it will be more troublesome and will take up a lot of memory in large websites. Our goal is to count, not to save the user id.
Using HyperLogLog: a HyperLogLog key requires only 12KB, but the number of calculations is huge, and the memory footprint is greatly reduced.
1.4 points for consideration
If fault tolerance is allowed (0.81% error rate, which can be ignored if counted), then you can definitely use Hyperloglog! If fault tolerance is not allowed, just use set or your own data type!
1.5 basic command serial number command and description 1PFADD key element [element...]
Adds the specified element to the HyperLogLog. 2PFCOUNT key [key...]
Returns the cardinality estimate for the given HyperLogLog. 3PFMERGE destkey sourcekey [sourcekey...]
Merge multiple HyperLogLog into one HyperLogLog1.6 using 127.0.0.1 pfadd mykey1 a b c d e f # to add data to the first group (integer) 1127.0.1 pfadd mykey1 a b c d e f 6379 > pfcount mykey1 # to count the cardinality of mykey1 (integer) 6127.0.1 > pfadd mykey2 e e f j # to add data to the second group (integer) 1127.0.0.1 mykey2 6379 > pfcount mykey2 # to count the cardinality of mykey2 ) 3127.0.0.1 pfmerge mykey3 mykey1 mykey2 # merge two groups mykey1 mykey2 = > mykey3 Union OK127.0.0.1:6379 > pfcount mykey3 # count the cardinality number of mykey3 (integer) 7 II. Introduction to Geospatial Geolocation 2.1
At the beginning of the Redis3.2 version, Geospatial is launched, which can calculate the geographical location information, the distance between the two places, and people within a few miles.
2.2 usage scenarios
? Friend positioning
? Check the people nearby.
? Calculation of taxi-hailing distance
2.3 basic command serial number command and description 1GEOADD key longitude and latitude location name
Adds the specified geospatial location (latitude, longitude, name) to the 2GEOPOS key location name in the specified key
Returns the position (longitude and latitude) of all given location elements from the key. 3GEODIST key location 1 location 2 Unit
Returns the distance between two given positions, and if one of them does not exist, the command returns a null value. four
GEORADIUS key longitude and latitude range numerical unit
Centering on a given latitude and longitude, find out the elements within a certain radius
5GEORADIUSBYMEMBER key location distance numerical unit
Find out the elements within the specified range, and the central point is determined by the given location element 6GEOHASH key location 1 location 2
An 11-character Geohash string is returned, and the closer the two strings are, the closer they are. 7zrange key start stop
Get the coordinate information in the specified key 8zrem key location
Delete the data of the specified target under the specified key
Query location, longitude and latitude:
Urban longitude and latitude query-domestic urban longitude and latitude online query tool
2.4 detailed explanation 2.4.1 GEOADD
Purpose: adding geolocation
Rules: two levels can not be directly added, we will generally download city data, directly through the java program one-time import!
Syntax: GEOADD key Latitude and Longitude location name
Matters needing attention
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.
Use
# add single message 127.0.0.1 beijin 6379 > geoadd address 116.708463 23.37102 shantou (integer) "add multiple messages 127.0.0.1 integer 6379 > geoadd address 116.405285 39.904989 beijin 121.472644 31.231706 shanghai (integer) 22.4.2 GEOPOS
Function: to obtain the location information (longitude and latitude) of the specified location
Syntax: GEOPOS key location name
Use
127.0.0.1 geopos address beijin # obtain the geographical location of Beijing 1) 1) "116.40528291463851929" # Longitude 2) "39.9049884229125027" # Latitude 2.4.3 GEODIST
Function: returns the distance between two given positions, and if one of them does not exist, the command returns a null value.
Grammar: GEODIST key location 1 location 2 Unit
Unit parameters:
M represents the unit in meters.
Km means the unit is kilometer.
Mi stands for miles.
Ft means the unit is feet.
If the user does not explicitly specify the unit parameter, GEODIST defaults to meters.
Use:
127.0.1 geodist address beijin shanghai km 6379 > query the distance between Beijing and Shanghai "1067.5980" 2.4.4 GEORADIUS
Function: to find out the elements within a certain radius with a given latitude and longitude as the center.
Syntax: GEORADIUS key longitude and latitude range numerical units
Use:
# find around the latitude and longitude of 116539 Looking for the city of 1500km 127.0.0.1 km1 > georadius address 11639 1500 km1) "shanghai" 2) "beijin" # shows the location to the middle distance 127.0.0.1 beijin 6379 > shanghai 11639 1500km withdist1) 1) "shanghai" 2) "996.7313" 2) 1) "beijin" 2) "106.5063" # displays the location information of others 127.0.0.1 1500 km withcoord1) 1) "shanghai" 2) 1) "121.47264629602432251" 2) "31.23170490709807012" 2) "beijin" 2) 1) "116.40528291463851929" 2) "39.9049884229125027" # screening the nearest city and showing its distance from 127.0.0.1km withdist withcoord count 6379 > georadius address 11639 1500 km withdist withcoord count 11) 1) "beijin" 2) "106.5063" 3) 1) "116.40528291463851929" 2) "39.9049884229125027" # screen the nearest two cities and show their distance from 127.0.0.1 georadius address 6379 > georadius address 11639 1500 km withdist withcoord count 21) "beijin" 2) "106.5063" 3) 1) "116.40528291463851929" 2) "39.9049884229125027" 1) "996.7313" 3) 1) "121.47264629602432251" 2) "31.23170490709807012" 2.4.5 GEORADIUSBYMEMBER
Function: find out the elements within the specified range, and the center point is determined by the given location element.
Syntax: GEORADIUSBYMEMBER key location distance numeric unit
Use:
# find the city within the 1500km of Beijing 127.0.0.1 km1 > georadiusbymember address beijin 1500 GEOHASH) "shanghai" 2) "beijin" 2.4.6 GEOHASH
Purpose: an 11-character Geohash string is returned, and the closer the two strings are, the closer they are.
Syntax: GEOHASH key location 1, location 2
? Use:
127.0.0.1 ws4uzy8d030 6379 > geohash address beijin shantou1) "wx4g0b7xrt0" 2) "ws4uzy8d030" 2.4.7 ZRANGE
Function: get the coordinate information in the specified key.
Syntax: zrange key start stop
Use:
127.0.1 zrange address 6379 > beijin 0-11) "shantou" 2) "shanghai" 3) "beijin" 2.4.8 ZREM
Function: delete the data of the specified target under the specified key.
Syntax: zrem key location
Use:
127.0.0.1 BitMap 6379 > zrem address shanghai (integer) 1 III. Introduction to BitMap
BitMap uses a bit bit to represent the corresponding value or state of an element, with only 0 and 1 states, where key is the corresponding element itself. 365 days = 365 bit, 1 byte = 8bit, that is to say, it only takes about 46 bytes to count the user status in a year, so it can save a lot of space.
Application scenario
(1) user check-in
(2) Statistics of active users
(3) user online status (set to 1 if online and 0 if not online)
Use
Demand: record the clock-in from Monday to Sunday
1: indicates that there is a sign-in
0: no sign-in
127.0.1 setbit sign 01 (integer) 0127.0.1 0.1 setbit sign 1 (integer) 0127.0.0.1 setbit sign 6379 > setbit sign 2 0 (integer) 0127.0.1 0.1 setbit sign 6 379 > setbit sign 4 1 (integer) 0127.0.1 0.1 setbit sign 5 0 (integer) 0127.0.1 1 setbit sign 6 379 > setbit sign 60 (integer) 0
Check to see if there is any sign-in one day.
127.0.0.1 getbit sign 6379 > getbit sign 3 (integer) 1127.0.0.1 getbit sign 6 (integer) 0
Count the sign-in records of this week
127.0.1 bitcount sign 6379 > Redis (integer) 4 at this point, the study on "how to use special data types of Redis" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.