Python Examples
Convert IP to Integer and Integer To IP in Python
How to Get IPv4/IPv6 Address Range from CIDR in Python?
Compare Two Objects For Equality in Python
How to find Duplicate Elements from a List in Python
Convert Timestamp to datetime in Python
Convert datetime to Timestamp in Python
Generate Random String of Specific Length in Python
Encryption and Decryption of Strings in Python
The string module in Python
Convert string to bytes in Python
Convert bytes to string in Python
Convert string to datetime and datetime to string in Python
Call a function Asynchronously from within a Loop and wait for the Results in Python
Remove Duplicate Elements from a List in Python
Caching in Python with Examples
How to Bulk Insert and Retrieve Data from Redis in Python
How to Write Unit Test in Python
Read and Write CSV Files in Python
Read and Write Data to a Text File in Python
How to Convert CSV to JSON in Python
Create ICS Calendar File in Python
Install Python on Windows 10/11
Install Python on Ubuntu 20.04 or 22.04.3
Python - Install Virtual Environment
How to Find a Specific Field Value from a JSON list in Python
Download and Unzip a Zipped File in Python
Python Install PIP
Python Install Virtual Environment
How to Fix Python Error: string argument without an encoding
Compare Two JSON files in Python
How to Hash a Dictionary Object in Python?
Create a Digital Clock in Python
Create Multiple URLs Using Each Path of a URL in Python
Send an Email with Multiple Attachments using Amazon SES in Python
SQLAlchemy Query Examples for Effective Database Management
SQLAlchemy Query to Find IP Addresses from an IP Range in Bulk
How to Create and Use Configuration files in a Python Project
Check if a Value Already Exists in a List of Dictionary Objects in Python
How to Split Large Files by size in Python?
Fixing - Running Scripts is Disabled on this System Error on Windows
Generating QR Codes in Python
Reading QR Codes in Python
How to Bulk Insert and Retrieve Data from Redis in Python
- Last updated Apr 25, 2024
To perform bulk insert and retrieve operations from Redis, we will use the redis-py Python library.
To install redis-py, use the following pip command:
pip install redis
To complete this tutorial, you need to have a running Redis server on your local system. Learn how to install and run Redis server on Windows here.
Insert Data in Bulk
The Redis class has a subclass called Pipeline. The use of Pipeline allows to send multiple commands for execution in a single request.
Python code to data in bulk into a Redis server:
import redis
import json
redis_client = redis.Redis(host='localhost',
port=6379, db=0, ssl=False)
pipe = redis_client.pipeline()
data_list = [{"key":"1", "value":"apple"},
{"key":"2", "value":"mango"},
{"key":"3", "value":"grapes"},
{"key":"4", "value":"orange"},
{"key":"5", "value":"pineapple"},
{"key":"6", "value":"guava"},
{"key":"7", "value":"watermelon"}]
for item in data_list:
pipe.set(item['key'], json.dumps({item['key'] : item['value']}))
set_response = pipe.execute()
print("bulk insert response : ", set_response)
The output of the above code is as follows:
bulk insert response : [True, True, True, True, True, True, True]
Retrieve Data in Bulk
Python code to retrieve data in bulk from a Redis server:
import redis
redis_client = redis.Redis(host='localhost',
port=6379, db=0, ssl=False)
pipe = redis_client.pipeline()
data_list = [{"key":"1"},
{"key":"2"},
{"key":"3"},
{"key":"4"},
{"key":"5"},
{"key":"6"},
{"key":"7"}]
for item in data_list:
pipe.get(item['key'])
get_response = pipe.execute()
print("bulk get response : ", get_response)
The output of the above code is as follows:
bulk get response : [b'{"1": "apple"}', b'{"2": "mango"}', b'{"3": "grapes"}', b'{"4": "orange"}', b'{"5": "pineapple"}', b'{"6": "guava"}', b'{"7": "watermelon"}']