(Turn) Redis executes commands in batches from files

http://blog.csdn.net/u012689336/article/details/53332653

 

In Redis , if you want to execute some commands in batches, under the redis -cli of redis, you can only execute commands one by one, which is too troublesome! 

 

If you store the commands to be executed line by line in a file, and then execute them all at once.

 

1. Create a file

 

First create a txt file and write the commands to be executed line by line.

[sparkadmin@hadoop4 redis-3.2.4]$ vim d1.txt 
set mykey1 value1
zadd sortedsort 0 a 1 b 3 c
sadd sort mongodb mysql oracle
set mykey2 value2
hmset hash name "redis" description "redis basic commands for caching" likes 20

 

 

2. Execute the import

 

The import uses a combination of cat and redis-cli commands, one is used to read the file content, and the other is used to send the file to redis for execution. If the file to be imported is on the same server as redis, you can directly import the instructions in the local file into redis implement

 

[sparkadmin@hadoop4 redis-3.2.4]$ cat d1.txt | redis-cli -a runoob
OK
(integer) 3
(integer) 3
OK
OK

 

 

 

We can see how many lines you enter, how many lines will return records, and tell you their execution results. If you import a lot of instructions, you can use the --pipe parameter to enable the pipe protocol, which not only It can reduce the output of the returned result and execute the instruction faster.

 

[sparkadmin@hadoop4 redis-3.2.4]$ cat d1.txt | redis-cli -a runoob --pipe
All data transferred. Waiting for the last reply...
ERR unknown command 'add'
ERR unknown command 'add'
ERR unknown command 'et'
ERR wrong number of arguments for MSET
Last reply received from server.
errors: 4, replies: 5

 

 

 

If this prompt does not know the command, it is because redis-cli only supports the newline character \r\n in doc format. If you create a file under Linux , Mac or Windows, it is best to convert the code. Without transcoded files, the execution will fail. 

 

[sparkadmin@hadoop4 redis-3.2.4]$ unix2dos d1.txt 
-bash: unix2dos: command not found

 

Prompt that without this command, you need to install.

 

[sparkadmin@hadoop4 redis-3.2.4]$ sudo yum install unix2dos -y

 

 

 

Then transcode:

 

[sparkadmin@hadoop4 redis-3.2.4]$ unix2dos d1.txt             
unix2dos: converting file d1.txt to DOS format ...

 

[sparkadmin@hadoop4 redis-3.2.4]$ cat d1.txt | redis-cli -a runoob --pipe
All data transferred. Waiting for the last reply...
Last reply received from server.
errors: 0, replies: 5

 

 

 

If you want to import data to a remote machine, you can use the following method to import the file to the remote server:

 

[sparkadmin@hadoop4 redis-3.2.4]$ cat d1.txt | redis-cli -a runoob -p 6380 -h 192.168.1.100 --pipe

All data transferred. Waiting for the last reply...
Last reply received from server.
errors: 0, replies: 5

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326311711&siteId=291194637