Redis: BUSYKEY Target key name already exists - redis

I am trying to copy a specific set of keys from a Redis instance(v4.0.1) to another instance(v5.0.5). For this I am using MIGRATE command with COPY option:
migrate <ip-address of dest redis> 6379 "" 0 5000 COPY KEYS key_1 key_2 key_3
There are around 500 keys and it may be possible that the some keys are getting repeated and some don't even exist in the source.
I am getting the following error:
(error) ERR Target instance replied with error: BUSYKEY Target key
name already exists.
On the destination instance, I got some of the keys. Is this an error that can be ignored and can I be sure that all the existing keys are migrated?

The MIGRATE command internally uses DUMP to generate the serialized version of the key value, and RESTORE in order to synthesize the key in the target instance.
That error happens when on a RESTORE command the key already exists. If you want to replace existing keys on the targe instance, you should use the REPLACE optional argument. Please revise the full optional arguments here. https://redis.io/commands/migrate#options

Related

Is there a way to remove the ttl from all keys in redis?

I'm wanting to a redis instance to be used for development purposes without any expirations in it. Is there way to remove the ttl from all the keys in the database?
The following command via redis-cli will loop through all the keys in the selected database and call the persist command which removes the ttl:
EVAL "for i, name in ipairs(redis.call('KEYS', '*')) do redis.call('persist', name); end" 0

lastsave command for previously saved key

I need to see when the key is saved lastly.
But its returning an error for the command
redis-cli lastsave "mykey"
(error) ERR wrong number of arguments for 'lastsave' command
Read the docs - LASTSAVE accepts 0 arguments and reports the last time that the entire database was persisted.

How to remove redis zset?

I have key "test", the type is zset, I want to remove it.Its value is complex.
127.0.0.1:6379>zrange test 1 2
1) "\x80\x02}q\x01(U\x04bodyq\x02U\x00U\t_encodingq\x03U\x05utf-8q\x04U\acookiesq\x05}q\x06U\x04metaq\a}q\bU\x05depthq\tK\x01sU\aheadersq\n}q\x0bU\aRefererq\x0c]q\rU.http://guba.eastmoney.com/list,002273,f_1.htmlq\x0easU\x03urlq\x0fX0\x00\x00\x00http://guba.eastmoney.com/list,002273,f_334.htmlU\x0bdont_filterq\x10\x89U\bpriorityq\x11K\x00U\bcallbackq\x12U\x05parseq\x13U\x06methodq\x14U\x03GETq\x15U\aerrbackq\x16Nu."
2) "\x80\x02}q\x01(U\x04bodyq\x02U\x00U\t_encodingq\x03U\x05utf-8q\x04U\acookiesq\x05}q\x06U\x04metaq\a}q\bU\x05depthq\tK\x01sU\aheadersq\n}q\x0bU\aRefererq\x0c]q\rU.http://guba.eastmoney.com/list,002273,f_1.htmlq\x0easU\x03urlq\x0fX0\x00\x00\x00http://guba.eastmoney.com/list,002273,f_335.htmlU\x0bdont_filterq\x10\x89U\bpriorityq\x11K\x00U\bcallbackq\x12U\x05parseq\x13U\x06methodq\x14U\x03GETq\x15U\aerrbackq\x16Nu."
127.0.0.1:6379>zcard test
57232
There are so many values, I want to remove them all or some. How can I do this?
You can use DEL command to remove any key from redis database, regardless of the datatype associated with it.
DEL key [key ...]
Removes the specified keys. A key is ignored if it does not exist.
DEL will allow you to remove the key entirely: http://redis.io/commands/del.
ZREM will allow you to remove members from the set: http://redis.io/commands/zrem.
There are additional ZREM* commands that allow the removal of ranges of members - see ZREMRANGEBYLEX, ZREMRANGEBYRANK and ZREMRANGEBYSCORE
You use the "del" command to remove an entry from redis:
del test
See: http://redis.io/commands/del

get/sum values from wildcard keys in redis

