有一个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. Windows下实战Apache+PHP [转]

        一.Apache 1.下载登陆Apache Lougne(http://www.apachelounge.com/download/),找到最新版本的Apache.笔者下载的是带IPv6和Cr ...

  2. 基于visual Studio2013解决C语言竞赛题之1076放鞭炮

        题目 解决代码及点评 /************************************************************************/ /* ...

  3. MFC画二维动态图表[GDI]

    源博客:http://www.codeproject.com/Articles/9350/2D-Animated-Charts 源代码:http://download.csdn.net/detail/ ...

  4. oracle如何设置show parameter显示隐含参数

    在sqlplus中shwo parameter是显示不了隐藏参数的,需要做一个处理,如下所示:    以SYS用户登录: C:\Documents and Settings\guogang>sq ...

  5. Linux chmod权限管理需要小心的地方

    档案的权限管理和简单,比如chmod 775 /tmp/test.sh 另外使用chmod +w /tmp/test.sh,会给档案的拥有者,群组,其他人的权限都加上了可编辑.这样就有安全隐患了.所以 ...

  6. SRM 583 Div II Level Three:GameOnABoard,Dijkstra最短路径算法

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12556 用Dijkstra实现,之前用Floyd算法写了一个, ...

  7. Asp.NET调用百度翻译

    Asp.NET调用百度翻译,图示: HTML: <%@ Page Language="C#" AutoEventWireup="true" CodeFil ...

  8. 用angularjs开发下一代web应用(二):angularjs应用骨架(二)

    1.浅谈非入侵式JavaScript <div ng-click="doSomething()">...</div>这些指令和原来的事件处理器有下面不同之处 ...

  9. Irvine的专业汇编网站

    http://asmirvine.com/ http://download.csdn.net/download/stupid_boy2007/3890853 http://download.csdn. ...

  10. ASP.NET - 分页

    效果: SQL-存储过程(Paging): ROW_NUMBER() over(order by MessageDateTime desc)  其中的 MessageDateTime desc 代表的 ...