Guest User

Untitled

a guest
Sep 15th, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. #!/usr/bin/python
  2. ## SQL to Redis
  3.  
  4. # import Redis and MySQL drivers
  5. import redis
  6. import MySQLdb
  7. from collections import Counter
  8.  
  9. # create class
  10. class dataProcessor(object):
  11.  
  12. # Mysql server data
  13. MYSQL_IP_ADDRESS_SERVER = 'localhost'
  14. MYSQL_USER = 'root'
  15. MYSQL_PASSWORD = 'my_strong_password'
  16. MYSQL_DATABASE_NAME = 'database_name'
  17.  
  18. # Redis server data
  19. REDIS_SERVER = 'localhost'
  20.  
  21. # function to get data from mysql and to transfer it to redis
  22. @staticmethod
  23. def sql_to_redis():
  24. r_redis = redis.StrictRedis(dataProcessor.REDIS_SERVER)
  25. print ""
  26. print "Connected to Redis successfully!"
  27.  
  28. database = MySQLdb.connect(dataProcessor.MYSQL_IP_ADDRESS_SERVER, dataProcessor.MYSQL_USER, dataProcessor.MYSQL_PASSWORD, dataProcessor.MYSQL_DATABASE_NAME)
  29. print "Connected to MySQL successfully!"
  30. print ""
  31.  
  32. cursor = database.cursor()
  33. select = 'SELECT * FROM records WHERE location_id = 9 LIMIT 100'
  34. # select = 'SELECT * FROM records WHERE location_id = 9'
  35. cursor.execute(select)
  36. data = cursor.fetchall()
  37.  
  38. # Clean redis before run again
  39. # This is for test purpose
  40. r_redis.delete("all_records")
  41.  
  42. # Put all data from MySQL to Redis
  43. for row in data:
  44. r_redis.rpush("all_records", row[3])
  45.  
  46. # Close connection to DB and Cursor
  47. cursor.close()
  48. database.close()
  49.  
  50. @staticmethod
  51. def get_data_from_redis():
  52.  
  53. r2_redis = redis.StrictRedis(dataProcessor.REDIS_SERVER)
  54.  
  55. list = []
  56. list = r2_redis.lrange("all_records", 0, 100)
  57.  
  58. print list
  59. print ""
  60.  
  61. print "Size of list:", len(list)
  62. print ""
  63.  
  64. word_list = [word for line in list for word in line.split()]
  65. print word_list
  66. print ""
  67.  
  68. words_to_count = (word for word in word_list if word[:1].isupper())
  69. top_ten = Counter(words_to_count)
  70.  
  71. print "Top 10 Most popular words:"
  72. print top_ten.most_common(10), "\n"
Add Comment
Please, Sign In to add comment