解讀hadoop命令

【大數(shù)據(jù)筆記】-解讀hadoop命令 - flyfoxs - ITeye技術(shù)網(wǎng)站 http://flyfoxs.iteye.com/blog/2113064

下面是hadoop發(fā)布版本, bin目錄下面的hadoop命令的源碼,hadoop命令支持好多種參數(shù),一直記不住,想通過精度這部分代碼,能記住部分參數(shù).

!/usr/bin/env bash

Licensed to the Apache Software Foundation (ASF) under one or more

contributor license agreements. See the NOTICE file distributed with

this work for additional information regarding copyright ownership.

The ASF licenses this file to You under the Apache License, Version 2.0

(the "License"); you may not use this file except in compliance with

the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software

distributed under the License is distributed on an "AS IS" BASIS,

WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

See the License for the specific language governing permissions and

limitations under the License.

This script runs the hadoop core commands.

這3行命令的主要目的是,獲取Hadoop運行所在目錄.

bin=which $0
bin=dirname ${bin}
bin=cd "$bin"; pwd

定位找到 hadoop-config.sh 文件,里面包含了很多Hadoop命令的配置文件.

先找HADOOP_LIBEXEC_DIR目錄,如果沒有定義,就使用默認(rèn)的路徑,也就是hadoop根目錄下面的libexec

DEFAULT_LIBEXEC_DIR="$bin"/../libexec
HADOOP_LIBEXEC_DIR=${HADOOP_LIBEXEC_DIR:-$DEFAULT_LIBEXEC_DIR}
. $HADOOP_LIBEXEC_DIR/hadoop-config.sh

function print_usage(){
echo "Usage: hadoop [--config confdir] COMMAND"
echo " where COMMAND is one of:"
echo " fs run a generic filesystem user client"
echo " version print the version"
echo " jar <jar> run a jar file"
echo " checknative [-a|-h] check native hadoop and compression libraries availability"
echo " distcp <srcurl> <desturl> copy file or directories recursively"
echo " archive -archiveName NAME -p <parent path> <src>* <dest> create a hadoop archive"
echo " classpath prints the class path needed to get the"
echo " Hadoop jar and the required libraries"
echo " daemonlog get/set the log level for each daemon"
echo " or"
echo " CLASSNAME run the class named CLASSNAME"
echo ""
echo "Most commands print help when invoked w/o parameters."
}

如果命令參數(shù)個數(shù)為0,則打印提示,退出

if [ $# = 0 ]; then
print_usage
exit
fi

解析第1個參數(shù),第0個參數(shù)是命令本身

COMMAND=$1
case $COMMAND in

usage flags

--help|-help|-h)
print_usage
exit
;;

hdfs commands

namenode|secondarynamenode|datanode|dfs|dfsadmin|fsck|balancer|fetchdt|oiv|dfsgroups)
echo "DEPRECATED: Use of this script to execute hdfs command is deprecated." 1>&2
echo "Instead use the hdfs command for it." 1>&2
echo "" 1>&2
#try to locate hdfs and if present, delegate to it.
shift
if [ -f "${HADOOP_HDFS_HOME}"/bin/hdfs ]; then
exec "${HADOOP_HDFS_HOME}"/bin/hdfs ${COMMAND/dfsgroups/groups} "$@"
elif [ -f "${HADOOP_PREFIX}"/bin/hdfs ]; then
exec "${HADOOP_PREFIX}"/bin/hdfs ${COMMAND/dfsgroups/groups} "$@"
else
echo "HADOOP_HDFS_HOME not found!"
exit 1
fi
;;

mapred commands for backwards compatibility

pipes|job|queue|mrgroups|mradmin|jobtracker|tasktracker|mrhaadmin|mrzkfc|jobtrackerha)
echo "DEPRECATED: Use of this script to execute mapred command is deprecated." 1>&2
echo "Instead use the mapred command for it." 1>&2
echo "" 1>&2
#try to locate mapred and if present, delegate to it.
shift
if [ -f "${HADOOP_MAPRED_HOME}"/bin/mapred ]; then
exec "${HADOOP_MAPRED_HOME}"/bin/mapred ${COMMAND/mrgroups/groups} "$@"
elif [ -f "${HADOOP_PREFIX}"/bin/mapred ]; then
exec "${HADOOP_PREFIX}"/bin/mapred ${COMMAND/mrgroups/groups} "$@"
else
echo "HADOOP_MAPRED_HOME not found!"
exit 1
fi
;;

打印出Hadoop執(zhí)行時的classpath,方便查找classpath的錯誤

classpath)
if $cygwin; then
CLASSPATH=cygpath -p -w "$CLASSPATH"
fi
echo $CLASSPATH
exit
;;

