微信搜索lxw1234bigdata | 邀请体验:数阅–数据管理、OLAP分析与可视化平台 | 赞助作者:赞助作者

MapReduce和Hive支持递归子目录作为输入

Hadoop lxw1234@qq.com 24507℃ 1评论

关键字:MapReduce、Hive、子目录、递归、输入、Input、mapreduce.input.fileinputformat.input.dir.recursive、hive.mapred.supports.subdirectories

一般情况下,传递给MapReduce和Hive的input文件夹中不能包含子目录,否则就会报错。但后来增加了递归遍历Input目录的功能,

这个貌似是从0.23开始的,具体不清楚,反正在0.20中是不支持的。

我使用的Hadoop版本为:hadoop-2.3.0-cdh5.0.0

Hive版本为:apache-hive-0.13.1-bin

具体使用示例如下:

数据准备

 

cat 1.txt 
1
1
1

cat 2.txt 
2
2

hadoop fs -mkdir /tmp/lxw1234/
hadoop fs -mkdir /tmp/lxw1234/subdir/
hadoop fs -put 1.txt /tmp/lxw1234/
hadoop fs -put 2.txt /tmp/lxw1234/subdir/

hadoop fs -ls -R /tmp/lxw1234/
-rw-r--r--   2 lxw1234 supergroup          6 2015-07-08 13:56 /tmp/lxw1234/1.txt
drwxr-xr-x   - lxw1234 supergroup          0 2015-07-08 13:56 /tmp/lxw1234/subdir
-rw-r--r--   2 lxw1234 supergroup          4 2015-07-08 13:56 /tmp/lxw1234/subdir/2.txt

1.txt在/tmp/lxw1234/下,2.txt在/tmp/lxw1234/subdir/目录下。

 

MapReduce

默认情况下,mapreduce.input.fileinputformat.input.dir.recursive为flase.

运行wordcount:

hadoop  jar  hadoop-mapreduce-examples-2.3.0-cdh5.0.0.jar  wordcount  /tmp/lxw1234/  /tmp/output/

MapReduce

报错 “Error: java.io.FileNotFoundException: Path is not a file: /tmp/lxw1234/subdir”,原因是MapReduce获取/tmp/lxw1234下的列表,把/tmp/lxw1234/subdir也作为一个input file来处理。

设置mapreduce.input.fileinputformat.input.dir.recursive=true,这个参数是客户端参数,可以在MapReduce中设置,也可以在mapred-site.xml中设置,无所谓。

再运行上面的wordcount命令:

hadoop  jar  hadoop-mapreduce-examples-2.3.0-cdh5.0.0.jar  wordcount  /tmp/lxw1234/  /tmp/output/

Job成功执行,查看结果:

hadoop fs -cat /tmp/output/*
1       3
2       2

正确。

 

Hive

仍然使用上面的HDFS路径/tmp/lxw1234/建表:

CREATE EXTERNAL TABLE lxw1234 (d string) stored AS textfile location '/tmp/lxw1234/';

查询:select * from lxw1234;

hive

同样报错 “Not a file: hdfs://cdh5/tmp/lxw1234/subdir” 。

在hive-cli中设置参数:

set hive.mapred.supports.subdirectories=true;
set mapreduce.input.fileinputformat.input.dir.recursive=true;

再执行:

hive

结果正确。

 

参数mapreduce.input.fileinputformat.input.dir.recursive表示是否在MapReduce中递归遍历Input目录,

Hadoop1.0中该参数为:mapred.input.dir.recursive

Hive中设置hive.mapred.supports.subdirectories=true之后,即可将包含子目录的文件夹作为表或分区的数据目录,

查询的时候会递归遍历查询,但需要Hadoop的版本支持该功能才可以。

比如:hive0.13+hadoop0.20就不起作用。

 

Hive相关文章(持续更新)

一起学Hive系列

—-Hive概述,Hive是什么

—-Hive函数大全-完整版

—-Hive中的数据库(Database)和表(Table)

—-Hive的安装配置

—-Hive的视图和分区

—-Hive的动态分区

—-向Hive表中加载数据

—-使用Hive命令行

—-Hive的查询语句SELECT

—-Hive中Join的原理和机制

—-Hive中Join的类型和用法

—-Hive SQL的优化

—-Hive整合HBase,操作HBase表

—-Hive的元数据表结构详解

Hive分析函数系列

Hive索引

hive优化之——控制hive任务中的map数和reduce数

 

 

 

如果觉得本博客对您有帮助,请 赞助作者

转载请注明:lxw的大数据田地 » MapReduce和Hive支持递归子目录作为输入

喜欢 (13)
分享 (0)
发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
(1)个小伙伴在吐槽
  1. 今天用到了,非常感谢,还有博主的Hive手册也是非常好用的,支持希望多写一些这样的技术文章
    修仙途道2015-10-16 10:11 回复