MapReduce问题

sxdfiona 2016-03-28 12:24:11
简单描述一下我的问题,我想用MapReducec处理数据,Map函数写好能跑出来,但是reduce函数一直出错,由于Map函数比较复杂,这里只给出Map函数的输出,以及我的Reduce函数,希望各位大神们看到可能帮我解答一下~Hadoop初学者,身边没有非常懂的同学~~自己在家狂试,但是还是没解决~谢谢大家了~

Map输出:
key value
112008 张峰计算机信息工程 1
计算机 信息工程 1
181678 沈玉日语外国语 2
日语 外国语 2
112009 张峰计算机经济管理 3
计算机 经济管理 3

(其中value的值中间以空格分割的)

Reduce处理
将相同的Key值合并,这里只有两个计算机的key值相同,其他的都自成一个key,遍历同一个key的value,
如 计算机 信息工程 1
经济管理 3
按照空格分隔每一个valuelist,如将信息工程赋值给split[0]将1赋值给split[1],然后直接将其输出,接下来同样的,将经济管理赋值给split[0]将3赋值给split[1],然后直接将其输出。原始程序后面还有步骤但是后来发现这一部就不对了。。所以想请大家帮忙看看~

我的Reduce 部分如下:
public static class YCLReducer extends Reducer< Text, Text,Text,Text> {
//private LongWritable result = new LongWritable();
private Text a1 = new Text();
private Text b1 = new Text();
public void reduce(Text key, Iterable<Text> values,
Context context
) throws IOException, InterruptedException {


String a;
String b;

Iterator ite=values.iterator();

while(ite.hasNext())
{
String record=ite.next().toString();
String [] lineSplit = record.split(" ");


a1.set(lineSplit[0]);
b1.set(lineSplit[1]);

context.write(a1,b1);

}

/*
for(int m=0;m<ziduan.size();m++)
{
jlh1.set(jiluhao.get(m));
jlh2.set(jiluhao.get(m));
context.write(jlh1,jlh2);
}
*/
/*for(int n=m+1;n<ziduan.size();n++)
{
if(ziduan.get(m)!=ziduan.get(n))
{
jlh1.set(jiluhao.get(m));
jlh2.set(jiluhao.get(n));
context.write(jlh1,jlh2);
}

}
*/
}


}


详细代码如下:

数据:1 112008 zhangfeng jisuanji xinxigongcheng

import java.io.IOException;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.*;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import org.apache.hadoop.util.GenericOptionsParser;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;

public class YCLyilai extends Configured implements Tool
{
enum Counter
{
LINESKIP,//chu cuo de hang
}

public static class YCLMapper extends Mapper<LongWritable, Text, Text, Text>
{

// private final static LongWritable one = new LongWritable(1);
private Text zhuma = new Text();
private Text out = new Text();
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException
{
String linevalue = value.toString();
String [] relateSplit="1 2 3 4 p 3 4 p".split(" ");
StringTokenizer stringTokenizer = new StringTokenizer(linevalue);

try
{
String [] lineSplit = linevalue.split(" ");

String t=null;
String s=" ";


for(int m=0;m<relateSplit.length;m++)
{
if(!relateSplit[m].equals("p"))
{
s+=lineSplit[Integer.parseInt(relateSplit[m])]+" ";
}
else
{
String [] partSplit = s.split(" ");
zhuma.set(partSplit[1]);
String s1=" ";
for(int j=2;j<partSplit.length;j++)
{
s1+=partSplit[j];
}
out.set(s1+" "+lineSplit[0]);
context.write(zhuma,out);
s=" ";
}

}

}
catch(java.lang.ArrayIndexOutOfBoundsException e)
{
context.getCounter(Counter.LINESKIP).increment(1);
return;
}
}
}

public static class YCLReducer extends Reducer< Text, Text,Text,Text> {
//private LongWritable result = new LongWritable();
private Text a1 = new Text();
private Text b1 = new Text();
public void reduce(Text key, Iterable<Text> values,
Context context
) throws IOException, InterruptedException {


String a;
String b;

Iterator ite=values.iterator();

while(ite.hasNext())
{
String record=ite.next().toString();
String [] lineSplit = record.split(" ");


a1.set(lineSplit[0]);
b1.set(lineSplit[1]);

context.write(a1,b1);

}

/*
for(int m=0;m<ziduan.size();m++)
{
jlh1.set(jiluhao.get(m));
jlh2.set(jiluhao.get(m));
context.write(jlh1,jlh2);
}
*/
/*for(int n=m+1;n<ziduan.size();n++)
{
if(ziduan.get(m)!=ziduan.get(n))
{
jlh1.set(jiluhao.get(m));
jlh2.set(jiluhao.get(n));
context.write(jlh1,jlh2);
}

}
*/
}


}



public int run(String[] args)throws Exception
{
Configuration conf=getConf();
Job job=new Job(conf,"YCLyilai");
job.setJarByClass(YCLyilai.class);
FileInputFormat.addInputPath(job, new Path("hdfs://master:9000/DSYCLyilai_input2"));
FileOutputFormat.setOutputPath(job, new Path("hdfs://master:9000/DSYCLyilai_output3"));
// job.setJarByClass(YCLyilai.class);
job.setMapperClass(YCLMapper.class);
job.setCombinerClass(YCLReducer.class);
job.setReducerClass(YCLReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
job.waitForCompletion(true);
return job.isSuccessful()?0:1;
}

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

}

...全文
783 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

932

社区成员

发帖
与我相关
我的任务
社区描述
云计算 云存储相关讨论
社区管理员
  • 云存储
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