Chinaunix首页 | 论坛 | 博客
  • 博客访问: 629578
  • 博文数量: 149
  • 博客积分: 3901
  • 博客等级: 中校
  • 技术积分: 1558
  • 用 户 组: 普通用户
  • 注册时间: 2009-02-16 14:33
文章分类

全部博文(149)

文章存档

2014年(2)

2013年(10)

2012年(32)

2011年(21)

2010年(84)

分类: 数据库开发技术

2010-07-24 11:43:37

转自:

使用 hadoop file shell 可以方便地向 hdfs put 文件,但是,该 shell 不支持从管道读取数据并放到 hdfs 文件中。它仅支持这样的 put 命令:

  1. cd $HADOOP_HOME  
  2. bin/hadoop fs -put localfile $hdfsFile  
  3. bin/hadoop fs -put localfiles $hdfsDir  

 幸好,主流的 unix (linux,bsd等)都有一个 /dev/fd/ 目录,可以用它实现从管道 put 文件



Shell代码 
  1. cd $HADOOP_HOME  
  2. if bin/hadoop fs -test -d $hdfsFile  
  3. then  
  4.     echo "$hdfsFile is a directory" >&2  
  5.     exit 1  
  6. fi  
  7. cat localfileS | bin/hadoop fs -put /dev/fd/0  $hdfsFile  
  8. if [[ "0 0" == ${PIPESTATUS[*]} ]]  
  9. then  
  10.     echo success  
  11. else  
  12.     bin/hadoop fs -rm $hdfsFile  
  13.     echo failed >&2  
  14. fi   

其中,使用 PIPESTATUS 检查错误 。

 

需要注意,使用 /dev/fd/0 put 文件时,hdfsFile 必须事先不存在,并且不能是一个目录,如果hdfsFile实际上是一个目录,那么,put 仍然正确执行,但是,hdfs 中的文件名将是 hdfsFile/0

 

/dev/fd/ 中是进程所有已打开的文件描述符列表,例如 /dev/fd/0 代表标准输入,/dev/fd/1 代表标准输出,/dev/fd/2 代表标准错误输出,等等,打开 /dev/fd/n 相当于调用 dup(n) 。

阅读(1040) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~