I have a string type key value store in redis having keys like this--
/url-pattern/url-slug-1
/url-pattern/url-slug-2
/url-pattern/url-slug-3
/url-pattern/url-slug-4 ...
I can retrieve all the keys of /url-pattern/ using a wild card query like this --
keys /url-pattern/*
I would like to retrieve the values of all keys corresponding to this wildcard /url-pattern/*
I tried this
mget /url-pattern/*
1) (nil)
but it doesnt returned the array as expected.
How can I retrieve the values of all keys corresponding to /url-pattern/*
I also want to do a sum on the values, but I think there is no such thing called SUM() in redis
MGET accepts multiple arguments where each a key name. It does not do key name patterns.
What you could do is first fetch all the relevant key names (do not use KEYS, use SCAN instead) and then fetch their values with an MGET.
Here is an updated answer for 2015.
If you can upgrade Redis above 2.8, the SCAN command with MATCH will work for this. Before that version, not so much, and do NOT use the KEYS command except in a development environment.
http://redis.io/commands/scan
Example on command line:
$ redis-cli
127.0.0.1:6379> scan match V3.0:*
(error) ERR invalid cursor
127.0.0.1:6379> scan 0 match V3.0:*
1) "0"
2) 1) "V3.0:UNITTEST55660BC7E0C5B"
2) "V3.0:shop.domain.com:route"
3) "V3.0:UNITTEST55660BC4A2548"
127.0.0.1:6379> scan 0 match V1.0:*
1) "0"
2) (empty list or set)
127.0.0.1:6379> scan 0 match V3.0:*
1) "0"
2) 1) "V3.0:UNITTEST55660BC7E0C5B"
2) "V3.0:shop.domain.com:route"
3) "V3.0:UNITTEST55660BC4A2548"
Example in PHP:
// Initialize our iterator to NULL
$iterate = null;
// retry when we get no keys back
$redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY);
while ($arr_keys = $redis->scan($iterate, 'match:*')) {
foreach ($arr_keys as $str_key) {
echo "Here is a key: $str_key\n";
}
echo "No more keys to scan!\n";
}
Note, php code is not tested and from the core documentation for example here. Production code would need to be modified depending on the keys needed to look up.
For those on Ubuntu here are the instructions to upgrade php5-redis:
Download the 2.2.7 package here: http://pecl.php.net/package/redis
$ php -i | grep Redis
Redis Support => enabled
Redis Version => 2.2.4
Follow instructions in README to phpize, configure, make install
Create a symlink for command line cli package: cd /etc/php5/cli/conf.d && sudo ln -s ../../mods-available/redis.ini 20-redis.ini
$ php -i | grep Redis
Redis Support => enabled
Redis Version => 2.2.7
There is NO command available in REDIS which can return values from wildcard keys.
If you see the documentation for KEYS command: http://redis.io/commands/keys, it says
Consider KEYS as a command that should only be used in production
environments with extreme care. It may ruin performance when it is
executed against large databases. This command is intended for
debugging and special operations. Don't use KEYS in your regular
application code.
I don't know your business use case, but looks like you may have to use different data structure for this requirement. You can use list or set to store similar url patterns.

Cannot delete a printer queue on OpenVMS

Im trying to delete a printer queue but I always receive the message:
$> delete/que myprinter
%DELETE-E-NOTDELETED, error deleting MYPRINTER
-JBC-E-REFERENCED, existing references prevent deletion
$> sh que/all/full myprinter
Server queue MYPRINTER, stopped, on MYSERVER::, mounted form A4_LINEP_LS (stock=DEFAULT)
/BASE_PRIORITY=4 /DEFAULT=(FEED,FORM=DEFAULT) /OWNER=[SYSTEM] /PROCESSOR=TCPIP$LPD_SMB /PROTECTION=(S:M,O:D,G:R,W:S) /RETAIN
I try the "user action" suggested in this cases - delete all entryes and references to the queue and apparently there is no more references since the $ show que/full/all command shows nothing.
Anyway, ther must have a hiden reference, but where?
Can you help me?
From the OpenVMS System Manager's Manual: the queue you are attempting to delete might be named as a target for a generic queue.... To check for such a target queue do a $ pipe show queue/generic/full |search sys$pipe generic. If your queue shows you want to remove it from the generic queue.