今天在做hadoop小实验(类似于倒排索引),
在本地运行时本地job“Running job: job_local_0001” 的得到的数据格式为为:
Hello file3.txt:1;
MapReduce file3.txt:2;file1.txt:1;file2.txt:1;
bye file3.txt:1;
is file1.txt:1;file2.txt:2;
powerful file2.txt:1;
simple file2.txt:1;file1.txt:1;
而 提交到集群上运行“Running job: job_201405091426_0019”得到数据格式为空值。
输入文件内容为:
file1.txt
MapReduce is simple
file2.txt
MapReduce is powerful is simple
file3.txt
Hello MapReduce bye MapReduce
这三个文件。
搞了半天不知道什么问题。记录下来 以后解决。
程序源码如下:
package org.edu.bupt.xiaoye.hadooptest;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class MyInverseIndex {
public static final String INPUT_PATH = "hdfs://10.103.240.160:9000/usr/hadoop/MyInverseIndex_in";
public static final String OUTPUT_PATH = "hdfs://10.103.240.160:9000/usr/hadoop/MyInverseIndex_out";
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
final FileSystem fileSystem = FileSystem.get(new URI(INPUT_PATH), conf);
final Path outPath = new Path(OUTPUT_PATH);
if (fileSystem.exists(outPath)) {
fileSystem.delete(outPath, true);
}
conf.set("hadoop.job.user","hadoop");
conf.set("mapred.job.tracker", "10.103.240.160:9001");
final Job job = new Job(conf, MyInverseIndex.class.getSimpleName());
FileInputFormat.setInputPaths(job, INPUT_PATH);
FileOutputFormat.setOutputPath(job, outPath);
job.setMapperClass(MyMapper.class);
job.setReducerClass(MyReduce.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
job.setNumReduceTasks(1);//设置个数为1
job.waitForCompletion(true);
}
/**
* 只适用于文档中只出现一行 (可以一次读取整个文档)
*
* @author hadoop
*
*/
public static class MyMapper extends Mapper<LongWritable, Text, Text, Text> {
Map<String, Integer> map = new HashMap();
@Override
protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
String line = value.toString();
String[] words = line.split(" ");
for (String s : words) {
if (map.containsKey(s)) {
map.put(s, map.get(s) + 1);
} else {
map.put(s, 1);
}
}
Set<String> keys = map.keySet();
for (Iterator it = keys.iterator(); it.hasNext();) {
String s = (String) it.next();
context.write(new Text(s),
new Text(((FileSplit) context.getInputSplit()).getPath().getName().toString() + ":" + map.get(s)));
}
map.clear();
}
}
public static class MyReduce extends Reducer<Text, Text, Text, Text> {
@Override
protected void reduce(Text key, Iterable<Text> values, Context context)
throws IOException, InterruptedException
{
StringBuffer files = new StringBuffer();
for(Text fileName : values){
files.append(fileName+";");
}
context.write(key , new Text(files.toString()));
}
}
}
今天在检查的时候,重写了一遍,把MyReduce改成这样就好了,奇怪。
public static class MyReduce extends Reducer<Text, Text, Text, Text> {
private Text result = new Text();
// 实现reduce函数
public void reduce(Text key, Iterable<Text> values, Context context)
throws IOException, InterruptedException {
StringBuffer fileList = new StringBuffer();
for(Text value : values){
fileList.append(value.toString()+";");
}
result.set(fileList.toString());
context.write(key, result);
}
}
分享到:
相关推荐
Windows下Eclispe远程开发Mapreduce程序
利用采样器实现mapreduce任务输出全排序大数据-MapReduce
MapReduceExample 下建立新包 com.xijing.mapreduce,模仿内置的 WordCount 示例,自己编写一个 WordCount 程序,最后打包成 JAR 形式并在 Hadoop 集群上运行该 MR-App,查看运行结果。 4 分别在自编 MapReduce 程序...
这是 MapReduce 的多路径输入输出示例代码。有关大数据的相关文章可以阅读我的专栏:《大数据之Hadoop》 http://blog.csdn.net/column/details/bumblebee-hadoop.html
mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce ...
资源包含文件:课程论文报告word和PDF两...输出每个测试文档的分类结果; 3:利用测试文档的真实类别,计算分类模型的Precision,Recall和F1值。 详细介绍参考:https://blog.csdn.net/newlw/article/details/124984567
在hadoop平台上,用mapreduce编程实现大数据的词频统计
MapReduce详解Shuffle过程
【MapReduce篇07】MapReduce之数据清洗ETL1
(2)打开网站localhost:8088和localhost:50070,查看MapReduce任务启动情况 (3)写wordcount代码并把代码生成jar包 (4)运行命令 (1):把linus下的文件放到hdfs上 (2):运行MapReduce (5):查看运行结果 ...
02.mapreduce数据输入输出类型的序列化问题--明确jobsubmitter.mp4
MapReduce全部很详细,文档上面有截图,安装文档可以独立完成学习,及其案例,恰同学少年,风华正茂,挥斥方遒
MapReduce 初级编程实践 姓名: 实验环境: 操作系统:Linux(建议Ubuntu16.04); Hadoop版本:3.2.2; 实验内容与完成情况: (一)编程实现文件合并和去重操作 对于两个输入文件,即文件 A 和文件 B,请...
简单的在MapReduce中实现两个表的join连接简单的在MapReduce中实现两个表的join连接简单的在MapReduce中实现两个表的join连接
在Mapreduce框架下一个计算机群通过统一的任务调度将一个巨型任务分成许多部分 ,分别解决然后合并得到最终结果。Mapreduce可以让程序员以简单的程序来解决实际问 题,而隐藏了诸如分布、工作调度、容错、机器间通信...
IDEA SpringBoot集成hadoop运行环境,本地启动项目,GET请求接口触发远程提交MapReduce任务至生产集群报错: Error: java.lang.ClassNotFoundException: org.wltea.analyzer.core.IKSegmenter at java.net....
MapReduce发明人关于MapReduce的介绍
MapReduce Job集群提交过程源码跟踪及分析
大规模数据处理时,MapReduce在三个层面上的基本构思 如何对付大数据处理:分而治之 对相互间不具有计算依赖关系的大数据,实现并行最自然的办法就是采取分而治之的策略 上升到抽象模型:Mapper与Reducer MPI等...
图解MapReduce,系统介绍Hadoop MapReduce工作过程原理