I heard that searching “Java Fish Boy” on wechat will improve my skills to the next level
Advanced data types are not new data structures, unlike the five basic data types. Advanced data types are often used to solve business scenarios.
(a) BitMaps
(1.1) BitMaps Overview
In the application scenario, some data have only two attributes, such as whether it is a student or a Party member, etc. For these data, the most memory saving way is to use bit to record, for example, whether it is a student, 1 means a student, 0 means not a student. So 1000110 means that 3 out of 7 people are students, which is the storage requirement for BitMaps. Bitmaps are strings that can be manipulated with bits. We can think of Bitmaps as a string of binary digits that store only zeros and ones at each location. The subscript is the offset of Bitmaps.
(1.2) BitMaps operation
Gets the bit value of the offset corresponding to the specified key
getbit key offset
Copy the code
Set the bit value of the offset of the specified key. The value can be 1 or 0
setbit key offset value
Copy the code
Perform the join, union, non, xOR operation on the specified key bit by bit and save the result to the destKey
bitop and destKey key1 key2.... DestKey key1 key2.... // Bitop not destKey key1 key2.... // Non-bitop xor destKey key1 key2.... / / orCopy the code
Counts the number of 1s in a specified key
bitcount key start end
Copy the code
(1.3) BitMaps application scenario
Let’s say a company wants to count the daily/weekly/yearly visits of all their registered members to the site and uses BitMaps. The calculation method of daily login members is as follows: set up a BitMaps with key value as the current date. When member with ID 5 logs in, set the offset as 4 (subscript starts from 0), set the number with offset as 4 as 1, and so on. Use the bitcount command to count the number of members logged in each day. The method of counting the number of weekly login members is as follows: To count the number of weekly login members, just use the data of 7 days a week and calculate (OR). If a member’s value is equal to 1 on any given day, then after the operation, his value is equal to 1, indicating that he logged in that week. Then use bitcount to calculate.
(2) HyperLogLog
HyperLogLog is used to do cardinal statistics. The so-called cardinal statistics refers to the number of non-repeated digits in a string of numbers. For example, {1,2,1,2,3} has a cardinal of 3.
Add data:
pfadd key element1 element2...
Copy the code
statistics
pfcount key1 key2...
Copy the code
Merge data
pfmerge destkey sourcekey1 sourcekey2...
Copy the code
HyperLogLog:First, HyperLogLog can only record data. Due to the core cardinality estimation algorithm, there is a 0.81% error when the number is large. The footprint is small, with only 12K of memory for each hyperLogLog. The pfadd command does not allocate 12K of data at once, but increases the memory size as the cardinality increases. After the pfmerge command is merged, the storage space occupied by the command is 12 KB, regardless of the previous data amount.
(3) the GEO
GEO is an advanced data type of location calculation in Redis. For example, nearby friends in wechat will show the distance between friends and you, which is an application of GEO.
Add coordinate points:
geoadd key longitude latitude member [longitude latitude member...]
Copy the code
Get coordinate point
geopos key member
Copy the code
Computed coordinate distance
Geodist key member1 Member2 [unit] Unit indicates the unit. The default is M. Km, ft, mi can be setCopy the code
Let me write the horizontal and the vertical coordinates in simple numbers for simplicityCalculate the range of data according to the coordinates (withcoord shows the coordinates, withdist shows the distance, withhash shows the hash value, count count is used to get the range)
georadius key longitude latitude radius m|km|ft|mi [withcoord] [withdist] [withhash] [count count]
Copy the code
Calculate the range data according to the points
georadiusbymember key member radius m|km|ft|mi [withcoord] [withdist] [withhash] [count count]
Copy the code
Gets the hash value of the coordinates of the specified point
geohash key member1 member2...
Copy the code
(4) Summary
In actual application development, only special business needs can use these advanced data types, so you don’t need to master the specific methods, but you need to know what each high-level data type is, and then look up more detailed information when you use it.