Redis Integration with Python
Overview
This guide is made for those who want to install Redis in a Centos 7 docker container and to integrate Python. Data is also used to learn how to interact with Redis through Python.
1. Redis Installation
1.1 Basic Installations
Run a Redis Docker Container
docker run -d -t --privileged --network host --name redis_beto centos:7 /sbin/init
docker exec -it redis_beto bash
Update and Install Dependencies in the System
yum update -y
yum install sudo -y
yum install wget -y
sudo yum install gcc tcl -y
sudo yum install make -y
cd /opt/
wget <https://download.redis.io/releases/redis-7.0.8.tar.gz>
tar xvzf redis-7.0.8.tar.gz
cd /opt/redis-7.0.8/
make
sudo make install
Install the packages
chmod +x install_packages.sh && ./install_packages.sh
Configure Redis
Start the Redis server
redis-server /opt/redis-7.0.8/redis.conf
Test the Redis server
redis-cli ping
2. [Python] Ingesting Test Data
2.1 Jupyter Docker Installation
Make another docker container as below:
docker run -d -t --privileged --network host --name jupyter centos:7 /sbin/init
docker exec -it jupyter bash
Create an install_packages.sh file
yum update -y
yum install sudo -y
yum install wget -y
yum install python3 -y
python3 -m pip install --upgrade pip
python3 -m pip install jupyter
python3 -m pip install jupyterlab
python3 -m pip install pandas
python3 -m pip install redis
## Running Jupyter from Available Local IP
jupyter notebook --ip=0.0.0.0 --allow-root
Install the packages
chmod +x install_packages.sh && ./install_packages.sh
Run the Following on the local machine Browser
<https://{Server_IP}:8888/?token=>{check server}
2.2 Check Redis Connection
import redis
# create a Redis client
r = redis.Redis(host='localhost', port=6379, db=0)
# check Redis connection
if r.ping():
print('Redis is connected')
else:
print('Redis is not connected')
2.3 Ingest and Retrieve Data From Redis
The dataset used for this example can be download on this link. The Mall Customer dataset consists of Customer ID, Gender, Age, Annual Income, and Spending Score (1-100).
import pandas as pd
df = pd.read_csv("Mall_Customers.csv")
# Insert the data to Redis
for row in df.iterrows():
for num, value in enumerate(row[1][1:]):
r.hset(row[1][0], df.columns[num+1], value)
# Retrieve the Data from Redis
for val in r.keys():
if r.type(val).decode() == 'hash':
print("{}: {}".format("Customer ID",val.decode()))
print("{}: {}".format("Gender", r.hvals(val)[0].decode()))
print("{}: {}".format("Age", r.hvals(val)[1].decode()))
print("{}: {}".format("Annual Income (k$)", r.hvals(val)[2].decode()))
print("{}: {}".format("Spending Score (1-100)", r.hvals(val)[3].decode()))
print("-"*30)
Vizualize all inserted keys in Redis
r.keys()
#python #redis #cache #database #databasemanagement #datascience #developerlife #dataengineer #dataengineering #bigdata #researchanddevelopment #linux #docker #jupyternotebook