Redis Integration with Python

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

  • Create an install_packages.sh file and add the next:

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

  • Open the redis.conf file in the directory where we installed Redis “opt/redis-7.0.8”.
  • By default, Redis is in protective-mode. This issue prevents third-party users to write or make interactions with Redis DB. Change this by setting the variable as no.
  • Also, in the Network section the port is binding only to the server, to be able to test Redis with different computers, the port bind is changed to 0.0.0.0. This configuration allows all machines to access Redis in the network.

Start the Redis server

redis-server /opt/redis-7.0.8/redis.conf        

Test the Redis server

  • Open another tab to check if the Redis Server is running. If the Redis server is working, the answer should be “PONG”

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

  • The code and the image below show that the server IP address, port, and token have to be entered to access a web browser.

<https://{Server_IP}:8888/?token=>{check server}        

2.2 Check Redis Connection

  • Check if Redis is connected to Python

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).

  • The CustomerID will become the key of the hash
  • The Gender, Age, Annual Income, Spending Score will become the fields of the hash

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


要查看或添加评论,请登录

Humberto Villalta的更多文章

  • Network Configuration

    Network Configuration

    Try the following steps to reset the server’s internet connection: Before Starting…. To see which file is connected to…

  • How to Push Local Repo Code into Github Repo

    How to Push Local Repo Code into Github Repo

    This guide is intended to show how to push code from our local computer to a GitHub repository. Step 1 Open the Git…

  • HDFS Clustering Through Docker in CentOS

    HDFS Clustering Through Docker in CentOS

    Overview This guide will show you how to deploy a distributed file system for Hadoop (HDFS). We will first make a…

  • What is Redis?

    What is Redis?

    Overview Redis is a C programming written REmote DIctionary Server developed in 2006. Redis read and write operations…

  • Linux [~/.bashrc] File

    Linux [~/.bashrc] File

    This post is intended to show how to interact with the different settings this file offers. Some of the settings that…

  • How to Set a Virtual Machine in Windows OS

    How to Set a Virtual Machine in Windows OS

    Overview Installing a virtual machine on a Windows operating system seems to be the easiest and most basic process that…

    2 条评论
  • Spacetime Data Hub Technology Connecting the Physical and Digital Worlds

    Spacetime Data Hub Technology Connecting the Physical and Digital Worlds

    The advancement of IT technology has enabled people to project physical spaces into virtual spaces known as “digital…

  • Python Decorator Introduction with Examples

    Python Decorator Introduction with Examples

    1. Overview The decorator pattern is a software design pattern that allows us to dynamically add functionality to…

  • HyperLogLog Basics

    HyperLogLog Basics

    Overview Probabilistic data structures are very well-known because of their outstanding time and space complexity among…

  • Cuckoo Index

    Cuckoo Index

    1. Overview Cuckoo hashing is used as a solution for hash collisions, and its worst-case lookup time is constant O(1).

社区洞察