FileSplit cannot be cast Exception

hadoop 版本: cdh3
本地模式运行报以下异常,cluster模式下正常
org.apache.hadoop.mapreduce.lib.input.FileSplit cannot be cast to org.apache.hadoop.mapred.FileSplit 

原因:
本地模式时:配置项mapred.mapper.new-api 为 false,cluster模式时为true
当值为false时,hadoop会以OLD模式运行,haodop就会试图把新的FilSplit 转为老的FileSplit, 就会发生以上异常

解决办法:
在mapred-site.xml增加以下配置:
    <property>                                                                                                                                                                   
        <name>mapred.mapper.new-api</name>                                                                                                                                       
        <value>true</value>                                                                                                                                                      
    </property

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好,这是一个关于Java编写MapReduce代码的问题。以下是代码示例: public class MyMapReduce extends Configured implements Tool { public static void main(String[] args) throws Exception { int res = ToolRunner.run(new Configuration(), new MyMapReduce(), args); System.exit(res); } public int run(String[] args) throws Exception { Job job = Job.getInstance(getConf(), "MyMapReduce"); job.setJarByClass(this.getClass()); // 设置输入和输出路径 FileInputFormat.setInputPaths(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); // 设置Mapper和Reducer类 job.setMapperClass(MyMapper.class); job.setReducerClass(MyReducer.class); // 设置输出键和值的类型 job.setOutputKeyClass(Text.class); job.setOutputValueClass(Text.class); // 设置分区类 job.setPartitionerClass(MyPartitioner.class); // 设置Reduce任务数 job.setNumReduceTasks(3); return job.waitForCompletion(true) ? 0 : 1; } public static class MyMapper extends Mapper<LongWritable, Text, Text, Text> { private Text outputKey = new Text(); private Text outputValue = new Text(); public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { // 读取文件名 FileSplit fileSplit = (FileSplit) context.getInputSplit(); String fileName = fileSplit.getPath().getName(); // 处理文件内容 // ... // 输出键值对 outputKey.set(fileName); outputValue.set("处理结果"); context.write(outputKey, outputValue); } } public static class MyReducer extends Reducer<Text, Text, Text, Text> { public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException { // 处理相同文件名的多个结果 // ... // 输出键值对 context.write(key, new Text("最终结果")); } } public static class MyPartitioner extends Partitioner<Text, Text> { public int getPartition(Text key, Text value, int numPartitions) { // 按照日期进行分区 // ... return partition; } } } 以上是一个简单的MapReduce代码示例,可以对HDFS上的一个文件夹下的文件进行读取处理,并将处理结果按照日期进行分区存放。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值