A very obvious requirement in certain cases to get all the keys and values stored in the redis DB on your linux server.
Below code will help to get the all keys and value in a one sort ,
Note: Below code uses hiredis library , and must be compiled with -lhiredis. hiredis is available download on internet.
-------------------Snip-------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hiredis.h"
int main(int argc, char* argv[])
{
char *ip, **str1;
unsigned int count=0, port=0;
redisContext *connContext;
redisReply *replyKeys, *replyKeyValue;
if (argc > 2) {
ip = argv[1];
port = atoi(argv[2]);
}
else {
printf("Error:: Incorrect num of argument\n"
"Usage : ./a.out <IP_ADDR> <PORT>\n");
exit(1);
}
struct timeval timeout = { 1, 500000 };
connContext = redisConnectWithTimeout(ip, port, timeout);
if (connContext->err) {
printf("Connection error: %s\n", c->errstr);
exit(1);
}
replyKeys = redisCommand(connContext,"keys *");
printf("KEY\t\tVALUE\n" "------------------------\n");
while ( replyKeys->element[count]->str != NULL)
{
replyKeyValue = redisCommand(connContext,"GET %s", replyKeys->element[count]->str);
if (strstr(relyKeyValuep->str,"ERR Operation against a key holding")) {
printf("%s\t\t%s\n", replyKeys->element[count]->str,replyKeyValue->str);
break;
}
printf("%s\t\t%s\n", replyKeys->element[count]->str,replyKeyValue->str);
count++;
freeReplyObject(replyKeyValue);
}
return;
}
-------------------Snip-------------------
Could be complied as shown below:
-------------------Snip-------------------
-------------------Snip-1-------------------
[root@localhost hiredis_redis]# gcc -m32 getAllKeyValue.c -o test -lhiredis
[root@localhost hiredis_redis]# ./test 127.0.0.1 6391
KEY VALUE
------------------------
counter 24
foo hello world
bar hello
Below code will help to get the all keys and value in a one sort ,
Note: Below code uses hiredis library , and must be compiled with -lhiredis. hiredis is available download on internet.
-------------------Snip-------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hiredis.h"
int main(int argc, char* argv[])
{
char *ip, **str1;
unsigned int count=0, port=0;
redisContext *connContext;
redisReply *replyKeys, *replyKeyValue;
if (argc > 2) {
ip = argv[1];
port = atoi(argv[2]);
}
else {
printf("Error:: Incorrect num of argument\n"
"Usage : ./a.out <IP_ADDR> <PORT>\n");
exit(1);
}
struct timeval timeout = { 1, 500000 };
connContext = redisConnectWithTimeout(ip, port, timeout);
if (connContext->err) {
printf("Connection error: %s\n", c->errstr);
exit(1);
}
replyKeys = redisCommand(connContext,"keys *");
printf("KEY\t\tVALUE\n" "------------------------\n");
while ( replyKeys->element[count]->str != NULL)
{
replyKeyValue = redisCommand(connContext,"GET %s", replyKeys->element[count]->str);
if (strstr(relyKeyValuep->str,"ERR Operation against a key holding")) {
printf("%s\t\t%s\n", replyKeys->element[count]->str,replyKeyValue->str);
break;
}
printf("%s\t\t%s\n", replyKeys->element[count]->str,replyKeyValue->str);
count++;
freeReplyObject(replyKeyValue);
}
return;
}
-------------------Snip-------------------
Could be complied as shown below:
-------------------Snip-------------------
-------------------Snip-1-------------------
[root@localhost hiredis_redis]# gcc -m32 getAllKeyValue.c -o test -lhiredis
[root@localhost hiredis_redis]# ./test 127.0.0.1 6391
KEY VALUE
------------------------
counter 24
foo hello world
bar hello
-------------------Snip-1-------------------
Limitation: This code is only valid for SET/GET commands and will not work for special data types like sorted sets etc.