We can understand what cardinality statistics are directly through an example, such as the data set {1, 2, 3, 3, 5, 5,}, then the cardinality set of this data set is {1,2, 3, 5}, the base (non-repeating elements) is 4. That is to say, it is the number of non-repeating elements.
Each HyperLogLog key only requires 12 KB of memory to calculate the cardinality of close to 2^64 different elements. This is in sharp contrast to a collection that consumes more memory when calculating cardinality. The more elements there are, the more memory is consumed. If you want to compare from a memory perspective, Hyperloglog is the first choice.
uv of web pages (a person visits a website multiple times, but is still counted as one person)
Traditional method: set (because set does not allow repetition, if repeated, it will be overwritten) saves the user's ID, and then statistics can be made. The number of elements in the set is used as a standard judgment. This method will be more troublesome if a large number of user IDs are saved and will be used in large websites. Takes up a lot of memory. Our purpose is to count, not to save user IDs.
Using HyperLogLog: A HyperLogLog key only requires 12KB, but the number that can be calculated is very huge, and the memory space occupied is greatly reduced.
If fault tolerance is allowed (0.81% error rate, it can be ignored when counting), then Hyperloglog can definitely be used! If fault tolerance is not allowed, just use set or your own data type!
Serial number | Command and description |
---|---|
1 | PFADD key element [element ...] Add the specified element to HyperLogLog. |
2 | PFCOUNT key [key ...] Returns the cardinality estimate for the given HyperLogLog. |
3 | PFMERGE destkey sourcekey [sourcekey ...] Merge multiple HyperLogLogs into one HyperLogLog |
127.0.0.1:6379> pfadd mykey1 a b c d e f #给第一组添加数据 (integer) 1 127.0.0.1:6379> pfcount mykey1 #统计mykey1的基数数量 (integer) 6 127.0.0.1:6379> pfadd mykey2 e e f j #给第二组添加数据 (integer) 1 127.0.0.1:6379> pfcount mykey2 #统计mykey2的基数数量 (integer) 3 127.0.0.1:6379> pfmerge mykey3 mykey1 mykey2 # 合并两组 mykey1 mykey2 => mykey3 并集 OK 127.0.0.1:6379> pfcount mykey3 #统计mykey3的基数数量 (integer) 7
Geospatial, which has been launched since version 3.2 of Redis, can calculate geographical location information between two places The distance between people, how many miles around.
???? Friend location
???? View nearby people
???? Taxi distance calculation
Serial number | Commands and descriptions |
---|---|
1 | GEOADD key longitude latitude location name The specified geographical spatial location (latitude, longitude , name) is added to the specified key |
2 | GEOPOS key location name Returns all the given location elements from the key location (latitude and longitude). |
3 | GEODIST key location 1 location 2 unit Returns the distance between two given locations, if two locations If one of them does not exist, the command returns a null value. |
4 | GEORADIUS key Longitude and latitude range numerical unit With the given longitude and latitude as the center, find Out of elements within a certain radius |
5 | GEORADIUSBYMEMBER key Location distance value unit Find out the elements within the specified range element, the center point is determined by the given position element |
6 | GEOHASH key Location 1 Location 2 will return 11 A Geohash string of characters. The closer the two strings are, the closer the distance is. |
7 | zrange key start stop Get the coordinate information in the specified key |
8 | zrem key location Delete the data of the specified target under the specified key |
查询地点经纬度:
城市经纬度查询-国内城市经度纬度在线查询工具
作用:添加地理位置
规则:两级无法直接添加,我们一般会下载城市数据,直接通过java程序一次性导入!
语法:GEOADD key 经度 纬度 地点名称
注意事项
有效的经度从-180度到180度。
有效的纬度从-85.05112878度到85.05112878度。
当坐标位置超出上述指定范围时,该命令将会返回一个错误。
使用
#添加单个信息 127.0.0.1:6379> geoadd address 116.708463 23.37102 shantou (integer) 1 #添加多个信息 127.0.0.1:6379> geoadd address 116.405285 39.904989 beijin 121.472644 31.231706 shanghai (integer) 2
作用:获得指定地点的位置信息(经纬度)
语法:GEOPOS key 地点名称
使用
127.0.0.1:6379> geopos address beijin #获得北京的地理位置 1) 1) "116.40528291463851929" #经度 2) "39.9049884229125027" #纬度
作用:返回两个给定位置之间的距离,如果两个位置之间的其中一个不存在, 那么命令返回空值。
语法:GEODIST key 地点1 地点2 单位
单位参数:
m 表示单位为米。
km 表示单位为千米。
mi 表示单位为英里。
ft 表示单位为英尺。
如果用户没有显式地指定单位参数, 那么 GEODIST 默认使用米作为单位。
使用:
127.0.0.1:6379> geodist address beijin shanghai km #查询北京与上海之间的距离 "1067.5980"
作用:以给定的经纬度为中心, 找出某一半径内的元素。
语法:GEORADIUS key 经度 纬度 范围数值 单位
使用:
#查找以116,39这个经纬度为中心,寻找方圆1500km的城市 127.0.0.1:6379> georadius address 116 39 1500 km 1) "shanghai" 2) "beijin" # 显示到中间距离的位置 127.0.0.1:6379> georadius address 116 39 1500 km withdist 1) 1) "shanghai" 2) "996.7313" 2) 1) "beijin" 2) "106.5063" #显示他人的定位信息 127.0.0.1:6379> georadius address 116 39 1500 km withcoord 1) 1) "shanghai" 2) 1) "121.47264629602432251" 2) "31.23170490709807012" 2) 1) "beijin" 2) 1) "116.40528291463851929" 2) "39.9049884229125027" #筛选出最近的城市以及显示其距离 127.0.0.1:6379> georadius address 116 39 1500 km withdist withcoord count 1 1) 1) "beijin" 2) "106.5063" 3) 1) "116.40528291463851929" 2) "39.9049884229125027" #筛选最近两个城市以及显示其距离 127.0.0.1:6379> georadius address 116 39 1500 km withdist withcoord count 2 1) 1) "beijin" 2) "106.5063" 3) 1) "116.40528291463851929" 2) "39.9049884229125027" 2) 1) "shanghai" 2) "996.7313" 3) 1) "121.47264629602432251" 2) "31.23170490709807012"
作用:找出位于指定范围内的元素,中心点是由给定的位置元素决定。
语法:GEORADIUSBYMEMBER key 地点 距离数值 单位
使用:
#找出距离北京方圆1500km内的城市 127.0.0.1:6379> georadiusbymember address beijin 1500 km 1) "shanghai" 2) "beijin"
作用:将返回11个字符的Geohash字符串,如果两个字符串越接近,那么则距离越近。
语法:GEOHASH key 地点1 地点2
???? 使用:
127.0.0.1:6379> geohash address beijin shantou 1) "wx4g0b7xrt0" 2) "ws4uzy8d030"
作用:获得指定key中坐标信息。
语法:zrange key start stop
使用:
127.0.0.1:6379> zrange address 0 -1 1) "shantou" 2) "shanghai" 3) "beijin"
作用:删除指定key下指定目标的数据。
语法:zrem key 地点
使用:
127.0.0.1:6379> zrem address shanghai (integer) 1
BitMap是通过一个bit位来表示某个元素对应的值或者状态,只有0 和 1 两个状态,其中的key就是对应元素本身。365 天 = 365 bit ,1字节 = 8bit ,也就是说统计一年的用户状态只需要46 个字节左右,所以其能够节省很大的空间。
应用场景
(1)用户签到
(2)统计活跃用户
(3)用户在线状态(在线就设置为1,不在线就设置为0)
使用
需求:记录 周一到周日的打卡
1:表示有打卡
0:表示没有打卡
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 1 (integer) 0 127.0.0.1:6379> setbit sign 5 0 (integer) 0 127.0.0.1:6379> setbit sign 6 0 (integer) 0
查看某一天是否有打卡
127.0.0.1:6379> getbit sign 3 (integer) 1 127.0.0.1:6379> getbit sign 6 (integer) 0
统计本周的打卡记录
127.0.0.1:6379> bitcount sign (integer) 4
The above is the detailed content of How to use the special data types of Redis. For more information, please follow other related articles on the PHP Chinese website!