Redis - HMSET a json object - redis

What's the correct way to store a JSON object as the value for a pair using HMSET in red? Should I escape the JSON object like this below?
HMSET myhash field1 "{\"k1\":1}"

If you are using redis-cli, both the following commands return the same result:
> SET key1 '{"name":"Fred","age":25}'
> GET key1
"{\"name\":\"Fred\",\"age\":25}"
> SET key2 "{\"name\":\"Fred\",\"age\":25}"
> GET key2
"{\"name\":\"Fred\",\"age\":25}"

You'll need to serialize your object to a string. Consequently, you will also need to be able to deserialize that string back to an instance of your class. One option for serializing/deserializing would be JSON.

Related

How to extract data from array in a JSON message using CloudWatch Logs Insights?

I log messages that are JSON objects. The JSON has an array that contains key/value pairs:
{
...
"arr": [{"key": "foo", "value": "bar"}, ...],
...
}
Now I want to filter results that contains a specific key and extract the values for a specific key in the array.
I've tried using regex, something like parse #message /.*"key":"my_specific_key","value":(?<value>.*}).*/ which extracts the value but also returns the rest of the message. Also it doesn't filter the results.
How can I filter results and extract the values for a specific key?
If in your log entry in the cloudwatch log group they are actually showing up as json, you can just reference the key directly in any place you would a field.
(don't need the #, cloudwatch appends that automatically to all default values)
If you are using python, you can use aws_lambda_powertools to do this as well, in a very slick way (and its an actual aws product)
If they are showing up in your log as a string, then it may be an escaped string and you'll have to match it -exactly- - including spaces and what not. when you parse, you will want to do something like this:
if this is the string of your log message '{"AKey" : "AValue", "Key2" : "Value2"}
parse #message "{\"*\" : \"*\",\"*\" : \"*\"} akey, akey_value, key2, key2_value
then you can filter or count or anything against those variables. parse is specifically a statement to match a pattern and assign the wildcard to a variable, one at a time in order
tho with a complex json, if your above regex works than all you need is a filter statement
field #message
| pares #message ... your regex as value_var
| filer value_var /some more regex/
if its not a string in the log entry, but an actual json, you can just reference against the key:
filter a_key ~="some value" (or regex here)
https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/CWL_AnalyzeLogData-discoverable-fields.html
for more info

how to access type list of tuple values of module [NICs] output into another module[VM] in azure terraform

| var.nic1-id is tuple with 2 elements
Inappropriate value for attribute "network_interface_ids": element 0: string
required.
Please help me in this issue!
If var.nic1-id is a list, then you would do:
network_interface_ids = var.nic1-id
Otherwise, you are creating a list of lists, which results in your error.

Querying data on json saved using ReJSON

i have saved one json using Rejson against a key,now i would like to filter/query out data using ReJson.
Please let me know how can i do it ...python prefered .
print("Abount to execute coomnad")
response=redisClient.execute_command('JSON.SET', 'object', '.', json.dumps(data))
print(response)
reply = json.loads(redisClient.execute_command('JSON.GET', 'object'))
print(reply)
using the above code i was able to set data using ReJson .now lets suppose i want to filer data .
my test json is :
data = {
'foo': 'bar',
'ans': 42
}
How can you filter say json in which foo has value as bar
Redis in general, and ReJSON specifically, do not provide search-by-value functionality. For that, you'll have to either index the values yourself (see https://redis.io/topics/indexes) or use RediSearch.

How to get keys from the value in redis

I have checked following, but didn't work.
https://redis.io/commands/keys
KEYS Room:*
1) "Room:120"
2) "Room:121"
3) "Room:122"
Following is the redis key/values (HMSET)
Room:120 [SocketId:mOQDJusPjDTBN5L-AAAC,TimeStamp:10-10-2017 12:10:00 AM]
Room:121 ....
Room:122 ....
...
Need to search as Room:* SocketId:mOQDJusPjDTBN5L-AAAC
How can I search for SocketId in the collection ?
Need to search with:
mOQDJusPjDTBN5L-AAAC
The question is not so clear
as u mentioned hmset i am assuming that you are using hashes to store your data.
As per your data,
'room120' should be the key, 'socketId' should be the field and 'mOQDJusPjDTBN5L-AAAC' should be the value.
so in order to search for socketId you can use hscan,where hscan iterates through the field of a particular key.https://redis.io/commands/scan
in case if you are just using key/value storage i.e
'socketId' being the key ,'mOQDJusPjDTBN5L-AAAC' being the value.
here u can just use the command Keys *socket*to search for the key socketId

using redis to store time series / historical data

I'm trying to implement the redis solution suggested here: http://www.slideshare.net/cacois/cois-palkostrata2013: it is the best I have found so far.
I have the following data structure "hash"
{'user': username, 'text': sometext, 'time': 1400543375}
I want to save the data in a log, the slides suggest saving it in a sorted list. When I try this:
zadd mysortedset 1400543375 {'user': username, 'text': sometext, 'time': 1400543375}
I get
Invalid argument(s)
so I thought I should create the hash first and then add it to the set
127.0.0.1:6379> HMSET setmember:1400543375 user username text sometext time 1400543375
OK
and then
127.0.0.1:6379> zadd mysortedset 1400543375 setmember:1400543375
(integer) 1
Is this the way it is supposed to be done?
What’s simply missing are the quotes around your JSON. Otherwithe it tries to interpret your zadd as scove value score value. And „username,“ is no valid score. ;) I tried it with
zadd mysortedset 1234 ”{’user’: foo …}”
and it works.
A log suggest you get the entries in the right order to begin with. In which case use the l* commands (lpush,lpop etc)