MapReduce 异常 LongWritable cannot be cast to Text
有一个txt文件,内容格公式是这样的:
深圳订做T恤 5729944
深圳厂家t恤批发 5729945
深圳定做文化衫 5729944
文化衫厂家 5729944
订做文化衫 5729944
深圳t恤厂家 5729945
前面是搜索关键词,后面的是所属的分类ID,以tab分隔,想统计分类情况。于是用以下的MapReduce程序跑了下:
import java.io.IOException;
import java.util.*; import org.apache.hadoop.fs.Path;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.mapreduce.lib.input.*;
import org.apache.hadoop.mapreduce.lib.output.*;
import org.apache.hadoop.util.*; public class ClassCount extends Configured implements Tool
{
public static class ClassMap
extends Mapper<Text ,Text,Text,IntWritable>
{
private static final IntWritable one = new IntWritable(1);
private Text word = new Text(); public void map(Text key,Text value,Context context)
throws IOException,InterruptedException
{
String eachLine = value.toString();
StringTokenizer tokenizer = new StringTokenizer(eachLine,"\n");
while(tokenizer.hasMoreTokens())
{
StringTokenizer token = new StringTokenizer(tokenizer.nextToken(),"\t");
String keyword = token.nextToken();//i don't use it now.
String classId = token.nextToken();
word.set(classId);
context.write(word,one);
}
}
} public static class Reduce
extends Reducer<Text,IntWritable,Text,IntWritable>
{
public void reduce(Text key,Iterable<IntWritable> values,Context context)
throws IOException,InterruptedException
{
int sum = 0;
for(IntWritable val : values)
sum += val.get();
context.write(key,new IntWritable(sum));
}
}
public int run(String args[]) throws Exception{
Job job = new Job(getConf());
job.setJarByClass(ClassCount.class);
job.setJobName("classCount"); job.setMapperClass(ClassMap.class);
job.setReducerClass(Reduce.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);
return success ? 0 : 1;
}
public static void main(String[] args) throws Exception
{
int ret = ToolRunner.run(new ClassCount(),args);
System.exit(ret);
}
}
抛出例如以下异常:
java.lang.ClassCastException: org.apache.hadoop.io.LongWritable cannot be cast to org.apache.hadoop.io.Text
我以为输入的键是文本就用Text来作为key,但貌似不是这样子的,map方法把文件的行号当成key,所以要用LongWritable。
可是改过来之后,报了以下的异常:
14/04/25 17:21:15 INFO mapred.JobClient: Task Id : attempt_201404211802_0040_m_000000_1, Status : FAILED
java.io.IOException: Type mismatch in value from map: expected org.apache.hadoop.io.Text, recieved org.apache.hadoop.io.IntWritable
这个就更加直观了,须要在run方法中加入以下的两行以明白声明输入的格式。
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
版权声明:本文博客原创文章。博客,未经同意,不得转载。
MapReduce 异常 LongWritable cannot be cast to Text的更多相关文章
- MapReduce异常:java.lang.ClassCastException: interface javax.xml.soap.Text
MapReduce异常:java.lang.ClassCastException: interface javax.xml.soap.Text java.lang.ClassCastException ...
- Hadoop: LongWritable cannot be cast to org.apache.hadoop.io.IntWritable
写MR Job的时候遇到一个坑爹的异常: LongWritable cannot be cast to org.apache.hadoop.io.IntWritable 当写Map的时候,key的默认 ...
- org.apache.hadoop.io.LongWritable cannot be cast to org.apache.hadoop.io.Text
代码缺少这一行:job.setInputFormatClass(KeyValueTextInputFormat.class);
- E QUERY [main] SyntaxError: identifier starts immediately after numeric literal mongodb mapReduce 异常分析 集合命名规范
异常信息 repl_test:PRIMARY> db.0917order_totals_b.find()2018-09-28T15:13:03.992+0800 E QUERY [main] S ...
- 【Json】关于json解析时异常org.json.JSONException: A JSONObject text must begin with '{' at character 1 of {的解决方法
遇到这种异常有几种情况: 1.JSON格式有问题,检查一下格式. 2.格式没问题,仍然报错,这个是因为你的json文件头里带有编码字符(如UTF-8等),读取字符串时json串是正常的,但是解析就有异 ...
- 在hadoop上进行编写mapreduce程序,统计关键词在text出现次数
mapreduce的处理过程分为2个阶段,map阶段,和reduce阶段.在要求统计指定文件里的全部单词的出现次数时. map阶段把每一个关键词写到一行上以逗号进行分隔.并初始化数量为1(同样的单词h ...
- 编写第一个MapReduce程序—— 统计气温
摘要:hadoop安装完成后,像学习其他语言一样,要开始写一个“hello world!” ,看了一些学习资料,模仿写了个程序.对于一个C#程序员来说,写个java程序,并调用hadoop的包,并跑在 ...
- Hadoop学习(4)-- MapReduce
MapReduce是一种用于大规模数据集的并行计算编程模型,由Google提出,主要用于搜索领域,解决海量数据的计算问题.其主要思想Map(映射)和Reduce(规约)都是从函数是编程语言中借鉴而来的 ...
- Mapreduce执行过程分析(基于Hadoop2.4)——(二)
4.3 Map类 创建Map类和map函数,map函数是org.apache.hadoop.mapreduce.Mapper类中的定义的,当处理每一个键值对的时候,都要调用一次map方法,用户需要覆写 ...
随机推荐
- Qt QDebug等重定向到文件中
源地址:http://blog.163.com/soyo_gogogo/blog/static/171414077201392705639321/ Qt的qDebug() 等函数,可以打印出十分细致的 ...
- C++ template error: undefined reference to XXX
一般来说,写C++程序时推荐“类的声明和实现分离”,也就是说一个类的声明放在example.h文件中,而这个类的实现放在example.cpp文件中,这样方便管理,条理清晰. 但是如果类的声明用到了模 ...
- POJ 2762推断单个联通(支撑点甚至通缩+拓扑排序)
Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14789 ...
- RMAN 备份
backup database; --备份整库 backup database format '\xxxxxx\xxx_%U'; --备份整库到指定路劲 backup tablespace users ...
- 由于“Table(T_Test)”没有主键,因此无法在其上执行 Create、Update 或 Delete 操作
在使用Linq To Sql查询的时候,遇到这么个问题,如图所示: 出现这个问题的原因就像途中所说的——没有主键(现在终于初步知道“为什么别人常说数据库中的逻辑主键是为了在编程中方便使用”的原因了,估 ...
- javascript事件委托,事件代理,元素绑定多个事件之练习篇
<ul id="parent-list"> <li id="post-1">item1</li> <li id=&qu ...
- 【IACV】边缘检测技术传统的方法与理论
1.边缘检测的目的 边缘检测是图像分析中使用到的最常见的操作之一,而且相比其他任何主题来说,文献中提到的与边缘增强(edge enhancement)[1]与边缘检测(edge detection)[ ...
- android 请求网络 和 httpclient的使用上传下载
访问网络最主要的也就是 http协议了. http协议很简单,但是很重要. 直接上代码了,里面都是1个代码块 代码块的,用哪一部分直接拷出去用就好了. 1.访问网络用 get 和 post 自己组拼 ...
- 网络数据(socket)传输总结
环境限定:TCP/IP下的socket网络传输:C/C++开发语言,32/64位机. 目前有两种方式对数据进行传输:1)字符流形式,即将数据用字符串表示:2)结构型方式,即将数据按类型直接传输. 1) ...
- python中的中文编码
我现在编写python代码,有一些内容需要用中文编写,例如注释,一些其它的东西 默认python是不支持中文的,包括两个方面不支持,一是文件编码默认是ansi的,二是虚拟机运行解析脚本时也是非utf的 ...