首页 > 代码库 > 理解Hadoop脚本hadoop-2.5.0/bin/hadoop
理解Hadoop脚本hadoop-2.5.0/bin/hadoop
1 #!/usr/bin/env bash 此处为什么不是 #!/bin/bash ?
考虑到程序的可移植性,env的作用就是为了找到正确的脚本解释器(这里就是bash),在不同的Linux系统上该解释器可能所处的位置不同
18 # This script runs the hadoop core commands.
19
20 bin=`which $0` shell的默认赋值是字符串赋值, $0表示脚本本身的名字,which locate the file
21 bin=`dirname ${bin}`
22 bin=`cd "$bin"; pwd`
23
24 DEFAULT_LIBEXEC_DIR="$bin"/../libexec
25 HADOOP_LIBEXEC_DIR=${HADOOP_LIBEXEC_DIR:-$DEFAULT_LIBEXEC_DIR}
26 . $HADOOP_LIBEXEC_DIR/hadoop-config.sh
27
28 function print_usage(){
29 echo "Usage: hadoop [--config confdir] COMMAND"
30 echo " where COMMAND is one of:"
31 echo " fs run a generic filesystem user client"
32 echo " version print the version"
33 echo " jar <jar> run a jar file"
34 echo " checknative [-a|-h] check native hadoop and compression libraries availability"
35 echo " distcp <srcurl> <desturl> copy file or directories recursively"
36 echo " archive -archiveName NAME -p <parent path> <src>* <dest> create a hadoop archive"
37 echo " classpath prints the class path needed to get the"
38 echo " Hadoop jar and the required libraries"
39 echo " daemonlog get/set the log level for each daemon"
40 echo " or"
41 echo " CLASSNAME run the class named CLASSNAME"
42 echo ""
43 echo "Most commands print help when invoked w/o parameters."
44 }
45
46 if [ $# = 0 ]; then
47 print_usage
48 exit
49 fi
50
51 COMMAND=$1
52 case $COMMAND in
53 # usage flags
54 --help|-help|-h)
55 print_usage
56 exit
57 ;;
58
59 #hdfs commands
60 namenode|secondarynamenode|datanode|dfs|dfsadmin|fsck|balancer|fetchdt|oiv|dfsgroups|portmap|nfs3)
61 echo "DEPRECATED: Use of this script to execute hdfs command is deprecated." 1>&2
62 echo "Instead use the hdfs command for it." 1>&2
63 echo "" 1>&2
64 #try to locate hdfs and if present, delegate to it.
65 shift
66 if [ -f "${HADOOP_HDFS_HOME}"/bin/hdfs ]; then
67 exec "${HADOOP_HDFS_HOME}"/bin/hdfs ${COMMAND/dfsgroups/groups} "$@"
68 elif [ -f "${HADOOP_PREFIX}"/bin/hdfs ]; then
69 exec "${HADOOP_PREFIX}"/bin/hdfs ${COMMAND/dfsgroups/groups} "$@"
70 else
71 echo "HADOOP_HDFS_HOME not found!"
72 exit 1
73 fi
74 ;;
75
76 #mapred commands for backwards compatibility
77 pipes|job|queue|mrgroups|mradmin|jobtracker|tasktracker)
78 echo "DEPRECATED: Use of this script to execute mapred command is deprecated." 1>&2
79 echo "Instead use the mapred command for it." 1>&2
80 echo "" 1>&2
81 #try to locate mapred and if present, delegate to it.
82 shift
83 if [ -f "${HADOOP_MAPRED_HOME}"/bin/mapred ]; then
84 exec "${HADOOP_MAPRED_HOME}"/bin/mapred ${COMMAND/mrgroups/groups} "$@"
85 elif [ -f "${HADOOP_PREFIX}"/bin/mapred ]; then
86 exec "${HADOOP_PREFIX}"/bin/mapred ${COMMAND/mrgroups/groups} "$@"
87 else
88 echo "HADOOP_MAPRED_HOME not found!"
89 exit 1
90 fi
91 ;;
92
93 classpath)
94 echo $CLASSPATH
95 exit
96 ;;
97
98 #core commands
99 *)
100 # the core commands
101 if [ "$COMMAND" = "fs" ] ; then
102 class="org".apache.hadoop.fs.FsShell
103 elif [ "$COMMAND" = "version" ] ; then
104 class="org".apache.hadoop.util.VersionInfo
105 elif [ "$COMMAND" = "jar" ] ; then
106 class="org".apache.hadoop.util.RunJar
107 elif [ "$COMMAND" = "checknative" ] ; then
108 class="org".apache.hadoop.util.NativeLibraryChecker
109 elif [ "$COMMAND" = "distcp" ] ; then
110 class="org".apache.hadoop.tools.DistCp
111 CLASSPATH=${CLASSPATH}:${TOOL_PATH}
112 elif [ "$COMMAND" = "daemonlog" ] ; then
113 class="org".apache.hadoop.log.LogLevel
114 elif [ "$COMMAND" = "archive" ] ; then
115 class="org".apache.hadoop.tools.HadoopArchives
116 CLASSPATH=${CLASSPATH}:${TOOL_PATH}
117 elif [[ "$COMMAND" = -* ]] ; then
118 # class and package names cannot begin with a -
119 echo "Error: No command named \`$COMMAND‘ was found. Perhaps you meant \`hadoop ${COMMAND#-}‘"
120 exit 1
121 else
122 CLASS=$COMMAND
123 fi
124 shift
125
126 # Always respect HADOOP_OPTS and HADOOP_CLIENT_OPTS
127 HADOOP_OPTS="$HADOOP_OPTS $HADOOP_CLIENT_OPTS"
128
129 #make sure security appender is turned off
130 HADOOP_OPTS="$HADOOP_OPTS -Dhadoop.security.logger=${HADOOP_SECURITY_LOGGER:-INFO,NullAppender}"
131
132 export CLASSPATH=$CLASSPATH
133 exec "$JAVA" $JAVA_HEAP_MAX $HADOOP_OPTS $CLASS "$@"
134 ;;
135
136 esac
理解Hadoop脚本hadoop-2.5.0/bin/hadoop