I want to use Hash of Redis and store a JSON Object in one key and Array in other.
How i can achieve this.
For example when i run this:
redis.hmset(`channel`, 'key1', 'someValue', 'key2', [{x: 1}])
then it gives the error.
But if i change the value of key2 to string it store it in form of string.
I wnat to store the JSON data. How can i do so?
I am using Node and redis npm package.
There is no native way to store an array in Redis Hash.
But, you can use the Redis Module RedisJSON which adds native JSON support to Redis. It has couple of node.js clients you can use.
e.g. redis-modules-sdk and redis-rejson
Related
I have a problem with getting data from Redis. I want to get the object using not Id column. Can I do this in Redis? Or another case when I want to get the object using Id column, but I want to have my name of this column, not just 'Id', but I have the problem with this too.
Redis is primarily a Name/Value store.
You can set the NAME to anything you want. If you want to call it 'Id' you can. If you want to call it 'x-y-z' you can too and store the VALUE along with it.
Redis also supports Hashes. If you want to get an object "using not Id column" you can store similar object types in a Hash and retrieve the entire HashSet then find the object inside the list on the client side. This is not efficient if storing large objects or if you have many objects in a list.
Ex:
HMSET user:1000 username antirez password P1pp0 age 34
HGETALL user:1000
HSET user:1000 password 12345
HGETALL user:1000
Here is a list of datatypes Redis Supports.
https://redis.io/topics/data-types
The 2 popular libraries for using C# and Redis are:
https://github.com/ServiceStack/ServiceStack.Redis
https://github.com/StackExchange/StackExchange.Redis
In the backup file there are a lot of encoded values. How do I get back the original data.
For example there is
+ d q+LsiGs1gD9duJDbzQSXytajtCY=
which is of the format ["+"] [SP] ["d"] [SP] [{digest}] [LF] where q+LsiGs1gD9duJDbzQSXytajtCY= is the key digest. How would the get the primary key from this?
Also Map and List values are represented as opaque byte values. How do we restore the original Map and List?
I would currently need to do all this if I wanted to make a CSV dump out of the backup.
The tool asbackup is an open source tool, as is asrestore. The file format is described in the repo aerospike/aerospike-tools-backup on GitHub.
Alternatively, you could use the Kafka connector to move data from Aerospike to another database via Kafka.
The easiest way to do what you're looking for is still to write a program that scans the target namespace, and parses each record into a csv format. You can use predicate filtering to only get records whose last-update-time is greater than a specific timestamp, giving you the progressive backup you want. See the PredExp class of the Java client and its examples.
I am using Redis 2.8 key space Pub/Sub notification, I would like to know if it is possible to get notified of which field changed after HSET command?
At the moment I receive the notification for the key as a consequence of the
HSET command, but I would better know which field has been set - I understand I can read again the set to look at differences once I am notified, but I don't find it very efficient.
Standard Redis keyspace notifications do not include data about the data that was changes, and specifically do not include information about the touched Hash field.
While this isn't really what you want, it can still be used as a workaround.
Try to have unique hash key name, for example:
redis.hmset('task:{}'.format(unique_id), status='running')
And when you receive a message, it will look like this:
(b'__keyspace#0__:task:c81b8373-b5ea-4be0-b8f1-b490e7280898', 'hset')
Now, knowing unique task id, you can do:
redis.hget('task:{}'.format(unique_id), 'status')
> running
I want to store a set of strings against a key in a Redis in a single step. Presently, I am using multi-exec mechanism for storing by iterating over the set.
I am using spring-data-redis library for Redis operation.
Is it possible to store a set of strings in a Redis in a single step ?
Yes, the RPUSH/LPUSH commands accept multiple values, beginning with Redis 2.4. Example:
LPUSH key value1 value2 value3
If you're using a set rather than a list, it's the SADD command:
SADD key value1 value2 value3
In Spring Data Redis, StringRedisConnection supports multiple values in the rPush, lPush, and sAdd methods.
Do you want to store a list or a set? (You refer to both.)
In general you can store your list of strings as a single JSON string.
With Spring data API for Redis , you can set a list or object using
eg:-
opsForList.leftPush("key", object);
Im currently having a redis data set with key representing ids and values as a json . I need to add a new entity in the json for every userid(keys). Is there any existing opensource tool? what is the way i should proceed to update for 1M keys of data.
There are a few possibilities:
Here's some pseudo code for doing this with Redis 2.6 Lua scripting.
for userid in users:
EVAL 'local obj = cjson.decode(redis.call("GET", KEY[1])); obj.subobj.newjsonkey = ARGV[1]; redis.call("SET", KEY[1], cjson.encode(obj));' 1 userid "new value!"
Short of that, you may need to stop the service and do this with GETs and SETs since you probably don't have a locking mechanism in place. If you can enforce a lock, see http://redis.io/commands/setnx
There are a few tools for updating an rdb. https://github.com/sripathikrishnan/redis-rdb-tools https://github.com/nrk/redis-rdb
Note, this answer was adapted to my answer to: Working with nested objects in Redis?