能进行hello world之前假设你的环境已经搭建完毕(我搭建的伪分布式)
我用hadoop源码中的WordCount作为hadoop的hello world。
(1)我们拿到hadoop源码中的WordCount类代码如下
package org.apache.hadoop.examples;
import java.io.IOException;
import java.util.StringTokenizer;
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.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
public class WordCount {
public static class TokenizerMapper
extends Mapper<Object, Text, Text, IntWritable>{
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
public void map(Object key, Text value, Context context
) throws IOException, InterruptedException {
StringTokenizer itr = new StringTokenizer(value.toString());
while (itr.hasMoreTokens()) {
word.set(itr.nextToken());
context.write(word, one);
}
}
}
public static class IntSumReducer
extends Reducer<Text,IntWritable,Text,IntWritable> {
private IntWritable result = new IntWritable();
public void reduce(Text key, Iterable<IntWritable> values,
Context context
) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
result.set(sum);
context.write(key, result);
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
if (otherArgs.length != 2) {
System.err.println("Usage: wordcount <in> <out>");
System.exit(2);
}
Job job = new Job(conf, "word count");
job.setJarByClass(WordCount.class);
job.setMapperClass(TokenizerMapper.class);
job.setCombinerClass(IntSumReducer.class);
job.setReducerClass(IntSumReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
(2)我们把这个文件放到hadoop的工作目录下,并在工作目录下面新建文件夹WordCount
(3)
javac -classpath hadoop-core-0.20.203.0.jar:lib/commons-cli-1.2.jar -d WordCount WordCount.java
(4)进入到WordCount文件夹中执行
jar -cvf wordcount.jar org/*
然后把生成的jar拷贝到hadoop工作目录下面
(5)然后在hadoop工作目录下面新建一个input目录 mkdir input,在目录里面新建一个文件vi file1,输入以下内容:
hello world
hello hadoop
hello mapreduce
,把该文件上传到hadoop的分布式文件系统中去
./bin/hadoop fs -put input/file* input
(6)然后我们开始执行
./bin/hadoop jar wordcount.jar org.apache.hadoop.examples.WordCount input wordcount_output
(7)最后我们查看运行结果
./bin/hadoop fs -cat wordcount_output/part-r-00000
hadoop 1
hello 3
mapreduce 1
world 1
分享到:
相关推荐
hadoop单词计数程序
Hadoop Mac安装和Map Reduce示例 先决条件 确认适用于HadoopJava 1.6。*或更高版本 $ java -version java version " 1.7.0_79 " Java(TM) SE Runtime Environment (build 1.7.0_79-b15) Java HotSpot(TM) 64-Bit ...
hadoop mapreduce helloworld 能调试 详细内容请看:http://blog.csdn.net/wild46cat/article/details/53641765
这个程序是基本的字数统计,Hadoophello world Hadoop 多行读取: 该程序读取传递两行作为记录而不是默认的一行 Hadoop 将数组传递给减速器: 该程序将一个数组从映射器传递给化简器,为了传递数组,需要扩展 ...
Hadoop “Hello World” 示例-附件资源
性的博客和演示文稿,它们会教你如何做一个“Hello World”的示例。当足够熟悉之后,你就会 知道如何在邮件列表中提问,在大小会议中邂逅专家,甚至自己阅读源代码。但在这中间存在一 个巨大的知识落差,你的胃口更...
通过hdfs getconf -confKey dfs.ha.automatic-failover.enabled获取zkfc的机器列表,并在这些机器上关闭zk
本次实验的目的是让大家熟悉搭建智能手机开发平台的过程,了解Andorid开发项目的基本文件目录结构,并实现HelloWorld小例程。 实验要求 1. 完成Android开发平台的搭建及相关配置 2. 创建项目并熟悉文件目录结构 3. ...
hive安装,部署,简单测试helloworld
jar包,亲测可用
jar包,亲测可用
jar包,亲测可用
jar包,亲测可用
jar包,亲测可用
jar包,亲测可用
jar包,亲测可用
jar包,亲测可用
jar包,亲测可用
Hadoop EchoSystem 演示: avro 级联 cassandra 紧缩 flink 水槽 giraph hadoop mahout tez zookeeper 中间件演示: kafka 弹性搜索 netty akka NoSQL 演示: mongodb neo4j redis riak 泰坦风暴与火花: 和来自官方...