# Detailed explanation of three special data types in Redis

In addition to the five basic data types, Redis also has three special data types, namely   Hyperlogs (cardinality Statistics),   Bitmaps   (bitmap) and   geospatial   (geographical location).

## Hyperlogs (cardinality Statistics)

Redis version 2.8.9 updates the Hyperloglog data structure!

• What is cardinality?

For example, A = {1, 2, 3, 4, 5}, B = {3, 5, 6, 7, 9}; Then cardinality (non repeating elements) = 1, 2, 4, 6, 7, 9; (fault tolerance is allowed, i.e. a certain error can be accepted)

• What problems are hyperlogs cardinality statistics used to solve?

This structure can save memory to count various counts, such as the number of registered IP, the number of daily access IP, the number of real-time UV pages, the number of online users, the number of common friends, etc.

• What are its advantages?

For a large website, for example, there are 1 million IPS per day. Roughly, an IP consumes 15 bytes, so 1 million IPS are 15M. The content occupied by each key of HyperLogLog in Redis is 12K, and the theoretical storage is close to 2 ^ 64 values. No matter what the stored content is, it is an algorithm based on cardinality estimation, which can only accurately estimate the cardinality, and can use a small amount of fixed memory to store and identify the only element in the set. Moreover, the estimated cardinality is not necessarily accurate. It is an approximate value with 0.81% standard error (it can be ignored for business scenarios that can accept certain fault tolerance, such as IP statistics, UV, etc.).

• Use of related commands
```127.0.0.1:6379> pfadd key1 a b c d e f g h i	# Create the first set of elements
(integer) 1
127.0.0.1:6379> pfcount key1					# Count the cardinality number of elements
(integer) 9
127.0.0.1:6379> pfadd key2 c j k l m e g a		# Create a second set of elements
(integer) 1
127.0.0.1:6379> pfcount key2
(integer) 8
127.0.0.1:6379> pfmerge key3 key1 key2			# Merge two groups: key1, key2 - > Key3 Union
OK
127.0.0.1:6379> pfcount key3
(integer) 13
```

## Bitmap (bit storage)

Bitmap is the bit graph data structure. It is recorded by operating binary bits. There are only two states: 0 and 1.

• To solve what problem?

For example: Statistics of user information, active or inactive! Login, not login! Punch in, no punch in!   Bitmaps can be used in both States!

How much memory is needed to store a year's punch in status? 365 days = 365 bit, 1 byte = 8bit, about 46 bytes!

• Use of related commands

Use bitmap to record clock outs from Monday to Sunday! Monday: 1 Tuesday: 0 Wednesday: 0 Thursday: 1

```127.0.0.1:6379> setbit sign 0 1
(integer) 0
127.0.0.1:6379> setbit sign 1 1
(integer) 0
127.0.0.1:6379> setbit sign 2 0
(integer) 0
127.0.0.1:6379> setbit sign 3 1
(integer) 0
127.0.0.1:6379> setbit sign 4 0
(integer) 0
127.0.0.1:6379> setbit sign 5 0
(integer) 0
127.0.0.1:6379> setbit sign 6 1
(integer) 0
```

Check whether there is a clock out on a certain day!

```127.0.0.1:6379> getbit sign 3
(integer) 1
127.0.0.1:6379> getbit sign 5
(integer) 0
```

Statistics operation, count the days of clock in!

```127.0.0.1:6379> bitcount sign # By counting the clock in records of this week, you can see whether there is full attendance!
(integer) 3
```

## geospatial (geographic location)

Redis Geo was launched in redis version 3.2! This function can calculate the information of geographical location: the distance between the two places, people within a few miles.

### geoadd

Add geographic location

```127.0.0.1:6379> geoadd china:city 118.76 32.04 manjing 112.55 37.86 taiyuan 123.43 41.80 shenyang
(integer) 3
127.0.0.1:6379> geoadd china:city 144.05 22.52 shengzhen 120.16 30.24 hangzhou 108.96 34.26 xian
(integer) 3
```

