当前位置:网站首页>Redis learning and understanding of three special data types
Redis learning and understanding of three special data types
2022-07-28 00:42:00 【Although the sunset is not as beautiful as you】
Preface
Many blogs only talk about five basic types , exactly , Is the most commonly used , And 90% of programmers are interested in Redis Limited to understanding String This is the most commonly used . But I personally think , since Redis Other data types are officially provided , There must be corresponding considerations , In some special business scenarios , These special types can still give us a solution ! that , This learning is worth it !!!
1.Geospatial: Location
City longitude and latitude query : Latitude and longitude query
Be careful 1: Two poles can't be added directly , We usually download city data , Directly through java The program is imported once !
Be careful 2: Effective longitude from -180 C to 180 degree .
Be careful 3: Effective latitude from -85.05112878 C to 85.05112878 degree .
Be careful 4:m For meters .km In kilometers .mi For miles .ft Feet .
①geoadd( add to )、geopos( see )、geodist( Calculated distance ) operation
127.0.0.1:6379> geoadd city 118.8921 31.32751 nanjing 197.30794 31.79322
# When one or both of the longitude and latitude exceed the limit value , Report errors , The information is as follows :
(error) ERR syntax error. Try GEOADD key [x1] [y1] [name1] [x2] [y2] [name2] ...
# Add city longitude and latitude Grammar format : geoadd key longitude latitude name +++ You can add more than one
# After adding successfully, return the quantity value of adding successfully
127.0.0.1:6379> geoadd city 118.8921 31.32751 nanjing 117.30794 31.79322 hefei 102.82147 24.88554 kunming 91.13775 29.65262 lasa 116.23128 40.22077 beijing 106.54041 29.40268 chongqing
(integer) 6
127.0.0.1:6379> ZRANGE city 0 -1 # Be careful :geo How to view and zset The orders are consistent ,
# Thus we can see that ,geo It's essentially a collection , however Redis The official has re encapsulated it
1) "lasa"
2) "kunming"
3) "chongqing"
4) "hefei"
5) "nanjing"
6) "beijing"
127.0.0.1:6379> geopos city nanjing # View the longitude and latitude information of the specified city
1) 1) "118.89209836721420288"
2) "31.32750976275760735"
127.0.0.1:6379> geopos city nanjing beijing # View the longitude and latitude information of multiple cities
1) 1) "118.89209836721420288"
2) "31.32750976275760735"
2) 1) "116.23128265142440796"
2) "40.22076905438526495"
127.0.0.1:6379> geodist city nanjing beijing # Calculate the distance between Nanjing and Beijing , The default return unit is m
"1017743.1413"
127.0.0.1:6379> geodist city nanjing beijing km #km km
"1017.7431"
127.0.0.1:6379> geodist city nanjing beijing mi #mi miles
"632.3978"
127.0.0.1:6379> geodist city nanjing beijing ft #ft feet
"3339052.3010"
②georadius( Query nearby locations ) operation
127.0.0.1:6379> ZRANGE city 0 -1 # View city
1) "lasa"
2) "kunming"
3) "chongqing"
4) "hefei"
5) "nanjing"
6) "beijing"
# View the 1000 Which cities are within kilometers
127.0.0.1:6379> georadius city 120 38 1000 km
1) "beijing"
2) "hefei"
3) "nanjing"
127.0.0.1:6379> georadius city 120 38 400 km # View the 400 Which cities are within kilometers
(empty array)
127.0.0.1:6379> georadius city 120 38 550 km # View the 550 Which cities are within kilometers
1) "beijing"
# View the 550 Which cities are within kilometers ,withcoord Specifies the name of the city to return to name
127.0.0.1:6379> georadius city 120 38 1000 km withcoord
1) 1) "beijing"
2) 1) "116.23128265142440796"
2) "40.22076905438526495"
2) 1) "hefei"
2) 1) "117.30793744325637817"
2) "31.79321915080526395"
3) 1) "nanjing"
2) 1) "118.89209836721420288"
2) "31.32750976275760735"
# View the 550 Which cities are within kilometers ,withdist Specifies the name of the city to return to ’ Longitude and latitude ‘ value
127.0.0.1:6379> georadius city 120 38 1000 km withcoord withdist
1) 1) "beijing"
2) "408.3496"
3) 1) "116.23128265142440796"
2) "40.22076905438526495"
2) 1) "hefei"
2) "732.6371"
3) 1) "117.30793744325637817"
2) "31.79321915080526395"
3) 1) "nanjing"
2) "749.0265"
3) 1) "118.89209836721420288"
2) "31.32750976275760735"
# View the 550 Which cities are within kilometers ,withhash Specifies the name of the city to return to ’ Longitude and latitude ‘ Of hash value
# If two cities hash Value more ’ image ‘, Prove that the closer the city is !
127.0.0.1:6379> georadius city 120 38 1000 km withcoord withdist withhash
1) 1) "beijing"
2) "408.3496"
3) (integer) 4069896088584598
4) 1) "116.23128265142440796"
2) "40.22076905438526495"
2) 1) "hefei"
2) "732.6371"
3) (integer) 4052763834193093
4) 1) "117.30793744325637817"
2) "31.79321915080526395"
3) 1) "nanjing"
2) "749.0265"
3) (integer) 4054278565840695
4) 1) "118.89209836721420288"
2) "31.32750976275760735"
# View the 550 Which cities are within kilometers ,count num Specify return ’num‘ Data volume of cities
127.0.0.1:6379> georadius city 120 38 1000 km withcoord withdist withhash count 2
1) 1) "beijing"
2) "408.3496"
3) (integer) 4069896088584598
4) 1) "116.23128265142440796"
2) "40.22076905438526495"
2) 1) "hefei"
2) "732.6371"
3) (integer) 4052763834193093
4) 1) "117.30793744325637817"
2) "31.79321915080526395"
③ georadiusbymember ( Find the element within the specified range of the specified element )、geohash ( Returns the latitude and longitude hash value )、zrange、zrem( Use zset The command operation geo)
# Query Nanjing 500 Which cities are within kilometers
127.0.0.1:6379> georadiusbymember city nanjing 500 km
1) "hefei"
2) "nanjing"
# Query Chongqing 1500 Which cities are within kilometers
127.0.0.1:6379> georadiusbymember city chongqing 1500 km
1) "lasa"
2) "kunming"
3) "chongqing"
4) "hefei"
5) "nanjing"
6) "beijing"
# Return to the longitude and latitude of Beijing and Nanjing hash value
127.0.0.1:6379> geohash city beijing nanjing
1) "wx4sucvncn0"
2) "wtsd1qyxfx0"
# See all cities name
127.0.0.1:6379> ZRANGE city 0 -1
1) "lasa"
2) "kunming"
3) "chongqing"
4) "hefei"
5) "nanjing"
6) "beijing"
# according to geo Medium name Delete g Elements
127.0.0.1:6379> ZREM city lasa
(integer) 1
# Delete successful
127.0.0.1:6379> ZRANGE city 0 -1
1) "kunming"
2) "chongqing"
3) "hefei"
4) "nanjing"
5) "beijing"
④ summary : In actual demand , We can use it to query people nearby 、 Calculate the distance between two people, etc . Of course , We must combine the required longitude and latitude java Import the code once , Manual query and entry is too time-consuming !
2.Hyperloglog: base
The first thing to understand is base ?
On the mathematical level, it can be said to be : Non repeating elements in two datasets ~
But again Redis in , There may be some error . The official error rate is 0.81%.
Hyperloglog Of advantage : The memory occupied is fixed ,2^64 Elements , Equivalent to just 12kb The memory of . Very efficient !
①pfadd( Add dataset )、pfcount( Statistical data sets )、pfmegre( Merged data set - Automatic weight removal
127.0.0.1:6379> pfadd dataList 1 2 3 4 5 6 7 # Add dataset
(integer) 1
127.0.0.1:6379> pfcount dataList # Elements in the statistical dataset
(integer) 7
127.0.0.1:6379> pfadd dataList1 4 5 6 7 8 9 10 # Add dataset
(integer) 1
127.0.0.1:6379> pfcount dataList1 # Elements in the statistical dataset
(integer) 7
# take dataList and dataList1 The two data sets are combined into a new newdata Data sets , And automatically de duplication
127.0.0.1:6379> pfmerge newdata dataList dataList1
OK
127.0.0.1:6379> pfcount newdata
(integer) 10
② summary : If in actual business , A certain error value is allowed , We can use cardinality statistics to calculate ~ Very efficient ! such as : Website visits , You can use it Hyperloglog To calculate statistics !
3.Bitmap: Bit storage
Bitmap Bitmap , data structure ! It's all operating bits to record , Only 0 and 1 Two states !
①setbit( add to )、getset( obtain )、bitcount( Statistics ) operation
127.0.0.1:6379> setbit login 1 1 # Add Monday logged in by 1
(integer) 0
127.0.0.1:6379> setbit login 2 1
(integer) 0
127.0.0.1:6379> setbit login 3 1
(integer) 0
127.0.0.1:6379> setbit login 4 0 # Added Thursday logged in by 0
(integer) 0
127.0.0.1:6379> setbit login 5 0
(integer) 0
127.0.0.1:6379> setbit login 6 1
(integer) 0
127.0.0.1:6379> setbit login 7 0
(integer) 0
127.0.0.1:6379> getbit login 1 # Get whether to log in on Monday
(integer) 1
127.0.0.1:6379> getbit login 4 # Get whether to log in on Thursday
(integer) 0
127.0.0.1:6379> bitcount login # Count the number of days logged in this week
(integer) 4
② summary : In actual demand , We may need to count the login information of users , Employees' punch in information, etc . As long as it is a business, only Two states Of , We can all use Bitmap To operate !!!
边栏推荐
- Have you ever seen this kind of dynamic programming -- the stock problem of state machine dynamic programming (Part 2)
- View the construction details of Yongzhou dioxin Laboratory
- 相应通道无电压但ADC的值却在大幅变化且不等于0的可能原因
- A few lines of code can easily realize the real-time reasoning of paddleocr. Come and get!
- MATLAB如何将k线图设置为经典红绿配色?
- LeetCode_位运算_中等_137.只出现一次的数字 II
- 头补零和尾补零对FFT输出结果的影响
- Current situation of semiconductor testing equipment Market: the localization rate is still less than 10%!
- Strong collaboration and common development! Intel and Taiyi IOT held a seminar on AI computing box aggregation services
- 网络设备硬核技术内幕 防火墙与安全网关篇 (十一) 零接触办公的奥秘 上
猜你喜欢

҈ straight ҈ Broadcast ҈ Pre ҈ Report ҈ |҈ In hot summer, let's cross the high temperature and "bake" with nono!

MATLAB | MATLAB地形生成:矩形迭代法 · 傅里叶逆变换法 · 分形柏林噪声法

Intel AI practice day issue 56 | explore new trends in industry development
![[meetup preview] openmldb + ONEFLOW: link feature engineering to model training to accelerate machine learning model development](/img/17/15c759aadafc335028d37380903ee7.jpg)
[meetup preview] openmldb + ONEFLOW: link feature engineering to model training to accelerate machine learning model development

Leetcode 415. string addition and 43. string multiplication

Remote monitoring of pump station
![[meetup preview] openmldb + ONEFLOW: link feature engineering to model training to accelerate machine learning model development](/img/29/f31fa3af18198754d2433f47c0c556.jpg)
[meetup preview] openmldb + ONEFLOW: link feature engineering to model training to accelerate machine learning model development

Intel joins hands with hanshuo and Microsoft to release the "Ai + retail" trick!

startUMl

冲量在线出席2022数据要素安全流通论坛—政务领域专场,助力行业政务大数据建设创新发展
随机推荐
Matlab | those matlab tips you have to know (2)
Fastjson历史漏洞复现
The construction of Yongzhou entry exit inspection laboratory
小程序助力智能家居生态平台
头补零和尾补零对FFT输出结果的影响
永州植物细胞实验室建设布局方案
Matlab | those matlab tips you have to know (4)
Redis-事务与乐观锁
Rational and perceptual activities and required skills in programmers' work
The influence of head zeroing and tail zeroing on FFT output
Application scenario Display of metauniverse
智能便利店带你解锁未来科技购物体验
Microsoft Amazon layoffs, the economic crisis is getting closer...
这种动态规划你见过吗——状态机动态规划之股票问题(中)
ASML推出第一代HMI多光束检测机:速度提升600%,适用于5nm及更先进工艺
半导体测试设备市场现状:国产化率仍不足10%!
大众中国豪掷80亿,成国轩高科第一大股东
[BRE]软件构建发布自动化
The latest notice of the Chinese Academy of Sciences: abandon the impact factor! The journal zoning table will be published for the "Journal surpassing index"
How does matlab set the K-line diagram to classic red and green color matching?