You can study the debug command provided by redis by yourself, which involves simulating redis abnormal scenarios such as oom, downtime, command execution failure, and redis reloading rdb files and aof files. It takes time to simulate the redis command, etc. We can check the details through DEBUG help
, as shown below
What needs to be used here is debug populate
command, use the following
#### 后面数量代表创建多少个键值 127.0.0.1:6379> DEBUG POPULATE 1000 OK 127.0.0.1:6379> DBSIZE (integer) 1000 127.0.0.1:6379> 127.0.0.1:6379> info memory # Memory used_memory:974368 used_memory_human:951.53K used_memory_rss:5234688 used_memory_rss_human:4.99M ### 查看生成键值(生成时没有指定默认以key做为前缀) 127.0.0.1:6379> keys * 1) "key:32201" 2) "key:59146" 3) "key:10551" ...... 127.0.0.1:6379> get key:796 "value:796"
Before using the shell script, we need to know that even without entering the Redis command line, you can still insert into Redis in the following way Data
### 如果使用默认端口可以直接采用以下命令,如果不是默认端口或默认ip,需要重新指定如下 ### redis-cli -c -h 127.0.0.1 -p 7000 [root@zzf993 bin]# ./redis-cli set name zhangsan OK ### 到redis中查看 127.0.0.1:6379> keys * 1) "name" 127.0.0.1:6379> 127.0.0.1:6379> get name "zhangsan"
With the above knowledge, it is easy for us to create data. We can solve the problem by using a shell script loop
Create 1000 key values in batches
#!/bin/bash ### 需要转到redis-cli的目录 cd /opt/redis/redis-6.0.6/bin/ for i in {1..10000} do echo "key${i} ${i}" ### 如果采用默认端口而且在本机可以这样做,不是默认端口或者本机需要指定 ### redis-cli -c -h 127.0.0.1 -p 7000 redis-cli set key${i} ${i} done
Create a bigkey test data
#!/bin/bash ### 需要转到redis-cli的目录 cd /opt/redis/redis-6.0.6/bin/ for i in {1..10000} do echo "key${i} ${i}" redis-cli hset obj key${i} ${i} done
You need to pay attention when using shell scripts. You need to ensure that the current user has executable permissions. If not, you can directly grant all permissions as shown below
The above is the detailed content of How to use Redis to generate data in batches. For more information, please follow other related articles on the PHP Chinese website!