知识点
相关文章
更多最近更新
更多Hadoop示例程序之单词统计MapReduce
2019-03-28 13:19|来源: 网络
在eclipse下新建一个map/reduce Project
1,新建文件MyMap.java
import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.Hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
public class MyMap extends Mapper<Object, Text, Text, IntWritable> {
private final static IntWritable one = new IntWritable(1);
private Text word;
public void map(Object key, Text value, Context context)
throws IOException, InterruptedException {
String line = value.toString();
StringTokenizer tokenizer = new StringTokenizer(line);
while (tokenizer.hasMoreTokens()) {
word = new Text();
word.set(tokenizer.nextToken());
context.write(word, one);
}
}
}
2,新建文件MyReduce.java:
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
public class MyReduce 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));
}
}
3,新建一个文件MyDriver.java
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
public class MyDriver {
public static void main(String[] args) throws Exception,InterruptedException {
Configuration conf=new Configuration();
Job job=new Job(conf,"Hello Hadoop World");
job.setJarByClass(MyDriver.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
job.setMapperClass(MyMap.class);
job.setCombinerClass(MyReduce.class);
job.setReducerClass(MyReduce.class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
FileInputFormat.setInputPaths(job, new Path("./input/555.txt"));
FileOutputFormat.setOutputPath(job, new Path("./input/out.txt"));
job.waitForCompletion(true);
}
}
好了,见证奇迹的时刻到了,先在工程目录下创建一个目录input,并在下面新建文件555.txt,
Hello World 555 hahaha
Hello World
保存,运行java应用程序
在input下多了个文件目录:out.txt,该目录下有个文件part-r-0000文件,
打开后文件的内容是:
555 1
Hello 2
World 2
hahaha 1
算是搞定了。。。
运行中碰到包错:
org.apache.hadoop.fs.ChecksumException: Checksum error:
这个好办 只要将工程文件下的CRC数据校验文件删除就可以了
相关问答
更多-
怎么在hadoop上运行mapreduce程序[2022-03-21]
一、 首先要知道此前提 转载 若在windows的Eclipse工程中直接启动mapreduc程序,需要先把hadoop集群的配置目录下的xml都拷贝到src目录下,让程序自动读取集群的地址后去进行分布式运行(您也可以自己写java代码去设置job的configuration属性)。 -
hadoop的MapReduce程序运行操作问题[2022-03-24]
都可以,简单的直接用txt打开java文件,写好后打包成class文件,就可以运行了。你看他原来在哪里放class文件的,你就放在那里 -
java程序统计单词词频[2023-12-20]
1.其实你这个功能用lucene加IK Analyer分词器很好解决。 把文档读入,然后用lucene创建索引,在索引里你的每个单词都会被分出来,词频就是命中次数totalHits,然后读取索引,放进map就行。这只是一个大体思路。 2.如果不用lucene,就用正则表达式匹配英文单词之间的空格,把空格去掉放进map的key,同时设置value=1。这样你的每个英文单词(有连词符号的如I'm根据你的需求另考虑)就都放进了map的key,且value都等于1。当然这些key值是有重复的,所以就要开始遍历比较k ... -
用于hadoop mapreduce的罐子(Jars for hadoop mapreduce)[2023-07-28]
本教程提到: 下载Hadoop-core-1.2.1.jar,用于编译和执行MapReduce程序。 访问以下链接http://mvnrepository.com/artifact/org.apache.hadoop/hadoop-core/1.2.1下载jar。 所以在这里你可以找到不同版本的所有罐子 This tutorial mentions : Download Hadoop-core-1.2.1.jar, which is used to compile and execute the MapRe ... -
在Hadoop中,您处理输入拆分而不是块。 输入拆分是完整的数据集。 您希望避免一个映射器超过两个拆分的情况,因为这会降低性能并创建流量。 在文本世界中,假设您在block1中并且您有一个句子,例如“我是一个哈”,而block2继续“doop developer”,那么这会创建网络流量,因为我们始终必须在一个完整的节点上工作输入拆分和一些数据必须转移到另一个节点。 In Hadoop you work on input splits and not on blocks. An input split is ...
-
您尝试使用的文件位于本地Linux文件系统上,而不是HDFS上。 所以使用可以使用简单的命令,如: cat /home/Kumar/DEV/Eclipse/eclipse/Workspace/MyFirstMapReduce/Files/input/file1 如果MapReduce程序需要HDFS上的输入文件,则将相同的文件放在作业中的HDFS和HDFS路径上。 您可以使用此命令将文件放在hdfs上: hadoop fs -put看起来这种行为是由于内存问题导致的。 当我清理系统上的一些空间并再次执行这些步骤时,它成功地运行了。 但是,我不是100%肯定它。 It seems like the behaviour was due to low memory issues. The moment i cleaned up some space on my system and did the steps again, it ran succesfully. However, i'm not 100% sure about it.我想这可以帮到你: http : //blogs.msdn.com/b/avkashchauhan/archive/2012/03/29/how-to-chain-multiple-mapreduce-jobs-in-hadoop.aspx 您还可以查看ChainMapper( http://hadoop.apache.org/docs/current/api/org/apache/hadoop/mapred/lib/ChainMapper.html ),具体取决于您要实现的目标。 但是,这仅适用于一个re ...
Hadoop Mapreduce Wordcount示例意外终止(Hadoop Mapreduce Wordcount example gets terminated unexpectedly)[2022-01-11]
尝试: Hadoop job -list 杀死所有工作并重新运行: Hadoop job –kill尝试检查作业跟踪器的日志是否有错误 http://localhost:50070/ – web UI of the NameNode daemon http://localhost:50030/ – web UI of the JobTracker daemon http://localhost:50060/ – web UI of the TaskTracker daemon The ... 映射器api https://hadoop.apache.org/docs/stable/api/org/apache/hadoop/mapreduce/Mapper.html 减少api https://hadoop.apache.org/docs/stable/api/org/apache/hadoop/mapreduce/Reducer.html 帮助您入门的教程 http://hadoop.apache.org/docs/r0.18.3/mapred_tutorial.html The mapper ...