This is the 26th day of my participation in the August More Text Challenge

Three special data types

πŸ₯ Redis, as a high-performance NoSQL database, provides us with three special data types Geospatal, HyperLogLog and BitMaps in addition to five basic data types. In the previous article, we introduced the five basic data types of Redis. We will focus on these three special data types.

Geospatal

Geospatal is Redis3.2.0 version of the launch of a data structure, through which the user can be given geographical location information stored, and the operation of these information, used to achieve such as computing the distance between two locations, search for people nearby such functions dependent on geographical location information, The Geospatal underlying layer is implemented by Zset.

Common operation

  • Geoadd key longitude latitude geoadd key longitude latitude memberAdds the geographic location of the specified latitude and longitude and its name to the specified zset collection. Multiple locations can be added at a time. The return value is the number of locations added to the zset ordered collection. For example, add a latitude and longitude116.413384 39.910925(represents the latitude and longitude of Chaozhou, separated by Spaces) to the set with key as Cityzset, as follows

  • geopos key member [member ...]Obtain the specified latitude and longitude according to the key and member name. For example, obtain the geographical location of Chaozhou saved above, support to obtain more than one time.

  • geodist key member1 member2 [m|km|ft|mi]]Returns the distance between two given locations. This can be used to find the distance between two geographical locations. If one of the two positions does not exist, the command returns a null value. Where the unit argument must be one of the following units (default: m) :
    • mRepresents the unit in meters.
    • kmThe unit is kilometers.
    • miRepresents miles.
    • ftThe unit is feet.

Example: Calculate the distance between Guangzhou and Beijing.

  • Georadius key longitude latitude (longitude) (latitude) radius [m | km | ft | mi]] [withcoord] [withdist] can be realized through the command for the center with the given latitude and longitude, Find elements within the specified radius, as can be applied to find nearby people.

  • Georadiusbymember key member radius [m | km | ft | mi]] [withcoord] [withdist] to find the other elements around the specified element, with a command, Georadiusbymember searches based on member and the specified search distance RADIUS, and saves the search results to another collection.

  • zrem key member [member …] The underlying layer of geo is Zset, so you can use zrem key member to remove specified member elements

  • Zrange key min Max Displays all member elements corresponding to the specified key

HyperLogLog

HyperLogLog is a data structure that implements cardinality statistics. What else is the cardinal number? For example, if there are two sets A and B, their contents are A{1, 3, 5}, B{2, 4, 4, 6, 8} respectively, then the cardinal number of set A is 3, and the cardinal number of set B is 4. From this simple example, we can get the so-called cardinal number, which is the number of non-repeating elements. HyperLogLog provided by Redis has a fixed memory footprint and only needs 16KB of content to store 2^64 different elements. It is often used for UV statistics on websites.

Common operations

  • pfadd key element1 element2 [element…] Add 1 or n elements to the collection corresponding to the specified key

  • pfmerge destkey sourcekey1 sourcekey2 [sourcekey….] Merges the cardinality of n specified elements into the cardinality of the specified destkey element.

  • Pfcount key Counts the cardinality of the specified key (automatic de-weighting)

The following is an example:

BitMaps

A bitmap is not an actual data type, but rather a set of bit-oriented operations defined on a String. Since the string is binary safe and has a maximum length of 512MB, it can be set to up to 2^32 different bits, which are recorded by manipulating the binary bits. There are only two states, 0 and 1, which can be used to count daily user punches.

Common operations

  • Setbit key offset value Offset and value must be Integer and value can only be 0 or 1

  • getbit key offset

  • Bitcount key [start end] Counts the number of values 1 in the specified range

  • bitop AND|OR|NOT|XOR destkey key [key…] The command can perform bitwise operations between different strings. The bitwise operations can be logical union, logical OR, logical xOR, and the result is saved in destkey.

  • Bitpos key bit [start] [end] Searches for the first digit in the specified range that is 0 or 1.

Application: Make statistics of employees’ weekly attendance (0 represents absence, 1 represents attendance) :

🏁 the above is a brief introduction to the three special data types of Redis. If there is any error, please leave a comment. If you think this article is helpful, please give a thumbs up to πŸ‘