core commands

)
# the core commands
if [ "$COMMAND" = "fs" ] ; then
CLASS=org.apache.hadoop.fs.FsShell
elif [ "$COMMAND" = "version" ] ; then
CLASS=org.apache.hadoop.util.VersionInfo
elif [ "$COMMAND" = "jar" ] ; then
CLASS=org.apache.hadoop.util.RunJar
elif [ "$COMMAND" = "checknative" ] ; then
CLASS=org.apache.hadoop.util.NativeLibraryChecker
elif [ "$COMMAND" = "distcp" ] ; then
CLASS=org.apache.hadoop.tools.DistCp
CLASSPATH=${CLASSPATH}:${TOOL_PATH}
elif [ "$COMMAND" = "daemonlog" ] ; then
CLASS=org.apache.hadoop.log.LogLevel
elif [ "$COMMAND" = "archive" ] ; then
CLASS=org.apache.hadoop.tools.HadoopArchives
CLASSPATH=${CLASSPATH}:${TOOL_PATH}
elif [[ "$COMMAND" = -
]] ; then
# class and package names cannot begin with a -
echo "Error: No command named `$COMMAND' was found. Perhaps you meant `hadoop ${COMMAND#-}'"
exit 1
else
#如果上面的都沒匹配上,那么第一個參數(shù)作為classname 來解析,比如下面就是一個示例
#hadoop org.apache.hadoop.examples.WordCount /tmp/15 /tmp/46
CLASS=$COMMAND
fi

#刪除$@中的第一個參數(shù),比如"hadoop org.apache.hadoop.examples.WordCount /tmp/15 /tmp/46"
#在運行shift之前$@=org.apache.hadoop.examples.WordCount /tmp/15 /tmp/46
#之后$@=/tmp/15 /tmp/46
shift

# Always respect HADOOP_OPTS and HADOOP_CLIENT_OPTS
# 對應(yīng)的這兩個變量默認(rèn)的定義在文件hadoop-config.sh,如果要修改啟動參數(shù),也可以修改這個文件,比如想開啟遠(yuǎn)程debug
HADOOP_OPTS="$HADOOP_OPTS $HADOOP_CLIENT_OPTS"

#make sure security appender is turned off
HADOOP_OPTS="$HADOOP_OPTS -Dhadoop.security.logger=${HADOOP_SECURITY_LOGGER:-INFO,NullAppender}"

#兼容cygwin模擬器
if $cygwin; then
  CLASSPATH=`cygpath -p -w "$CLASSPATH"`
fi

#沒什么意思,放在這是為了方便修改擴(kuò)展CLASSPATH
export CLASSPATH=$CLASSPATH
exec "$JAVA" $JAVA_HEAP_MAX $HADOOP_OPTS $CLASS "$@"
;;

esac

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末个绍,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子呻引,更是在濱河造成了極大的恐慌,老刑警劉巖所森,帶你破解...
    沈念sama閱讀 206,126評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異荚板,居然都是意外死亡址遇,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,254評論 2 382
  • 文/潘曉璐 我一進(jìn)店門滋戳,熙熙樓的掌柜王于貴愁眉苦臉地迎上來钻蔑,“玉大人,你說我怎么就攤上這事奸鸯∵湫Γ” “怎么了?”我有些...
    開封第一講書人閱讀 152,445評論 0 341
  • 文/不壞的土叔 我叫張陵娄涩,是天一觀的道長窗怒。 經(jīng)常有香客問我映跟,道長,這世上最難降的妖魔是什么扬虚? 我笑而不...
    開封第一講書人閱讀 55,185評論 1 278
  • 正文 為了忘掉前任努隙,我火速辦了婚禮,結(jié)果婚禮上辜昵,老公的妹妹穿的比我還像新娘荸镊。我一直安慰自己,他們只是感情好堪置,可當(dāng)我...
    茶點故事閱讀 64,178評論 5 371
  • 文/花漫 我一把揭開白布躬存。 她就那樣靜靜地躺著,像睡著了一般舀锨。 火紅的嫁衣襯著肌膚如雪岭洲。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 48,970評論 1 284
  • 那天坎匿,我揣著相機(jī)與錄音钦椭,去河邊找鬼。 笑死碑诉,一個胖子當(dāng)著我的面吹牛彪腔,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播进栽,決...
    沈念sama閱讀 38,276評論 3 399
  • 文/蒼蘭香墨 我猛地睜開眼德挣,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了快毛?” 一聲冷哼從身側(cè)響起格嗅,我...
    開封第一講書人閱讀 36,927評論 0 259
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎唠帝,沒想到半個月后屯掖,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,400評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡襟衰,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 35,883評論 2 323
  • 正文 我和宋清朗相戀三年贴铜,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片瀑晒。...
    茶點故事閱讀 37,997評論 1 333
  • 序言:一個原本活蹦亂跳的男人離奇死亡绍坝,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出苔悦,到底是詐尸還是另有隱情轩褐,我是刑警寧澤,帶...
    沈念sama閱讀 33,646評論 4 322
  • 正文 年R本政府宣布玖详,位于F島的核電站把介,受9級特大地震影響勤讽,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜拗踢,卻給世界環(huán)境...
    茶點故事閱讀 39,213評論 3 307
  • 文/蒙蒙 一地技、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧秒拔,春花似錦莫矗、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,204評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至庵芭,卻和暖如春妹懒,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背双吆。 一陣腳步聲響...
    開封第一講書人閱讀 31,423評論 1 260
  • 我被黑心中介騙來泰國打工眨唬, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人好乐。 一個月前我還...
    沈念sama閱讀 45,423評論 2 352
  • 正文 我出身青樓匾竿,卻偏偏與公主長得像,于是被迫代替她去往敵國和親蔚万。 傳聞我的和親對象是個殘疾皇子岭妖,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 42,722評論 2 345

推薦閱讀更多精彩內(nèi)容