You are viewing a plain text version of this content. The canonical link for it is here.
Posted to common-commits@hadoop.apache.org by Apache Wiki <wi...@apache.org> on 2014/07/08 19:39:31 UTC
[Hadoop Wiki] Update of "ShellScriptProgrammingGuide" by SomeOtherAccount
Dear Wiki user,
You have subscribed to a wiki page or wiki category on "Hadoop Wiki" for change notification.
The "ShellScriptProgrammingGuide" page has been changed by SomeOtherAccount:
https://wiki.apache.org/hadoop/ShellScriptProgrammingGuide
New page:
= Introduction =
With [[https://issues.apache.org/jira/browse/HADOOP-9902|HADOOP-9902]], the shell script code base has been refactored, with common functions and utilities put into a shell library (hadoop-functions.sh). Here are some tips and tricks to get the most out of using this functionality:
= The Skeleton =
All properly built shell scripts contain the following sections:
1. `hadoop_usage` function that contains an alphabetized list of subcommands and their description. This is used when the user directly asks for help, a command line syntax error, etc.
2. `HADOOP_LIBEXEC_DIR` configured. This should be the location of where `hadoop-functions.sh`, `hadoop-config.sh`, etc, are located.
3. `HADOOP_NEW_CONFIG=true`. This tells the rest of the system that the code being executed is aware that it is using the new shell API and it will call the routines it needs to call on its own. If this isn't set, then several default actions that were done in Hadoop 2.x and earlier are executed and several key parts of the functionality are lost.
4. `$HADOOP_LIBEXEC_DIR/___-config.sh` is executed, where ___ is the subproject. HDFS scripts should call `hdfs-config.sh`. MAPRED scripts should call `mapred-config.sh` YARN scripts should call `yarn-config.sh`. Everything else should call `hadoop-config.sh`. This does a lot of standard initialization, processes standard options, etc. This is also what provides override capabilities for subproject specific environment variables. For example, the system will normally ignore `yarn-env.sh`, but `yarn-config.sh` will activate those settings.
5. At this point, this is where the majority of your code goes. Programs should process the rest of the arguments and doing whatever their script is supposed to do.
6. Before executing a Java program or giving user output, call `hadoop_finalize`. This finishes up the configuration details: adds the user class path, fixes up any missing Java properties, configures library paths, etc.
7. Either an `exit` or an `exec`. This should return 0 for success and 1 or higher for failure.
= Things to Avoid =
* Adding more globals or project specific globals and/or entries in *-env.sh. In a lot of cases, there is pre-existing functionality that already does what you might need to do. Additionally, every configuration option makes it that much harder for end users.
* Mutli-level `if`'s where the comparisons are static strings. Use case statements.
* BSDisms, GNUisms, or SysVisms. Double check your esoteric command and parameters on multiple operating systems. Usually a quick Google search will pull up man pages
= Other Best Practices =
* If you need a new global variable for additional functionality, start it with HADOOP_ for common, HDFS_ for HDFS, YARN_ for YARN, and MAPRED_ for MapReduce. It should be documented in either *-env.sh or hadoop-functions.sh if it isn't meant to be touched by users. This helps prevents our variables from clobbering other people.
* If you need to add a new function, it should start with hadoop_ and declare any local variables with the `local` tag. This allows for 3rd parties to use our function libraries without worry about conflicts.
= Adding a Subcommand to an Existing Script =
In order to add a new subcommand, there are two things that need to be done:
1. Add a line to that script's `hadoop_usage` function that lists the name of the subcommand and what it does. This should be alphabetized.
2. Add an additional entry in the case conditional. Depending upon what is being added, several things may need to be done:
a. Set the `CLASS` to the Java method.
b. Add $HADOOP_CLIENT_OPTS to $HADOOP_OPTS (or, for YARN apps, $YARN_CLIENT_OPTS to $YARN_OPTS) if this is an interactive application or for some other reason should have the user client settings applied.
c. For methods that can also be daemons, set `daemon=true`. This will allow for the `--daemon` option to work.
d.