[实验]avro与non-avro的mapred例子-wordcount改写

avro非常适合用于hadoop。在开发的时候可能有这样的场景,输入的文件是non-avro的,输出的文件是avro的。这样就需要一个是非avro的mapper和一个avro的reducer。下面通过改写wordcount例子演示这个过程。
[b]Mapper[/b]

public class WordCountMapper extends MapReduceBase implements
Mapper<LongWritable, Text, AvroKey<CharSequence>, AvroValue<Integer>> {
private Text word = new Text();
private static final AvroValue<Integer> one = new AvroValue<Integer>(1);

public void map(LongWritable key, Text value,
OutputCollector<AvroKey<CharSequence>, AvroValue<Integer>> output,
Reporter reporter) throws IOException {
String line = value.toString();
StringTokenizer tokenizer = new StringTokenizer(line);
while (tokenizer.hasMoreTokens()) {
word.set(tokenizer.nextToken());
output.collect(new AvroKey<CharSequence>(word.toString()), one);
}
}
}

[b]Reducer[/b]

public class WordCountReducer extends
AvroReducer<CharSequence, Integer, Pair<CharSequence, Integer>> {

@Override
public void reduce(CharSequence key, Iterable<Integer> values,
AvroCollector<Pair<CharSequence, Integer>> collector,
Reporter reporter) throws IOException {
int sum = 0;
for (Integer cnt : values) {
sum += cnt;
}
collector.collect(new Pair<CharSequence, Integer>(key.toString(), sum));
}

}

[b]Driver[/b]

public class AvroWordCount extends Configured implements Tool {

public static void main(String[] args) throws Exception {
int exitCode = ToolRunner.run(new AvroWordCount(), args);
System.exit(exitCode);
}

@Override
public int run(String[] args) throws Exception {
if(args.length != 2){
System.out.printf("Usage %s [generic options] <in> <out>\n", getClass().getName());
ToolRunner.printGenericCommandUsage(System.out);
return -1;
}
JobConf conf = new JobConf(AvroWordCount.class);
conf.setJobName("wordcount");
conf.set("fs.default.name", "hdfs://node04vm01:9000");

AvroJob.setOutputSchema(conf, Pair.getPairSchema(Schema.create(Type.STRING),
Schema.create(Type.INT)));
conf.setMapperClass(WordCountMapper.class);
AvroJob.setReducerClass(conf, WordCountReducer.class);
conf.setInputFormat(TextInputFormat.class);

FileInputFormat.setInputPaths(conf, new Path(args[0]));
FileOutputFormat.setOutputPath(conf, new Path(args[1]));

JobClient.runJob(conf);
return 0;
}
}


java -jar avro/avro-tools-1.7.5.jar tojson part-00000.avro
{"key":"But","value":3}
{"key":"By","value":2}
{"key":"Dashwood","value":12}
...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值