java实现map和reduce,java实现map-reduce代码

本文记录用java实现map-reduce,并在服务器上运行的代码。

package com.demo.hadoop.test;

import java.io.IOException;

import java.util.StringTokenizer;

import java.util.Arrays;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.conf.Configured;

import org.apache.hadoop.fs.FileSystem;

import org.apache.hadoop.fs.FileUtil;

import org.apache.hadoop.fs.Path;

import org.apache.hadoop.io.IntWritable;

import org.apache.hadoop.io.LongWritable;

import org.apache.hadoop.io.Text;

import org.apache.hadoop.mapreduce.Job;

import org.apache.hadoop.mapreduce.Mapper;

import org.apache.hadoop.mapreduce.Reducer;

import org.apache.hadoop.mapreduce.Mapper.Context;

import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;

import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;

import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

import org.apache.hadoop.mapreduce.v2.app.job.TaskAttempt;

import org.apache.hadoop.util.GenericOptionsParser;

import org.apache.hadoop.util.Tool;

import org.apache.hadoop.util.ToolRunner;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import com.demo.hadoop.test.AvgScore.MyMap;

import com.demo.hadoop.test.AvgScore.MyReduce;

import com.sun.media.sound.SoftLowFrequencyOscillator;

public class NbRoute implements Tool {

public static final Logger log = LoggerFactory.getLogger(NbRoute.class);

Configuration configuration;

public static class MyMap extends Mapper {

public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

String[] citation = value.toString().split(",");

context.write(new Text(citation[0]), new Text(citation[1]));

}

}

public static class MyReduce extends Reducer {

public void reduce(Text key, Iterable values, Context context) throws IOException, InterruptedException {

String csv = "";

for (Text val : values) {

if (csv.length() > 0) {

csv += ",";

}

if (!csv.contains(val.toString()))

csv += val.toString();

}

String[] tmp = csv.split(",");

Arrays.sort(tmp);

String scenic = "";

if (tmp.length > 2) {

for (int i = 0; i < tmp.length; i++){

scenic += tmp[i];

scenic += ",";

}

String sctmp = scenic.substring(0,scenic.length()-1);

context.write(key, new Text(sctmp));

}

}

}

@Override

public int run(String[] args) throws Exception {

// TODO Auto-generated method stub

Configuration conf = new Configuration();

Job job = new Job(conf, "NbRoute");

job.setJarByClass(NbRoute.class);

job.setJobName("NbRoute");

job.setOutputKeyClass(Text.class);

job.setOutputValueClass(Text.class);

job.setMapperClass(MyMap.class);

job.setCombinerClass(MyReduce.class);

job.setReducerClass(MyReduce.class);

job.setInputFormatClass(TextInputFormat.class);

job.setOutputFormatClass(TextOutputFormat.class);

FileInputFormat.setInputPaths(job, new Path(args[0]));

FileOutputFormat.setOutputPath(job, new Path(args[1]));

boolean success = job.waitForCompletion(true);

System.exit(job.waitForCompletion(true) ? 0 : 1);

return success ? 0 : 1;

}

@Override

public Configuration getConf() {

return configuration;

}

@Override

public void setConf(Configuration conf) {

conf = new Configuration();

configuration = conf;

}

public static void main(String[] args) throws Exception {

int ret = ToolRunner.run(new NbRoute(), args);

System.exit(ret);

}

} 从eclipse中导成jar包放到服务器上运行,代码运行命令:hadoop jar avgscore.jar com.demo.hadoop.test.NbRoute /APPLICATION/YIWUXSP/oidd/test/201703 /APPLICATION/YIWUXSP/oidd/test/out1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值