<?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/"
	>

<channel>
	<title>MySQL Fanboy &#187; Code</title>
	<atom:link href="/category/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mysqlfanboy.com</link>
	<description>Almost crazy about Opensource / Free  information.</description>
	<lastBuildDate>Tue, 26 Nov 2013 22:51:28 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.8</generator>
	<item>
		<title>Indexed CSV</title>
		<link>http://www.mysqlfanboy.com/2012/07/indexed-csv/</link>
		<comments>http://www.mysqlfanboy.com/2012/07/indexed-csv/#comments</comments>
		<pubDate>Thu, 26 Jul 2012 21:24:59 +0000</pubDate>
		<dc:creator><![CDATA[mark]]></dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Commentary]]></category>

		<guid isPermaLink="false">http://www.mysqlfanboy.com/?p=854</guid>
		<description><![CDATA[Why is there no index for CSV files?  Indexes are very simple. If the first column of your CSV file is in sorted order you can do a binary search to find your data.  But what if you need to find data in the second or third column? If you have a separate index file [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Why is there no index for CSV files?  Indexes are very simple.</p>
<p>If the first column of your CSV file is in sorted order you can do a binary search to find your data.  But what if you need to find data in the second or third column?</p>
<p>If you have a separate index file pointing to the first byte of each line you could seek to that position in the CSV file and get your data.  Given a file with only your needed column and the byte offset and length of the line you can search the index to find the pointer to the position in the CSV file.</p>
<p><strong>SAMPLE DATA</strong><br />
“record”,”title”,”data”<br />
1,”test1”,”data1”<br />
2,”test3”,”data3”<br />
3,”test2”,”data2”</p>
<p><strong>SAMPLE INDEX</strong><br />
“test1”,24,17<br />
“test2”,58,17<br />
“test3”,41,17<br />
“title”,0,23</p>
<p>Here is a simple perl program to create just such an index.</p>
<pre>#!/usr/bin/perl
# csvindex.pl

use strict;
use warnings;

use Text::CSV_XS;

my $file = $ARGV[0];

my $csv = Text::CSV_XS-&gt;new ({
binary    =&gt; 1,
auto_diag =&gt; 1,
sep_char  =&gt; ','    # not really needed as this is the default
});

open(my $data, '&lt;:encoding(utf8)', $file) or die "Could not open '$file'\n";

my $head = 0;
my $tail = 0 ;

while (my $fields = $csv-&gt;getline( $data )) {
$tail = tell $data ;
printf("\"%s\",%d,%d\n",$fields-&gt;[$ARGV[1]], $head, $tail-$head);
$head = $tail;
}</pre>
<p>This command create an index of the second column (number start with 0).</p>
<pre dir="ltr">$ perl csvindex.pl <a href="/Files/2009.csv">2009.csv</a> 1 | sort &gt; <a href="/Files/2009.cidx">2009.cidx</a></pre>
<p>A binary search can be done with the linux ‘look’ command.</p>
<pre dir="ltr">$ look \"OK\" 2009.cidx
"OK",2134,59</pre>
<p>Here is a simple program to seek into a file and print ‘n’ number of characters.</p>
<pre>#!/usr/bin/perl
# csvseek.pl
use strict;
use warnings;

use Text::CSV_XS;

my $file = $ARGV[0];

open(my $data, '&lt;:encoding(utf8)', $file) or die "Could not open '$file'\n";

seek $data, $ARGV[1], 0 ;

my $line = "" ;

read $data, $line, $ARGV[2];
chomp($line);

print $line . "\n" ;</pre>
<p>Here is how it is used.</p>
<pre dir="ltr">$ perl csvseek.pl  2009.csv 2134 59
OKLAHOMA,OK,40,"4,765","1,251","2,160","1,024",6,"9,206",</pre>
<p>I’d like to see these tools added to CSVKit.</p>
<p>What I’d really like to see is this index/s added to MySQL’s CSV engine.  Would anyone like to help me get this done?</p>
<p><img alt="" src="http://mark.grennan.com/images/MarkGrennanSigniture.bmp" class="alignleft" width="166" height="69" /></p>
<p><a href="http://twitter.com/share?url=http%3A%2F%2Fwww.mysqlfanboy.com%2F2012%2F07%2Findexed-csv%2F&amp;count=none&amp;via=MySQLFanBoy&amp;related=MySQLFanBoy:Site+Twitter+account&amp;text=Indexed CSV - MySQL Fanboy" class="twitter-share-button">Tweet</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.mysqlfanboy.com/2012/07/indexed-csv/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>automysqlbackup 2.5.5</title>
		<link>http://www.mysqlfanboy.com/2011/07/automysqlbackup-2-5-5/</link>
		<comments>http://www.mysqlfanboy.com/2011/07/automysqlbackup-2-5-5/#comments</comments>
		<pubDate>Tue, 12 Jul 2011 21:48:56 +0000</pubDate>
		<dc:creator><![CDATA[Mark Grennan]]></dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[automsyqlbackup]]></category>
		<category><![CDATA[Backup]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://www.mysqlfanboy.com/?p=618</guid>
		<description><![CDATA[I spent my day doing updates to the automysqlbackup script.  Here is some of what I&#8217;ve added over the last year. The bug number fixes are from SourceForge.  https://sourceforge.net/tracker/?atid=628964&#38;group_id=101066&#38;func=browse # 2.5.5 MTG &#8211; (2011-07-21) #    &#8211; Bug &#8211; Typo Ureadable Unreadable config file line 424 &#8211; ID: 3316825 #    &#8211; Bug &#8211; Change &#8220;#!/bin/bash&#8221; to [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>I spent my day doing updates to the automysqlbackup script.  Here is some of what I&#8217;ve added over the last year.</p>
<p>The bug number fixes are from SourceForge. <a href="https://sourceforge.net/tracker/?atid=628964&amp;group_id=101066&amp;func=browse"> https://sourceforge.net/tracker/?atid=628964&amp;group_id=101066&amp;func=browse</a></p>
<p># 2.5.5 MTG &#8211; (2011-07-21)<br />
#    &#8211; Bug &#8211; Typo Ureadable Unreadable config file line 424 &#8211; ID: 3316825<br />
#    &#8211; Bug &#8211; Change &#8220;#!/bin/bash&#8221; to &#8220;#!/usr/bin/env bash&#8221; &#8211; ID: 3292873<br />
#    &#8211; Bug &#8211; problem with excludes &#8211; ID: 3169562<br />
#    &#8211; Bug &#8211; Total disk space on symbolic links &#8211; ID: 3064547<br />
#    &#8211; Added DEBUG option to only print the commands that will be executed.<br />
#    &#8211; Bug &#8211; WHICH command didn&#8217;t work if there was a WHICH alias.<br />
# VER 2.5.4: MTG &#8211; (2011-01-28)<br />
#    &#8211; fixed bug in rsync process.<br />
#    &#8211; Added the ability to backup only a single table by naming the table like<br />
#      database.table<br />
# VER 2.5.2-02:  MTG &#8211; (2010-12-29)<br />
#    &#8211; Added file promission settings (chmod) to directory and file creation points.<br />
# VER 2.5.2-01:  MTG &#8211; (2010-11-06)<br />
#    &#8211; Added &#8216;-R&#8217; to the mysqldump options to include stored procedures in the backup<br />
#      by default. (suggested by Zack Evans)<br />
# VER 2.5.2:  MTG &#8211; (2010-11-04)<br />
#    &#8211; Added option to archive (rsync) the local backup files to a remote locations<br />
#      using the COPYDIR varaible.<br />
#    &#8211; Added option to copy files into a directory based on the hostname using the<br />
#      variable HOSTNAME.  This allows the script to be run from a shared storage directory<br />
#      ( SBM, NFS, NetApp) the data to be kept seperate places.<br />
#    &#8211; Added option to backup all database schemas only using variable FULLSCHEMA.<br />
#    &#8211; Added option to backup MySQL configuration file, my.cnf and remove files older then seven<br />
#      days from the BACKUPDIR directory.<br />
#    &#8211; Added &#8211;master-data=2 and &#8211;single-transaction to include a comment with the master server&#8217;s<br />
#      the binary log coordinates. If used the CHANGE_MASTER_TO line must be uncommented.</p>
<p>The project is at <a href="https://sourceforge.net/projects/automysqlbackup/">https://sourceforge.net/projects/automysqlbackup/</a></p>
<p>You can download my version at <a href="/Files/automysqlbackup.sh">http://www.mysqlfanboy.com/Files/automysqlbackup.sh</a></p>
<p>Email me or leave a comment if you have any trouble.</p>
<p><img class="alignleft" src="http://mark.grennan.com/images/MarkGrennanSigniture.bmp" alt="" width="166" height="69" /></p>
<p><a href="http://twitter.com/share?url=http%3A%2F%2Fwww.mysqlfanboy.com%2F2011%2F07%2Fautomysqlbackup-2-5-5%2F&amp;count=none&amp;via=MySQLFanBoy&amp;related=MySQLFanBoy:Site+Twitter+account&amp;text=automysqlbackup 2.5.5 - MySQL Fanboy" class="twitter-share-button">Tweet</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.mysqlfanboy.com/2011/07/automysqlbackup-2-5-5/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>mg_hot_replace_table.pl</title>
		<link>http://www.mysqlfanboy.com/2010/05/mg_hot_replace_table-pl/</link>
		<comments>http://www.mysqlfanboy.com/2010/05/mg_hot_replace_table-pl/#comments</comments>
		<pubDate>Sat, 01 May 2010 06:00:13 +0000</pubDate>
		<dc:creator><![CDATA[Mark Grennan]]></dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Data]]></category>
		<category><![CDATA[Inport]]></category>
		<category><![CDATA[Load]]></category>
		<category><![CDATA[mg_]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Tables]]></category>

		<guid isPermaLink="false">http://www.mysqlfanboy.com/?p=60</guid>
		<description><![CDATA[Do you have MyISAM tables you reload with new data? Do your queries, using that table, get blocked because the table is locked? Do the waiting queries create idle connections slowing down the table load? Do you wish you could just replace the table? Years ago I was told you can replace CSV tables by [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Do you have MyISAM tables you reload with new data?</p>
<p>Do your queries, using that table, get blocked because the table is locked?</p>
<p>Do the waiting queries create idle connections slowing down the table load?</p>
<p>Do you wish you could just replace the table?</p>
<p>Years ago I was told you can replace CSV tables by simply replacing the CSV file.  I figured this would also be true of a MyISAM file and it is. I use this perl script to replace MyISAM tables forcast and current observation weather data. The processing and tables are created on another computer.  Weather forecasting is CPU and database expensive. I then copy (rsync) the files to the production system and run this script.</p>
<pre>#!/usr/bin/perl
################################################################################
################################################################################
# mg_hot_replace_table.pl - Hot Replace a MySQL table.
#
# 2010-05-01 Mark Grennan - Weather Decision Technonigy Inc.
#
################################################################################
################################################################################
 use DBI;
 use File::Copy;
 use Getopt::Long;
 use Pod::Usage;
################################################################################
# These items need to be modified for your needs.
################################################################################
 $mydir="/data/mysql";           # MySQL data directory
 $stage="$mydir/stage";          # Directory containing the NEW data
 # database information
 $db="point_forecast";           # Database that containes the table to be replaced.
 $tb="uv_hourly";                # Table being replaced.
 $host="127.0.0.1";              # IP or FQDN of the MySQL server (localhost)
 $port="3306";                   # Port used by localhost (3306)
 $userid="dba";                  # User with access to database.
 $passwd="dbap@sswd";            # Password for the user.
################################################################################
# take command line options
################################################################################
 $help=0;
 &amp;GetOptions("mydir=s" =&gt; \$mydir, "stage=s" =&gt; \$stage, "u=s" =&gt; \$userid, "p=s" =&gt; \$passwd, "port=i" =&gt; \$port, "db=s" =&gt; \$db, "tb=s" =&gt; \$tb, 'help|?' =&gt; \$help);
 if ($help) {
 &amp;usage;
 exit 1;
 }

################################################################################
# main
################################################################################

#&amp;check_table;
print "Connecting to database $db.$tb on $host:$port\n";
# make connection to database
$connectionInfo="DBI:mysql:$db:$host:$port";
$dbh = DBI-&gt;connect($connectionInfo,$userid,$passwd) || die ;
&amp;lock_table;
&amp;flush_table;
&amp;move_files;
&amp;flush_table;
$sth-&gt;finish()
 or die "Couldn't execute statement: " . $sth-&gt;errstr;
print "Disconnecting from MySQL.\n";
$dbh-&gt;disconnect        # disconnect from database
 or die "Couldn't execute statement: " . $sth-&gt;errstr;

print "DONE\n";
exit 0;

################################################################################
# Subrutines
################################################################################
sub lock_table {                # Lock table to prevent READs
 print "Locking Table\n";
 $query = "LOCK TABLES $tb WRITE;";
 $sth = $dbh-&gt;prepare($query);
 $sth-&gt;execute()
 or die "Couldn't execute statement: " . $sth-&gt;errstr;
}
sub unlock_table {              # Un-Lock table to allow use.
 print "UnLocking Table\n";
 $query = "UNLOCK TABLES;";
 $sth = $dbh-&gt;prepare($query);
 $sth-&gt;execute()
 or die "Couldn't execute statement: " . $sth-&gt;errstr;
}
sub flush_table {               # Flush table cache data.
 print "Flushing Table\n";
 $query = "FLUSH TABLE $tb";
 $sth = $dbh-&gt;prepare($query);
 $sth-&gt;execute()
 or die "Couldn't execute statement: " . $sth-&gt;errstr;
}
sub move_files {                        # Move new data to table files being used.
 print "Moving Table Files\n";
#    `rm -f /data/mysql/$db/$tb.frm /data/mysql/$db/$tb.MYD /data/mysql/$db/$tb.MYI`;
#    `mv $stage/$tb.frm  $stage/$tb.MYD $stage/$tb.MYI /data/mysql/$db`;
 if (-e "$stage/$tb.frm" and -e "$stage/$tb.MYD" and -e "$stage/$tb.MYI" ) {
 move("$stage/$tb.frm", "$mydir/$db");
 move("$stage/$tb.MYD", "$mydir/$db");
 move("$stage/$tb.MYI", "$mydir/$db");
 } else {
 print "There is a file missing! Files NOT moved.\n";
 }
}
sub check_table {
 $status = `mysqlcheck $stage $tb`;
 print "Status is  $status";
 if ($status) {
 print "New data table is BAD!\n";
 exit 2;
 }
}
sub usage ($) {
 my ($message)= @_;
 if ( $message )
 {
 print STDERR "$message\n";
 }

print &lt;&lt;HERE;

$0 [ OPTIONS ]
Options are:
 -mydir=      Directory containing mysql data ($mydir)
 -stage=      Staging database ($stage) where new data table is held
 -u=          User name ($userid)
 -p=          Password ($passwd) -port= Port ($port)
 -db=         Destination database holding destination table ($db)
 -tb=         Table ($tb)
 -help        Prints this help
HERE
}</pre>
<p><img class="alignnone" src="http://mark.grennan.com/images/MarkGrennanSigniture.bmp" alt="" width="166" height="69" /></p>
<p><a href="http://twitter.com/share?url=http%3A%2F%2Fwww.mysqlfanboy.com%2F2010%2F05%2Fmg_hot_replace_table-pl%2F&amp;count=none&amp;via=MySQLFanBoy&amp;related=MySQLFanBoy:Site+Twitter+account&amp;text=mg_hot_replace_table.pl - MySQL Fanboy" class="twitter-share-button">Tweet</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.mysqlfanboy.com/2010/05/mg_hot_replace_table-pl/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
