shenhp 发表于 2016-12-9 06:14:09

Hadoop Streaming 实战: 实用Partitioner类KeyFieldBasedPartitioner

  我们知道,一个典型的Map-Reduce过程包括:Input->Map->Patition->Reduce->Output。Pation负责把Map任务输出的中间结果按key分发给不同的Reduce任务进行处理。Hadoop 提供了一个非常实用的partitioner类KeyFieldBasedPartitioner,通过配置相应的参数就可以使用。通过KeyFieldBasedPartitioner可以方便地实现二次排序。   
使用方法:   
-partitioner org.apache.hadoop.mapred.lib.KeyFieldBasedPartitioner      
一般配合:   
-D map.output.key.field.separator及-D num.key.fields.for.partition使用。   
map.output.key.field.separator指定key内部的分隔符   
num.key.fields.for.partition指定对key分出来的前几部分做partition而不是整个key
  示例:   
1. 编写map程序mapper.sh;reduce程序reducer.sh; 测试数据test.txt   
  mapper.sh:#!/bin/shcatreducer.sh:#!/bin/shsorttest.txt内容:1,2,1,1,11,2,2,1,11,3,1,1,11,3,2,1,11,3,3,1,11,2,3,1,11,3,1,1,11,3,2,1,11,3,3,1,1
  2. 测试数据test.txt放入hdfs,运行map-reduce程序
$ hadoop streaming /-D stream.reduce.output.field.separator=, /-D stream.num.reduce.output.key.fields=4 /-D map.output.key.field.separator=, /-D num.key.fields.for.partition=2 /-partitioner org.apache.hadoop.mapred.lib.KeyFieldBasedPartitioner /-input /app/test/test.txt/-output /app/test/test_result / -mapper ./mapper.sh/-reducer ./reducer.sh /-file mapper.sh /-file reducer.sh /-jobconf mapre.job.name="sep_test"$ hadoop fs –cat /app/test/test_result/part-000031,2,1,1   11,2,2,1   11,2,3,1   1$ hadoop fs –cat /app/test/test_result/part-000041,3,1,1   11,3,1,1   11,3,2,1   11,3,2,1   11,3,3,1   11,3,3,1   1通过这种方式,就做到前4个字段是key,但是通过前两个字段进行partition的目的
页: [1]
查看完整版本: Hadoop Streaming 实战: 实用Partitioner类KeyFieldBasedPartitioner