Redis: Sort hash by fields and get values - sorting

lets assume I have hash data type where fields are uuids and values are some strings
HSET myhash d1663228-3ce9-4651-8931-9f929df1778b test_data
HSET myhash 05c16eb1-fb7b-4732-a94c-ed9a2745101e test_other_data
and in separate sorted set I do have uuids stored with scores
ZADD myzset 1 05c16eb1-fb7b-4732-a94c-ed9a2745101e
ZADD myzset 2 d1663228-3ce9-4651-8931-9f929df1778b
What I want is to get all values from myhash sorted by fields scores from myzset
the expected output is
1) test_other_data
2) test_data

Related

How to create dataframe from ordered dictionary?

I have an ordered dictionary which has 4 keys and multiple values. I tried to create the dataframe like this
df = pd.DataFrame(items, index=[0])
print('\ndf is ',df)
But this triggers ValueError, as the multiple values from the dictionary don't match.
The ordered dictionary is below:
OrderedDict([('Product', 'DASXZSDASXZS'), ('Region', ['A', 'B', 'C']), ('Items', ['1', '2', '3']), ('Order', ['123', '456', '789'])])
I want the dataframe format to be like:
Product Region Items Order
DASXZSDASXZS A 1 123
DASXZSDASXZS B 2 456
...
How can I achieve this format for the dataframe?
Not enough rep to comment. Why do you try to specify index=[0]?
Simply doing
df = pd.DataFrame(items)
works; if you want to change the index, you can set it later with df.set_index(...)
#viktor_dmitry your comment to #Battleman links to external data, here's a solution.
In https://www.codepile.net/pile/GY336DYN you have a list of OrderedDict entries, in the example above you just had 1 OrderedDict. Each needs to be treated as a separate DataFrame construction. From the resulting list you use concat to get a final DataFrame:
ods = [OrderedDict([('MaterialNumber', '2XV9450-1AR24'), ('ForCountry'...]),
OrderedDict([('MaterialNumber', ...),
...]
new_df = pd.concat([pd.DataFrame(od) for od in ods])
# new_df has 4 columns and many rows
Note also that 1 of your example items is invalid, you'd need to filter this out, the rest appear to be fine:
ods[21]
OrderedDict([('MaterialNumber', '4MC9672')]) # lacks the rest of the columns!

Composite key / secondary indexing strategy in Redis

Say I have some data of the following sort that I want to store in Redis:
* UUID
* State (e.g. PROCESSED, WAITING_FOR_RESPONSE)
* […] other vals
The UUID or the State are the only two variables that I will ever need to query on
What data structure in Redis is most suited for this?
How would I go about structuring the keys?
Okay, not sure I understand completely but going to try to go with it.
Assuming you need to look up all entities with state PROCESSED you can use sets for these:
SADD PROCESSED 123-abcd-4567-0000
Then you can easily find all entities with PROCESSED state. You'll do same for each state you want.
SMEMBERS PROCESSED
Now you'll also then want to have a hash for all your entities and its values:
HSET 123-abcd-4567-0000 state PROCESSED
HSET 123-abcd-4567-0000 otherproperty valuedata
This will set the "state" in the hash for the UUID to be processed (you'll need to figure out how to keep these in sync, you can use scripts or just handle in your code)
So in summary you have 2 major structures:
Sets to store your State to UUID information
Thus you have 1 set per state
Hashes to store the UUID to properties information
Thus you have 1 hash PER entity
Example Hash
123-abcd-4567-0000 => { state: PROCESSED, active: true }
987-zxy-1234-0000 => { state: PROCESSED, active: false }
But please clarify more if this doesn't seem to fit.
If you want to reduce your key space since the Hashes per entity can be much you can create a hash per attribute instead:
HSET states 123-abcd-4567-0000 PROCESSED
Thus you have a hash per attribute and your key is the UUID and value is the value of the property which is the hash key.
Example Hash
state => { 123-abcd-4567-0000: PROCESSED, 987-zxy-1234-0000: PROCESSED }
active => { 123-abcd-4567-0000: true, 987-zxy-1234-0000: false }
RediSearch (a Redis module) supports adding secondary index to existing data in Redis like Hash.
After setting the schema for the fiels you would like to index you can easily search based on these fields values.
e.g.
127.0.0.1:6379> FT.CREATE myIdx ON HASH PREFIX 1 doc: SCHEMA title TEXT
127.0.0.1:6379> hset doc:1 title "mytitle" body "lorem ipsum" url "http://redis.io"
(integer) 3
127.0.0.1:6379> FT.SEARCH myIdx "#title:mytitle" LIMIT 0 10
1) (integer) 1
2) "doc:1"
3) 1) "title"
2) "mytitle"
3) "body"
4) "lorem ipsum"
5) "url"
6) "http://redis.io"

sum of all values in column in array based on column value from other array

I have an array of user info in #user_info and in array there is a column total and column user_id.
I have another instance variable #product and it has column user_id
How do i calculate sum of all values in column total verifying the user_id from #user_info and #product same?
I am trying to calcuate sum in controller, store it in instance variable and use that variable in JSON
Thanks
Try something like
#user_info.select { |user_info| user_info.user_id == #product.user_id }.sum(&:total)
or, if #user_info is not a simple Array, you should be able to do in one SQL query
#user_info.where(user_id: #product.user_id).sum(:total)

redis sort by multi fields

It's easy to use sql to query with multi sort fields.For example:
select * from user order by score desc,name desc
with two fields sort(score,name).
how should do this in redis?
Use sorted set of redis which is sorted by score. You have to prepare score according to your needs.
finalScore = score*MAX_NAME_VALUE + getIntRepresentation(name)
//MAX_NAME_VALUE is the maximum value returned by getIntRepresentation() method
and then use
zadd myset finalScore value
and the just use
zrevrange myset 0 10

Sorted results from hbase scanner

How to retrieve hbase column family "values" in any sorted order of the same?
like column family value
---------------------------------
column:1 1
column:3 2
column:4 3
column:2 4
HBase itself won't do that, instead you could retrieve the list of KeyValues using the Result.raw[1] method, put that in a List and sort it by passing your own comparator to Collections.sort[2].
http://hbase.apache.org/apidocs/org/apache/hadoop/hbase/client/Result.html#raw()
http://download.oracle.com/javase/6/docs/api/java/util/Collections.html#sort(java.util.List, java.util.Comparator)

Resources