rule

Two levels cannot be added directly. We usually download city data (you can query GEO on this website:   www.jsons.cn/lngcode)!

• The effective longitude is from - 180 degrees to 180 degrees.
• The effective latitude ranges from -85.05112878 degrees to 85.05112878 degrees.
```# When the coordinate position exceeds the above specified range, the command will return an error.
127.0.0.1:6379> geoadd china:city 39.90 116.40 beijin
(error) ERR invalid longitude,latitude pair 39.900000,116.400000
```

### geopos

Gets the longitude and latitude of the specified member

```127.0.0.1:6379> geopos china:city taiyuan manjing
1) 1) "112.54999905824661255"
1) "37.86000073876942196"
2) 1) "118.75999957323074341"
1) "32.03999960287850968"
```

To obtain the current positioning, it must be a coordinate value!

### geodist

If it does not exist, it returns null

The units are as follows

• m
• km
• mi miles
• FT ft
```127.0.0.1:6379> geodist china:city taiyuan shenyang m
"1026439.1070"
127.0.0.1:6379> geodist china:city taiyuan shenyang km
"1026.4391"
```

### georadius

People nearby = = > get the addresses of all people nearby, locate and query through the radius

Get a specified number of people

```127.0.0.1:6379> georadius china:city 110 30 1000 km			With 100,30 This coordinate is the center, Find a radius of 1000 km City of
1) "xian"
2) "hangzhou"
3) "manjing"
4) "taiyuan"
127.0.0.1:6379> georadius china:city 110 30 500 km
1) "xian"
127.0.0.1:6379> georadius china:city 110 30 500 km withdist
1) 1) "xian"
2) "483.8340"
127.0.0.1:6379> georadius china:city 110 30 1000 km withcoord withdist count 2
1) 1) "xian"
2) "483.8340"
3) 1) "108.96000176668167114"
2) "34.25999964418929977"
2) 1) "manjing"
2) "864.9816"
3) 1) "118.75999957323074341"
2) "32.03999960287850968"
```

Parameter key longitude latitude radius unit [longitude and latitude of display result] [distance of display result] [number of displayed results]

### georadiusbymember

Displays other members within a radius of the specified member

```127.0.0.1:6379> georadiusbymember china:city taiyuan 1000 km
1) "manjing"
2) "taiyuan"
3) "xian"
127.0.0.1:6379> georadiusbymember china:city taiyuan 1000 km withcoord withdist count 2
1) 1) "taiyuan"
2) "0.0000"
3) 1) "112.54999905824661255"
2) "37.86000073876942196"
2) 1) "xian"
2) "514.2264"
3) 1) "108.96000176668167114"
2) "34.25999964418929977"```

The parameters are the same as georadius

### Geohash (less used)

This command returns an 11 character hash string

```127.0.0.1:6379> geohash china:city taiyuan shenyang
1) "ww8p3hhqmp0"
2) "wxrvb9qyxk0"
```

Convert two-dimensional latitude and longitude into one-dimensional string. The closer the two strings are, the closer the distance is

### bottom

The underlying implementation principle of geo is actually Zset. We can operate geo through the Zset command

```127.0.0.1:6379> type china:city
zset
```

View all elements delete the specified element

```127.0.0.1:6379> zrange china:city 0 -1 withscores
1) "xian"
2) "4040115445396757"
3) "hangzhou"
4) "4054133997236782"
5) "manjing"
6) "4066006694128997"
7) "taiyuan"
8) "4068216047500484"
9) "shenyang"
1)  "4072519231994779"
2)  "shengzhen"
3)  "4154606886655324"
127.0.0.1:6379> zrem china:city manjing
(integer) 1
127.0.0.1:6379> zrange china:city 0 -1
1) "xian"
2) "hangzhou"
3) "taiyuan"
4) "shenyang"
5) "shengzhen"```

Tags: Java Back-end

Posted on Thu, 18 Nov 2021 01:31:11 -0500 by Reformed