<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	
xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
>

<channel>
	<title>redis csv import &#8211; DataYuge</title>
	<atom:link href="https://datayuge.com/tag/redis-csv-import/feed/" rel="self" type="application/rss+xml" />
	<link>https://datayuge.com</link>
	<description>The API Culture</description>
	<lastBuildDate>Thu, 08 Nov 2018 03:22:25 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://datayuge.com/wp-content/uploads/2018/07/cropped-favicon-32x32.png</url>
	<title>redis csv import &#8211; DataYuge</title>
	<link>https://datayuge.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How to convert and import CSV to Redis</title>
		<link>https://datayuge.com/convert-import-csv-redis/</link>
					<comments>https://datayuge.com/convert-import-csv-redis/#respond</comments>
		
		<dc:creator><![CDATA[arun]]></dc:creator>
		<pubDate>Sun, 15 Apr 2018 06:28:19 +0000</pubDate>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[redis csv import]]></category>
		<category><![CDATA[redis to csv]]></category>
		<guid isPermaLink="false">http://v3.datayuge.in/?p=1858</guid>

					<description><![CDATA[<p>Introduction When dealing with a huge amount of static data&#8217;s which are requested continuously, we need to have a fast key-value cache in place. Since we already had a huge chunk of data in SQL database. Let&#8217;s see how can...</p>
<p>The post <a rel="nofollow" href="https://datayuge.com/convert-import-csv-redis/">How to convert and import CSV to Redis</a> appeared first on <a rel="nofollow" href="https://datayuge.com">DataYuge</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h3>Introduction</h3>
<p>When dealing with a huge amount of static data&#8217;s which are requested continuously, we need to have a fast key-value cache in place. Since we already had a huge chunk of data in SQL database. Let&#8217;s see how can we convert that to CSV and then to redis.</p>
<p>&nbsp;</p>
<p><img fetchpriority="high" decoding="async" alt="How to convert and import CSV to Redis" class="aligncenter" src="https://upload.wikimedia.org/wikipedia/en/thumb/6/6b/Redis_Logo.svg/1200px-Redis_Logo.svg.png" width="559" height="248" /></p>
<h2>Step &#8211; 1 | Export data from SQL database</h2>
<p>Here the data is exported to CSV format from the SQL data store using the favourite tools. We used &#8220;sequel pro&#8221; to dump the data to CSV format.</p>
<h2>Step -2 | Convert the raw CSV data to Redis Commands (Generate the Redis commands from CSV)</h2>
<p>Our CSV file is in the following format. Let&#8217;s call this as input.csv</p>
<pre class="lang:default decode:true">1, key1, value1, created_at, updated_at
2, key2, value2, created_at, updated_at</pre>
<p>Convert the CSV to <a href="https://redis.io/commands">Redis commands</a>. This will print &#8220;SET, key1, key2&#8221; from the &#8220;input.csv&#8221; file and remove the double quotes. Edit the command to match your CSV file. &#8220;$1&#8221; prints the first column. (Counting start from 1 and not 0)</p>
<pre class="lang:default decode:true">awk -F, 'BEGIN {OFS=","} { print "SET",$2, $3}' input.csv | sed 's/\"//g'</pre>
<p>This will print the response on the command line as follows. Make sure that the output is in the format  &#8220;<strong>SET, key1,key2</strong>&#8220;. Once you are sure that the response is in the expected format, save the file to output.txt as follows</p>
<pre class="lang:default decode:true">awk -F, 'BEGIN {OFS=","} { print "SET",$2, $3}' input.csv | sed 's/\"//g' &gt; output.txt</pre>
<p>Note: We will be using the <a href="https://redis.io/topics/mass-insert">Redis mass insert</a>. However, you need to convert the <strong>Redis commands</strong> to <a href="https://redis.io/topics/protocol"><strong>Redis protocol</strong></a> to do that.</p>
<h2>Step &#8211; 3 | Convert the Redis command generated to Redis protocol. (Redis protocol generator)</h2>
<p>Here we will be generating the redis protocol to convert the redis commands for importing to redis. We will be using the redis mass insert method for that.</p>
<p>Copy and save the following script to gen_redis_proto.py. Raw GitHub <a href="https://raw.githubusercontent.com/arunbabucode/redis-tools/master/gen_redis_proto.py">link</a></p>
<pre class="lang:python decode:true ">#!/usr/bin/env python -tt
# -*- coding: UTF-8 -*-
"""
Generating Redis Protocol

Generate the Redis protocol, in raw format, in order to use 'redis-cli --pipe' command to massively insert/delete.... keys in a redis server
It accepts as input a pipe with redis commands formatted as "SET key value" or "DEL key"...

Usage:

      echo "SET,mykey1,value1\nSET,mykey2,value2" &gt; data.txt
      cat data.txt | python gen_redis_proto.py | redis-cli --pipe

"""

__author__ = "Salimane Adjao Moustapha (me@salimane.com)"
__version__ = "$Revision: 1.0 $"
__date__ = "$Date: 2013/04/30 12:57:19 $"
__copyleft__ = "Copyleft (c) 2013 Salimane Adjao Moustapha"
__license__ = "MIT"

import sys
import fileinput
from itertools import imap


def encode(value):
    "Return a bytestring representation of the value"
    if isinstance(value, bytes):
        return value
    if not isinstance(value, unicode):
        value = str(value)
    if isinstance(value, unicode):
        value = value.encode('utf-8', 'strict')
    return value


def gen_redis_proto(*cmd):
    proto = ""
    proto += "*" + str(len(cmd)) + "\r\n"
    for arg in imap(encode, cmd):
        proto += "$" + str(len(arg)) + "\r\n"
        proto += arg + "\r\n"
    return proto


if __name__ == '__main__':
    for line in fileinput.input():
        sys.stdout.write(gen_redis_proto(*line.rstrip().split(',')))</pre>
<p>Now run the following command to generate the redis protocol command and save to redis using the mass insert method.</p>
<pre class="lang:default decode:true">cat output.txt | python gen_redis_proto.py | redis-cli --pipe</pre>
<p>The CSV will be now cached into the redis memory. You can save the data in the memory to a .<strong>rdb</strong> file using &#8220;BGSAVE&#8221; command</p>
<p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://datayuge.com/convert-import-csv-redis/">How to convert and import CSV to Redis</a> appeared first on <a rel="nofollow" href="https://datayuge.com">DataYuge</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://datayuge.com/convert-import-csv-redis/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/

Object Caching 64/166 objects using disk
Page Caching using disk: enhanced 
Minified using disk
Database Caching 2/5 queries in 0.002 seconds using disk

Served from: datayuge.com @ 2026-04-09 02:00:47 by W3 Total Cache
-->