有一个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的更多相关文章

  1. MapReduce异常:java.lang.ClassCastException: interface javax.xml.soap.Text

    MapReduce异常:java.lang.ClassCastException: interface javax.xml.soap.Text java.lang.ClassCastException ...

  2. 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的默认 ...

  3. org.apache.hadoop.io.LongWritable cannot be cast to org.apache.hadoop.io.Text

    代码缺少这一行:job.setInputFormatClass(KeyValueTextInputFormat.class);

  4. 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 ...

  5. 【Json】关于json解析时异常org.json.JSONException: A JSONObject text must begin with '{' at character 1 of {的解决方法

    遇到这种异常有几种情况: 1.JSON格式有问题,检查一下格式. 2.格式没问题,仍然报错,这个是因为你的json文件头里带有编码字符(如UTF-8等),读取字符串时json串是正常的,但是解析就有异 ...

  6. 在hadoop上进行编写mapreduce程序,统计关键词在text出现次数

    mapreduce的处理过程分为2个阶段,map阶段,和reduce阶段.在要求统计指定文件里的全部单词的出现次数时. map阶段把每一个关键词写到一行上以逗号进行分隔.并初始化数量为1(同样的单词h ...

  7. 编写第一个MapReduce程序—— 统计气温

    摘要:hadoop安装完成后,像学习其他语言一样,要开始写一个“hello world!” ,看了一些学习资料,模仿写了个程序.对于一个C#程序员来说,写个java程序,并调用hadoop的包,并跑在 ...

  8. Hadoop学习(4)-- MapReduce

    MapReduce是一种用于大规模数据集的并行计算编程模型,由Google提出,主要用于搜索领域,解决海量数据的计算问题.其主要思想Map(映射)和Reduce(规约)都是从函数是编程语言中借鉴而来的 ...

  9. Mapreduce执行过程分析(基于Hadoop2.4)——(二)

    4.3 Map类 创建Map类和map函数,map函数是org.apache.hadoop.mapreduce.Mapper类中的定义的,当处理每一个键值对的时候,都要调用一次map方法,用户需要覆写 ...

随机推荐

  1. PL/SQL(二):变量

    变量 标识符定义 PL/SQL程序设计中的标识符定义与SQL的标识符定义的要求相同.要求和限制有: 个字符. )首字符必须为字母. )不区分大小写. )不能使用SQL保留字. )对标识符的命名最好遵循 ...

  2. qt学习笔记(七)之数据库简介(所有支持数据库类型的列表)

    笔者最近用Qt写公司的考勤机.本来要求是要基于frameBuffer下用自己开发的easyGUI来进行上层应用开发,但是考虑到easyGUI提供的接口不是很多,就考虑用Qt来开发,顺带练练手. 废话不 ...

  3. QT在构造函数中退出程序

    原地址:http://www.tuicool.com/articles/RZnYze 在QT的界面类的构造过程中,如果想退出整个程序,暴力的做法是调用exit(-1)进行,另外一种不是那么暴力的方式如 ...

  4. 静态代码检查工具 cppcheck 的使用(可分别集成到VS和QT Creator里)

    CppCheck是一个C/C++代码缺陷静态检查工具.不同于C/C++编译器及其它分析工具,CppCheck只检查编译器检查不出来的bug,不检查语法错误.所谓静态代码检查就是使用一个工具检查我们写的 ...

  5. 基于visual Studio2013解决面试题之0604O(1)时间复杂度删除链表节点

     题目

  6. POJ 2594 Treasure Exploration(最小路径覆盖变形)

    POJ 2594 Treasure Exploration 题目链接 题意:有向无环图,求最少多少条路径能够覆盖整个图,点能够反复走 思路:和普通的最小路径覆盖不同的是,点能够反复走,那么事实上仅仅要 ...

  7. Tui-x简单介绍

    1.什么是Tui-x Tui-x是一个创建cocos2d-x UI界面的解决方式,而builder用的则是FlashCS,通过使用jsfl来拓展FlashCS从而达到UI编辑器的功能.这个jsfl所做 ...

  8. 自拉ADSL网线搭建站点server,解决动态IP、无公网IP、80port被封、HTTP被屏蔽的方法

    自己的server搭建站点应用,提供站点服务,能够不用备案,但可能会面对网络限制问题,如动态公网IP.无公网IP.80port被封.HTTP被屏蔽,这些复杂网络情况. 依据本地网络环境,能够针对不同的 ...

  9. Unity 3D学习笔记(三)——关于脚本

    在脚本中定义的类成员,如果是公共的话,在脚本与游戏对象绑定之后,是可以在Inspector中编辑的,例如下面这个脚本: using UnityEngine; using System.Collecti ...

  10. 正确理解Python文件读写模式字w+、a+和r+

    w+ 和 r+的差别不难理解.还有a+ +同一时候读写,就可以读又可写,边写边读.边读边写,不用flush,用seek 和 tell可測得. fp = open("a.txt", ...