In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Today, the editor will share with you the relevant knowledge points about how to use Bitmap in Redis. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
In the daily development process, there is often some bool type data that needs to be accessed. For example, record the number of times users check in in a year. If you sign, it is 1. If you don't sign, it is 0. If you use key-value for storage, each user will have to record 365 times, and when users are in the tens of billions, the storage space required will be huge. To solve this problem, you can use bitmaps in redis.
Bitmaps (bitmap) are also of the string data type. The value of a string type in Redis can store up to 512 MB of content, with each string consisting of multiple bytes and each byte consisting of 8 Bit bits. The bitmap structure uses "bits" to achieve storage, which achieves the purpose of data access by setting bits to 0 or 1, which greatly increases the amount of value storage, which has an upper limit of 2 ^ 32.
A bitmap is essentially an ordinary byte string, that is, an bytes array. You can use the getbit/setbit command to process this bitmap, and the bitmap is structured as follows:
Bitmap is suitable for some specific application scenarios, such as user check-in times, or login times and so on. The picture above shows the number of times a user has checked in to the site in 10 days. 1 represents check-in and 0 indicates non-check-in, which makes it easy to count the user's activity. Instead of using strings directly, each record in the bitmap occupies only one bit bit, which greatly reduces memory usage.
Redis officials also did an experiment. They simulated a system with 128 million users, and then used Redis bitmaps to calculate the "average number of daily users." the final time was about 50ms and took up only 16 MB of memory.
Application principle of bitmap
A website needs to count the check-in records of a user for a year. If it is stored in sring type, it needs 365 key-value pairs. If bitmap storage is used, the user checks in and saves 1, otherwise 0. It will eventually generate 00010101... In such a storage result, there is only one record per day, which is 365 bits in a year, or about 46 bytes. If you only want to count the number of days users check in, you can count the number of 1.
The advantage of bitmap operation, compared with strings, it is not only efficient, but also very space-saving.
Redis's bit array is automatically expanded, and if an offset is set beyond the existing content range, the bit array is automatically expanded.
Bitmap common commands 1) SETBIT command
Used to set or clear a value on a bit, and the return value is the value stored in the original bit. All bits of key are 0 in the initial state, as shown in the following example:
SETBIT key offset value
Where offset represents the offset, starting at 0. Examples are as follows:
127.0.0.1 redis 6379 > SET user:1 aOK# sets the offset offset to 0127.0.0.1 SETBIT user:1 6379 > SETBIT user:1 01 (integer) characters when the character corresponding to the bit is unprintable, the redis will display 127.0.0.1 redis 6379 > GET user:1 "\ xe1" 2) GETBIT command in hexadecimal form
Used to get a value on a bit. Examples are as follows:
127.0.0.1 6379 > GETBIT user:1 0 (integer) 1
Returns 0 when the offset offset is larger than the length of the string, or when key does not exist.
Redis > EXISTS bits (integer) 0redis > GETBIT bits 100000 (integer) 03) BITCOUNT command
Statistics refers to the number with a value of 1 in the positioning interval. The syntax format is as follows:
BITCOUNT key [start end]
Examples are as follows:
127.0.0.1 6379 > BITCOUNT user:1 (integer) 8
By specifying the start and end parameters, you can make the count occur only on specific bytes. The start and end parameters are similar to those of the GETRANGE command, using negative numbers, such as-1 for the last bit and-2 for the second to last bit. .
4) Redis Bitop command
Perform bit operations on one or more string key that hold binary bits and save the results to destkey
Syntax: operation can be any of the four operations AND, OR, NOT, XOR:
BITOP AND destkey key [key...] To logically merge one or more key and save the result to destkey
BITOP OR destkey key [key...] To logically OR one or more key and save the result to-destkey.
BITOP XOR destkey key [key...] To logically XOR one or more key and save the result to destkey
BITOP NOT destkey key, logically negate the given key, and save the result to destkey.
Except for the NOT operation, all operations can accept one or more key as input.
Scenario statistics daily active users
Daily active statistics create a bitmap key, which is set according to the offset of the user's id when the user is active.
The corresponding bit is 1
User check in
Each user creates a key for a bitmap based on a certain day, and the number of days thereafter is offset from the number of days on that day
If the user clicks check in, the corresponding offset is set to 1.
These are all the contents of the article "how to use Bitmap in Redis". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.