我厌倦了hadoop fs仅仅查询 HDFS的缓慢启动时间。不过,这不是 HDFS 本身的问题,因为在 Pig“grunt shell”中使用 HDFS 文件系统命令非常快。但是当我只想发出一些 HDFS 命令时总是启动 grunt shell 是不切实际的。所以我写了这个脚本来为我在后台启动一个 grunt shell 实例并保持它打开以供后续调用:
#!/bin/bash
in=/tmp/grunt_in
out=/tmp/grunt_out
err=/tmp/grunt_err
if [ ! -p $in ]
then
mkfifo $in
mkfifo $out
( pig <>$in >$out 2>$err; rm $in $out ) &
disown
fi
>$err # Truncate errors
echo "fs $*" >$in
echo >$in
echo "-- end" >$in
sed -n '/^grunt> -- end/q;/^grunt>/d;p' $out
cat $err >&2
Run Code Online (Sandbox Code Playgroud)
当然,不仅输入必须发送到脚本,而且脚本的输出必须重定向到我当前的 bash 会话。我在这里使用/tmp/grunt_in和/tmp/grunt_outFIFO 来实现这一点。为了确定何时pig处理命令,我发送了一条"-- …