测试环境的hive cli启动时报错:
Unable to determine Hadoop version information.
hive是一个shell:
使用sh -x跟踪运行过程,可以看到其通过hadoop version获取hdfs的版本
相关的代码如下:
# Make sure we're using a compatible version of Hadoopif [ "x$HADOOP_VERSION" == "x" ]; then HADOOP_VERSION=$($HADOOP version | awk '{if (NR == 1) {print $2;}}');fiecho "hadoop_version is $HADOOP_VERSION"# Save the regex to a var to workaround quoting incompatabilities# between Bash 3.1 and 3.2hadoop_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 5fiif [ "$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 6fi
先使用
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的第一行输出严格来说不太好,最好使用正则来处理,更改如下:
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