Knowledge of Redis's three special data types (Geospatial geographic location, Hyperloglog cardinality, Bitmaps bit storage)

1. Geospatial location

1 Introduction

A Redis geospatial index allows storing coordinates and searching them. This data structure can be used to find neighboring points within a given radius or bounding box.

2. Related shell commands

2.1 Rules

The two levels cannot be added directly. Generally, the city data will be downloaded and imported directly through the java program at one time.
Parameters: key
value: latitude, longitude, name
Valid longitude: -180 degrees to 180 degrees Valid
latitude: -85.05112878 degrees to 85.05112878 degrees

2.2 geoadd command (assign value to city)

Assign a value to the city (latitude and longitude)

geoadd china:city 经度 纬度 城市名

For example

geoadd china:city 116.40 39.90 beijing

2.3 geopos command (get the longitude and latitude of the specified city)

Get the current location (must be a real coordinate value)

geopos china:city 城市名1 城市名2

For example

geopos china:city beijing shanghai

2.4 geodist command (get the distance between two cities)

Calculate the distance between two points
Unit:
m means the unit is meters
km means the unit is kilometers
mi means the unit is miles
ft means the unit is feet

geodist china:city 城市名1 城市名2 km

For example,
check the straight-line distance from Shanghai to Beijing

geodist china:city beijing shanghai km

2.5 georadius command (with a given latitude and longitude as the center, find elements within a certain radius)

georadius china:city 经度 纬度 距离值 km

(1) Take the longitude and latitude of 110 and 30 as the center, find the cities within a radius of 1000km

georadius china:city 110 30 1000 km

(2) Display the position of the distance to the center (withdist)

georadius china:city 110 30 1000 km withdist

(3) Display other people's location information (withcoord)

georadius china:city 110 30 1000 km withcoord

(4) Filter out the results at the specified location (withcoord count 1)

georadius china:city 110 30 1000 km withcoord count 1

2.6 georadiusbymember

Find other elements around the specified element

georadiusbymember china:city 城市名 1000 km

For example

georadiusbymember china:city beijing 1000 km

2.7 geohash command

Return a Geohash string of 11 characters.
Convert the two-dimensional latitude and longitude into a one-dimensional string. If the two strings are closer, the distance is closer.

geohash china:city 城市名

For example

geohash china:city beijing

3. Pay attention

The underlying implementation principle of GEO is actually Zset, we can use the Zset command to operate geo
(1) View all elements in the map

zrange china:city 0 -1

(2) Remove the specified element

zrem china:city beijing

Two, Hyperloglog base storage

1. Base

Cardinality refers to (non-repeating elements) = 5, error is acceptable
For example: A{1,3,5,7,8}

2. Introduction

Redis Hyperloglog cardinality statistics algorithm
Advantages: the occupied memory is fixed, and the technology of 2^64 different elements only requires 12KB of memory

3. Related shell commands

3.1 Create a set of elements

pfadd key值 a b c d e f g

For example

pfadd mykey a b c d e f g

3.2 Count the base number of mykey elements

pfcount key值

result

pfcount mykey

3.3 Merging two sets of data

pfmerge key值3 key值1 key值2

For example

pfmerge mykey3 mykey mykey2

4. Pay attention

If fault tolerance is allowed, Hyperloglog can be used, and
if fault tolerance is not allowed, set or other data types can be used.

Three, Bitmaps bit storage

1 Introduction

Statistical user information, active, inactive! Log in, not log in! You can use Bitmaps for punching cards, etc.
Bitmaps bitmaps, data structures, are recorded by manipulating binary bits, and there are only two states of 0 and 1.
365 days = 365bit 1 byte = 8bit 46 bytes or so

2. Related shell commands

2.1 Assignment to the bitmap

setbit 对象 某个属性 对应含义

For example, Monday is
labeled 0
and assigned a value of 1, which means check-in, and assigned a value of 0, which means no check-in. 1




setbit day 0 1
setbit day 1 0
setbit day 2 0
setbit day 3 1

2.2 Check whether to check in

getbit day 2

2.3 Count the number of check-in days

bitcount day

Guess you like

Origin blog.csdn.net/qq_46106857/article/details/128307322
Recommended