测试环境的hive cli启动时报错:
1 | Unable to determine Hadoop version information. |
hive是一个shell:
使用sh -x跟踪运行过程,可以看到其通过hadoop version获取hdfs的版本
相关的代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | # Make sure we're using a compatible version of Hadoop if [ "x$HADOOP_VERSION" == "x" ]; then HADOOP_VERSION=$($HADOOP version | awk '{if (NR == 1) {print $2;}}' ); fi echo "hadoop_version is $HADOOP_VERSION" # Save the regex to a var to workaround quoting incompatabilities # between Bash 3.1 and 3.2 hadoop_version_re= "^([[:digit:]]+)\.([[:digit:]]+)(\.([[:digit:]]+))?.*$" if [[ "$HADOOP_VERSION" =~ $hadoop_version_re ]]; then hadoop_major_ver=${BASH_REMATCH[1]} hadoop_minor_ver=${BASH_REMATCH[2]} hadoop_patch_ver=${BASH_REMATCH[4]} else echo "Unable to determine Hadoop version information." echo "'hadoop version' returned:" echo `$HADOOP version` exit 5 fi if [ "$hadoop_major_ver" -lt "1" -a "$hadoop_minor_ver$hadoop_patch_ver" -lt "201" ]; then echo "Hive requires Hadoop 0.20.x (x >= 1)." echo "'hadoop version' returned:" echo `$HADOOP version` exit 6 fi |
先使用
1 | hadoop version | awk '{if (NR == 1) {print $2;}}' |
来获取version输出地第一行,然后用到了3.x shell中的正则功能。,最后通过BASH_REMATCH数组来判断匹配组,hadoop 也是一个shell ,运行hadoop version时其实是调用了org.apache.hadoop.util.VersionInfo类
而这里由于之前debug hadoop的变量问题,更改了hadoop的脚本,导致其第一行并没有输出version的信息。
这里其实使用hadoop version的第一行输出严格来说不太好,最好使用正则来处理,更改如下:
1 2 3 4 | if [ "x$HADOOP_VERSION" == "x" ]; then #HADOOP_VERSION=$($HADOOP version | awk '{if (NR == 1) {print $2;}}'); HADOOP_VERSION=$($HADOOP version | awk '{if($0 ~ /[[:alpha:]]+ ([[:digit:]]+)\.([[:digit:]]+)\.([[:digit:]]+)?.*$/){print $2};}' ); fi |
本文转自菜菜光 51CTO博客,原文链接:http://blog.51cto.com/caiguangguang/1579829,如需转载请自行联系原作者