您的位置:首页 > 运维架构

Hadoop Streaming 实战: 输出文件分割

2013-12-20 15:24 417 查看
我们知道,Hadoop streaming框架默认情况下会以'/t’作为分隔符,将每行第一个'/t’之前的部分作为key,其余内容作为value,如果没有'/t’分隔符,则整行作为key;这个key/tvalue对又作为reduce的输入。hadoop
提供配置供用户自主设置分隔符。

-D stream.map.output.field.separator :设置map输出中key和value的分隔符

-D stream.num.map.output.key.fields : 设置map程序分隔符的位置,该位置之前的部分作为key,之后的部分作为value

-D map.output.key.field.separator : 设置map输出中key内部的分割符

-D num.key.fields.for.partition : 指定分桶时,key按照分隔符切割后,其中用于分桶key所占的列数(配合-partitioner org.apache.hadoop.mapred.lib.KeyFieldBasedPartitioner 使用)

-D stream.reduce.output.field.separator:设置reduce输出中key和value的分隔符

-D stream.num.reduce.output.key.fields:设置reduce程序分隔符的位置

实例:

1. 编写map程序mapper.sh;reduce程序reducer.sh; 测试数据test.txt

[html] view
plain copy

mapper.sh:

#!/bin/sh

cat

reducer.sh:

#!/bin/sh

sort

test.txt内容:

1,2,1,1,1

1,2,2,1,1

1,3,1,1,1

1,3,2,1,1

1,3,3,1,1

1,2,3,1,1

1,3,1,1,1

1,3,2,1,1

1,3,3,1,1

2. test.txt放入hadoop,两种方式运行

1)无分隔符设置运行

[html] view
plain copy

$ hadoop fs -put test.txt /app/test/

$ hadoop streaming -input /app/test/test.txt /

-output /app/test/test_result /

-mapper ./mapper.sh -reducer ./reducer.sh

-file mapper.sh -file reducer.sh /

-jobconf mapred.reduce.tasks=2 /

-jobconf mapre.job.name="sep_test"

$ hadoop fs –cat /app/test/test_result/part-00000

1,2,2,1,1

1,3,1,1,1

1,3,1,1,1

1,3,3,1,1

1,3,3,1,1

$ hadoop fs –cat /app/test/test_result/part-00001

1,2,1,1,1

1,2,3,1,1

1,3,2,1,1

1,3,2,1,1

2)设置分隔符运行

[html] view
plain copy

$ hadoop streaming -D stream.reduce.output.field.separator=,

-D stream.num.reduce.output.key.fields=2

-input /app/test/test.txt

-output /app/test/test_result_1

-mapper ./mapper.sh -reducer ./reducer.sh

-file mapper.sh -file reducer.sh

-jobconf mapred.reduce.tasks=2

-jobconf mapre.job.name="sep_test"

$ hadoop fs -cat /app/test/test_result_1/part-00000

1,2 1,1,1

1,2 2,1,1

1,2 3,1,1

$ hadoop fs -cat /app/test/test_result_1/part-00001

1,3 1,1,1

1,3 1,1,1

1,3 2,1,1

1,3 2,1,1

1,3 3,1,1

1,3 3,1,1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: