Examples

Word Count

Assumes that your data is located in redis keys, each key is a sentence (string). This Gears script allows you to count how many unique words there are.

  1. # creating gears builder
  2. bg = GearsBuilder()
  3.  
  4. # getting the value from each key
  5. bg.map(lambda x: x['value'])
  6.  
  7. # split each line to words
  8. bg.flatmap(lambda x: x.split())
  9.  
  10. # count for each word how many times it appears
  11. bg.countby()
  12.  
  13. # starting the execution
  14. bg.run()

Delete Keys by Prefix

Delete all the keys that starts with city:

  1. # creating gears builder
  2. bg = GearsBuilder()
  3.  
  4. # getting the key name
  5. bg.map(lambda x: x['key'])
  6.  
  7. # split each line to words
  8. bg.foreach(lambda x: execute('del', x))
  9.  
  10. # count how many keys was deleted
  11. bg.count()
  12.  
  13. # starting the execution on 'city:*'
  14. bg.run('city:*')

Stream Processing

Put each record that enter stream s1 into a hash

  1. # creating gears builder
  2. bg = GearsBuilder('StreamReader')
  3.  
  4. # Set the data in the hash
  5. bg.foreach(lambda x: execute('hmset', x['streamId'], *x))
  6.  
  7. # register the execution on `s1`
  8. bg.register('s1')