MapReduce
package whut.onetomany;
import java.io.IOException;
import java.util.Iterator;
import org.apache.Hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
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.Mapper.Context;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.input.MultipleInputs;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
public class JoinMain extends Configured implements Tool {
public static class JoinMapper extends Mapper<LongWritable, Text, TextIntPair, Text>
{
private TextIntPair tp=new TextIntPair();
private Text val=new Text();
@Override
protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
// TODO Auto-generated method stub
//获取要处理的文件的名称
FileSplit file=(FileSplit)context.getInputSplit();
String fileName=file.getPath().toString();
//获取输入行分隔
String line=value.toString();
String[] lineKeyValue=line.split("\"");
String lineKey=lineKeyValue[0];
String lineValue=lineKeyValue[1];
tp.setFirstKey(lineKey);
//判断是否是trade文件
if(fileName.indexOf("trade")>=0)
{
tp.setSecondKey(0);
val.set(lineValue);
}
//判断是否是pay文件
else if(fileName.indexOf("pay")>=0)
{
tp.setSecondKey(1);
val.set(lineValue);
}
context.write(tp, val);
}
}
public static class JoinReducer extends Reducer<TextIntPair, Text, Text, Text>
{
@Override
protected void reduce(TextIntPair key, Iterable<Text> values,
Context context)throws IOException, InterruptedException {
Iterator<Text> valList=values.iterator();
//注意这里一定要写成string不可变,写成Text有问题
//Text trade=valList.next();
String tradeName=valList.next().toString();
while(valList.hasNext())
{
Text pay=valList.next();
context.write(new Text(tradeName), pay);
}
}
}
@Override
public int run(String[] args) throws Exception
{
Configuration conf=getConf();
Job job=new Job(conf,"JoinJob");
job.setJarByClass(JoinMain.class);
//ToolRunner已经利用GenericOptionsParser解析了命令行中的参数
//并且将其存放在数组中,传递给该run()方法了
FileInputFormat.addInputPath(job, new Path(args[0]));
FileInputFormat.addInputPath(job, new Path(args[1]));
//输入文件必须以,隔开
//FileInputFormat.addInputPaths(job, args[0]);
FileOutputFormat.setOutputPath(job, new Path(args[2]));
job.setMapperClass(JoinMapper.class);
job.setReducerClass(JoinReducer.class);
//设置分区方法
job.setPartitionerClass(PartitionByText.class);
//设置分组排序
job.setGroupingComparatorClass(TextComparator.class);
job.setSortComparatorClass(TextIntComparator.class);
job.setMapOutputKeyClass(TextIntPair.class);
job.setMapOutputValueClass(Text.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
job.waitForCompletion(true);
int exitCode=job.isSuccessful()?0:1;
return exitCode;
}
public static void main(String[] args)throws Exception
{
// TODO Auto-generated method stub
int code=ToolRunner.run(new JoinMain(), args);
System.exit(code);
}
}
注意:
一般有些地方没有定义组内排序策略,但是经过多次测试,发现无法保证第一个表在第二个表之前到达,则这里就自定义了组内排序策略。版本号为Hadoop1.1.2
更多Hadoop相关信息见Hadoop 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=13
