如何解决ClassCastException无法将Writable转换为Text
我创建了一个名为TextArrayWritable的子类来保存文本数组。看起来像这样:
import org.apache.hadoop.io.ArrayWritable;
import org.apache.hadoop.io.Text;
public class TextArrayWritable extends ArrayWritable{
public TextArrayWritable() {
super(Text.class);
}
public TextArrayWritable(Text[] values) {
super(Text.class,values);
}
}
在我的映射器中,我生成一个键值对,其中键是Text,值是上面的TextArrayWritable。映射器的代码如下所示:
import java.io.IOException;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
public class TagWordMapper extends Mapper<LongWritable,Text,TextArrayWritable>{
@Override
public void map(LongWritable key,Text value,Context context) throws IOException,InterruptedException{
if(key.get()==0) {
return;
}else {
String line = value.toString();
Text title = new Text(line.split("\t")[2]);
Text likes = new Text(line.split("\t")[8]);
Text tags = new Text(line.split("\t")[6]);
Text[] temp = new Text[2];
temp[0]=likes;
temp[1]=tags;
context.write(title,new TextArrayWritable(temp));
}
}
}
问题发生在化简器中,当我循环遍历TextArrayWritable时,即使我将其强制转换为Text [],也发生了异常,因为TextArrayWritable#get()返回了Text数组。减速器代码如下:
import java.io.IOException;
import java.util.regex.*;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.mapreduce.Reducer;
public class TagWordCombiner extends Reducer<Text,TextArrayWritable,IntWritable>{
@Override
public void reduce(Text title,Iterable<TextArrayWritable> arrays,InterruptedException{
int count=0;
Pattern r = Pattern.compile("\\bcute\\b",Pattern.CASE_INSENSITIVE);
for(TextArrayWritable array:arrays) {
Text[] temp = (Text[]) array.get();
int likes = Integer.parseInt(temp[0].toString());
String tags = temp[1].toString();
Matcher matcher = r.matcher(tags);
boolean found = matcher.find();
if(found && likes>=3000){
count+=1;
}
}
context.write(title,new IntWritable(count));
}
}
即使我将返回类型转换为Text [],也知道为什么会发生这种情况。任何见解均表示赞赏。谢谢您的阅读。
解决方法
我决定不使用TextArrayWritable,而是选择使用Text以字符串形式发送数据数组。此外,我发现它可以使组合器正常工作。组合器的输入/输出数据类型必须与映射器的输出数据类型匹配。
这是我的代码:
映射器:
package combiner;
import java.io.IOException;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
public class CombinerMapper extends Mapper<LongWritable,Text,Text>{
@Override
public void map(LongWritable key,Text value,Context context) throws IOException,InterruptedException{
if(key.get()==0) {
return;
}else {
String line = value.toString();
Text title = new Text(line.split("\t")[2]);
Text arr = new Text(line.split("\t")[8]+"\t"+line.split("\t")[6]);
context.write(title,arr);
}
}
}
它的作用是映射一个.csv
文件。它跳过第一行,因为第一行是标题并且没有用。在下一行中,它被“ \ t”分隔以按顺序提取“标题”,“喜欢”,“标签”。仅将“喜欢”和“标签”插入到数组中。 Mapper的输出数据类型为Text,Text。
组合器:
package combiner;
import java.util.regex.*;
import java.io.IOException;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
public class CombinerCombiner extends Reducer<Text,Text>{
@Override
public void reduce(Text title,Iterable<Text> values,InterruptedException{
int sum=0;
for(Text val:values) {
String line = val.toString();
int likes = Integer.parseInt(line.split("\t")[0]);
String tags = line.split("\t")[1];
Pattern r = Pattern.compile("\\bcute\\b",Pattern.CASE_INSENSITIVE);
Matcher matcher = r.matcher(tags);
boolean found = matcher.find();
if(found && likes>=3000) {
sum+=1;
}
}
if(sum==0) {
return;
}else {
context.write(title,new Text(Integer.toString(sum)));
}
}
}
组合器如何工作的棘手之处在于其输入/输出必须与Mapper的输出数据类型匹配,否则它将抛出我遇到的错误。在这种情况下,我将组合器用作过滤器,以过滤出没有> = 3000个“赞”且标题中的标签不包含任何形式的“可爱”一词的标题。它输入为
最后,减速器:
package combiner;
import java.io.IOException;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.mapreduce.Reducer;
public class CombinerReducer extends Reducer<Text,IntWritable>{
public void reduce(Text title,Iterable<Text>counts,InterruptedException{
int sum = 0;
for(Text count:counts) {
String line = count.toString();
int temp = Integer.parseInt(line);
sum+=temp;
}
context.write(title,new IntWritable(sum));
}
它用作常规的Reducer,将数据数组求和并产生最终和。循环遍历Iterable是没有意义的,因为它是在组合器中为我们完成的,但是它可以工作。
这是我的驱动程序类:
package combiner;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.Job;
public class Combiner {
public static void main(String[] args) throws Exception{
//Check if proper arguments is inputed
if (args.length != 2) {
System.out.printf(
"Usage: TagWord <input dir> <output dir>\n");
System.exit(-1);
}
Job job = new Job();
job.setJarByClass(Combiner.class);
job.setJobName("Combiner");
//Declare the input file path
FileInputFormat.setInputPaths(job,new Path(args[0]));
//Declare the output file path
FileOutputFormat.setOutputPath(job,new Path(args[1]));
//Initiate the the -cers
job.setMapperClass(CombinerMapper.class);
job.setCombinerClass(CombinerCombiner.class);
job.setReducerClass(CombinerReducer.class);
//map output
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
//reducer output
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
//Initiate boolean to check whether MapReduce is successful
boolean success = job.waitForCompletion(true);
//Execute code based on whether the job is successful
System.exit(success ? 0 : 1);
}
}
请注意在映射器和化简器类之间的组合器类。另外,请确保包括setMapOutputKeyClass和setMapOutputValueClass。这允许Mapper类使用不同的输出数据类型。它还会强制化简器输出数据类型为您为setOutputKeyClass和setOutputValueClass设置的任何数据,在我们的情况下,我们的化简器必须输出Text和IntWritable。
如有任何疑问,请发表评论。 Hadoop的信息很少。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。