<?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>Engineering Rapleaf &#187; auto-complete</title>
	<atom:link href="http://blog.rapleaf.com/dev/tag/auto-complete/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.rapleaf.com/dev</link>
	<description>For engineers, by engineers.</description>
	<lastBuildDate>Mon, 12 Dec 2011 08:57:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Command-line auto completion for Hadoop DFS commands</title>
		<link>http://blog.rapleaf.com/dev/2009/11/17/command-line-auto-completion-for-hadoop-dfs-commands/</link>
		<comments>http://blog.rapleaf.com/dev/2009/11/17/command-line-auto-completion-for-hadoop-dfs-commands/#comments</comments>
		<pubDate>Wed, 18 Nov 2009 01:31:13 +0000</pubDate>
		<dc:creator>ckline</dc:creator>
				<category><![CDATA[bash]]></category>
		<category><![CDATA[Hadoop]]></category>
		<category><![CDATA[HDFS]]></category>
		<category><![CDATA[auto-complete]]></category>
		<category><![CDATA[auto-completion]]></category>
		<category><![CDATA[bash_completion]]></category>
		<category><![CDATA[command-line]]></category>

		<guid isPermaLink="false">http://blog.rapleaf.com/dev/?p=304</guid>
		<description><![CDATA[We like to keep things simple here at Rapleaf. One small tweak we made right after we installed hadoop was to alias 'hadoop dfs' to 'hdfs'. It rolls off the fingers nicely. We are also constantly typing 'hdfs -ls this' or 'hdfs -du that'. If we are not sure what this/that is, we type 'hdfs [...]]]></description>
			<content:encoded><![CDATA[<p>We like to keep things simple here at Rapleaf.  One small tweak we made right after we installed hadoop was to alias <tt>'hadoop dfs'</tt> to <tt>'hdfs'</tt>.  It rolls off the fingers nicely.  We are also constantly typing <tt>'hdfs -ls this'</tt> or <tt>'hdfs -du that'</tt>.  If we are not sure what <tt>this/that</tt> is, we type <tt>'hdfs -ls /this/what'</tt>, then <tt>'hdfs -ls /this/what/ever'</tt>, followed by a copy and paste or two.  Thanks to our recent HackLeaf day and Nathan&#8217;s great idea, we no longer have to go through all of that.  Just type <tt>'hdfs -ls [tab]'</tt> and it works just like bash command-line completion.</p>
<p>This was easy to implement once I found the <a href="http://www.caliban.org/bash/#completion">programmable completion</a> tool by Ian Macdonald.  I just added the following section to bash_completion:</p>
<p>[cc lang="bash" tab_size="2"]<br />
# hdfs(1) completion<br />
#<br />
have hadoop &amp;&amp;<br />
_hdfs()<br />
{<br />
  local cur prev</p>
<p>  COMPREPLY=()<br />
  cur=${COMP_WORDS[COMP_CWORD]}<br />
  prev=${COMP_WORDS[COMP_CWORD-1]}</p>
<p>  if [[ "$prev" == hdfs ]]; then<br />
    COMPREPLY=( $( compgen -W &#8216;-ls -lsr -du -dus -count -mv -cp -rm<br />
      -rmr -expunge -put -copyFromLocal -moveToLocal -mkdir -setrep<br />
      -touchz -test -stat -tail -chmod -chown -chgrp -help&#8217; &#8212; $cur ) )<br />
  fi</p>
<p>  if [[ "$prev" == -ls ]] || [[ "$prev" == -lsr ]] ||<br />
     [[ "$prev" == -du ]] || [[ "$prev" == -dus ]] ||<br />
     [[ "$prev" == -cat ]] || [[ "$prev" == -mkdir ]] ||<br />
     [[ "$prev" == -put ]] || [[ "$prev" == -rm ]] ||<br />
     [[ "$prev" == -rmr ]] || [[ "$prev" == -tail ]] ||<br />
     [[ "$prev" == -cp ]]; then<br />
    if [[ -z "$cur" ]]; then<br />
      COMPREPLY=( $( compgen -W &#8220;$( hdfs -ls / 2&gt;-|grep -v ^Found|awk &#8216;{print $8}&#8217; )&#8221; &#8212; &#8220;$cur&#8221; ) )<br />
    elif [[ `echo $cur | grep /$` ]]; then<br />
      COMPREPLY=( $( compgen -W &#8220;$( hdfs -ls $cur 2&gt;-|grep -v ^Found|awk &#8216;{print $8}&#8217; )&#8221; &#8212; &#8220;$cur&#8221; ) )<br />
    else<br />
      COMPREPLY=( $( compgen -W &#8220;$( hdfs -ls $cur* 2&gt;-|grep -v ^Found|awk &#8216;{print $8}&#8217; )&#8221; &#8212; &#8220;$cur&#8221; ) )<br />
    fi<br />
  fi<br />
} &amp;&amp;<br />
complete -F _hdfs hdfs<br />
[/cc]</p>
<p>I&#8217;m sure there are some ways to make the code more elegant, but it is called HackLeaf, after all.  This bit of code builds on top of other functions in the script, but the basic idea is pretty simple.  <tt>cur</tt> contains the current word you are typing, so this would be a partial command or partial path.  <tt>prev</tt> contains the previous word.  If the previous word is <tt>hdfs</tt>, then we present the user with valid arguments to <tt>hdfs</tt>.  If the previous word is <tt>-ls</tt> (or any other command where you want a path/file), then present the user with the possibilities for that path or partial path.  HDFS defaults to the user&#8217;s home directory if no path is provided, so we override that by presenting the user with the possibilities under &#8220;/&#8221;.  Finally, <tt>COMPREPLY</tt> returns the possibilities to the user on the command-line.</p>
<p>Be sure to check out some of the other features of bash_completion, particularly <tt>ssh</tt> and <tt>chkconfig</tt>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.rapleaf.com/dev/2009/11/17/command-line-auto-completion-for-hadoop-dfs-commands/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
	</channel>
</rss>

