[PYTHON] Delete data in a pattern with Redis Cluster

Conclusion

There is no straightforward method, so write a script

Overview

To delete Redis data with a pattern, click Delete keys that match the Redis pattern. Although it is introduced in articles (although it is not a straightforward method), when using Redis Cluster, data even if the prefix of the key is the same Is distributed across the nodes, making it difficult to browse and delete at once.

In the first place, I rarely do that in the release environment, but I'm a little troubled when using Redis Cluster from the development environment. What to do if you accidentally save corrupted data and finally want to delete it.

script

From the results, writing a script like this is the fastest.

delete-pat


#! /usr/bin/env python

import sys
import commands

print sys.argv

#Please set the node yourself.
nodes = [
"localhost:10000",
"localhost:10001",
"localhost:10002"
]

pat = sys.argv[1]
for node in nodes:
    host, port = node.split(":")
    output = commands.getoutput("redis-cli -h {host} -p {port} -c keys {pat}".format(host=host,port=port,pat=pat))
    result = filter(lambda s:len(s) > 0, output.split("\n"))
    print host, port
    print result
    for key in result:
        print commands.getoutput("redis-cli -h {host} -p {port} -c del {key}".format(host=host,port=port,key=key))

I use it like this

./delete-pat hoge:fuga:*

That's all from here.

Recommended Posts

Delete data in a pattern with Redis Cluster
Train MNIST data with a neural network in PyTorch
Ingenuity to handle data with Pandas in a memory-saving manner
Organize individual purchase data in a table with scikit-learn's MultiLabel Binarizer
Get additional data in LDAP with python
Spiral book in Python! Python with a spiral book! (Chapter 14 ~)
SE, a beginner in data analysis, learns with the data science unit vol.1
Try working with binary data in Python
[Python] Drawing a swirl pattern with turtle
Draw a heart in Ruby with PyCall
Overwrite data in RDS with AWS Glue
Read a character data file with numpy
Working with 3D data structures in pandas
Draw a graph with Japanese labels in Jupyter
System switching occurs in a CentOS 7 cluster environment
Start Django in a virtual environment with Pipenv
Create a virtual environment with conda in Python
Read table data in PDF file with Python
Delete rows with arbitrary values in pandas DataFrame
Build a Django environment with Vagrant in 5 minutes
A story stuck with handling Python binary data
Clone with a specific branch / tag in GitPython
Folium: Visualize data on a map with Python
Data analysis in Python: A note about line_profiler
I wrote a design pattern in kotlin Prototype
Work in a virtual environment with Python virtualenv.
Create a new page in confluence with Python
A story packed with absolute values in numpy.ndarray
Sort dict in dict (dictionary in dictionary) with a specific key
Configure a module with multiple files in Django
Make holiday data into a data frame with pandas
Create a loop antenna pattern in Python in KiCad
A well-prepared record of data analysis in Python
Extract data from a web page with Python
Realize a super IoT house by acquiring sensor data in the house with Raspberry Pi
Consolidate a large number of CSV files in folders with python (data without header)
How to use fixture in Django to populate sample data associated with a user model
I created a stacked bar graph with matplotlib in Python and added a data label
Python2 / numpy> Replace only a specific column in a file with column data from another file> numpy.c_
Create a data collection bot in Python using Selenium
How to convert / restore a string with [] in python
I want to transition with a button in flask
Receive dictionary data from a Python program in AppleScript
Get financial data with python (then a little tinkering)
I wrote a design pattern in kotlin Builder edition
Playing with a user-local artificial intelligence API in Python
Put AWS data in Google Spreadsheet with boto + gspread
Make a simple Slackbot with interactive button in python
I wrote a design pattern in kotlin Singleton edition
I wrote a design pattern in kotlin Adapter edition
Visualize railway line data as a graph with Cytoscape 2
Try embedding Python in a C ++ program with pybind11
I wrote a design pattern in kotlin, Iterator edition
Delete 1000 objects stored in AWS S3 at a time.
[Django] Perform Truncate Table (delete all data in the table)
Drawing a tree structure with D3.js in Jupyter Notebook
I want to work with a robot in python.
A addictive point in "Bayesian inference experience with Python"
Until you insert data into a spreadsheet in Python
A server that echoes data POSTed with flask / python
Output a character string with line breaks in PyYAML