#!/usr/bin/python
## SQL to Redis
# import Redis and MySQL drivers
importredisimportMySQLdbfromcollectionsimportCounter# create class
classdataProcessor(object):# Mysql server data
MYSQL_IP_ADDRESS_SERVER='localhost'MYSQL_USER='root'MYSQL_PASSWORD='my_strong_password'MYSQL_DATABASE_NAME='database_name'# Redis server data
REDIS_SERVER='localhost'# function to get data from mysql and to transfer it to redis
@staticmethoddefsql_to_redis():r_redis=redis.StrictRedis(dataProcessor.REDIS_SERVER)print""print"Connected to Redis successfully!"database=MySQLdb.connect(dataProcessor.MYSQL_IP_ADDRESS_SERVER,dataProcessor.MYSQL_USER,dataProcessor.MYSQL_PASSWORD,dataProcessor.MYSQL_DATABASE_NAME)print"Connected to MySQL successfully!"print""cursor=database.cursor()select='SELECT * FROM records WHERE location_id = 9 LIMIT 100'# select = 'SELECT * FROM records WHERE location_id = 9'
cursor.execute(select)data=cursor.fetchall()# Clean redis before run again
# This is for test purpose
r_redis.delete("all_records")# Put all data from MySQL to Redis
forrowindata:r_redis.rpush("all_records",row[3])# Close connection to DB and Cursor
cursor.close()database.close()@staticmethoddefget_data_from_redis():r2_redis=redis.StrictRedis(dataProcessor.REDIS_SERVER)list=[]list=r2_redis.lrange("all_records",0,100)printlistprint""print"Size of list:",len(list)print""word_list=[wordforlineinlistforwordinline.split()]printword_listprint""words_to_count=(wordforwordinword_listifword[:1].isupper())top_ten=Counter(words_to_count)print"Top 10 Most popular words:"printtop_ten.most_common(10),"\n"