hadooop的wordcount程序

本文介绍了如何在Hadoop环境中创建并运行WordCount程序,详细讲述了从新建项目文件夹到编写主程序类,再到最终成功运行程序的完整过程。
摘要由CSDN通过智能技术生成

创建项目文件夹

sudo mkdir -p ~/hpro/com/vs/example

创建主程序类

sudo gedit ~/hpro/com/vs/example/WordCount.java

java类如下

package com.vs.example;

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.mapred.*;
import org.apache.hadoop.util.*;

public class WordCount {
	public static class Map extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> {
		private final static IntWritable one = new IntWritable(1);
		private Text word = new Text();
		
		public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
			String line = value.toString();
			StringTokenizer tokenizer = new StringTokenizer(line);
			while(tokenizer.hasMoreTokens())
			{
				word.set(tokenizer.nextToken());
				output.collect(word, one);
			}
		}	
	}

	public static class Reduce extends MapReduceBase implements Reducer<Text, IntWritable, Text, IntWritable> {
		public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
			int sum = 0;
			while(values.hasNext())
			{
				sum += values.next().get();
			}
			output.collect(key, new IntWritable(sum));
		}
	}

	public static void main(String[] args) throws Exception
	{
		JobConf conf = new JobConf(WordCount.class);
		conf.setJobName("wordcount");

		conf.setOutputKeyClass(Text.class);
		conf.setOutputValueClass(IntWritable.class);
		
		conf.setMapperClass(Map.class);
		conf.setReducerClass(Reduce.class);

		conf.setInputFormat(TextInputFormat.class);
		conf.setOutputFormat(TextOutputFormat.class);
		
		FileInputFormat.setInputPaths(conf, new Path(args[0]));
		FileOutputFormat.setOutputPath(conf, new Path(args[1]));
		
		JobClient.runJob(conf);
	}	
}
创建类文件的目录

mkdir ~/hpro/FirstJar

编译

javac -classpath ~/hadoop-1.0.4/hadoop-core-1.0.4.jar -d ~/hpro/FirstJar hpro/com/vs/example/WordCount.java

打成jar包

$JAVA_HOME/bin/jar -cvf ~/hpro/wordcount.jar -C ~/hpro/FirstJar/ .

准备两个输入文件

echo "Hello World Bye World" > ~/hpro/file01

echo "Hello Hadoop GoodBye Hadoop" > ~/hpro/file02

准备上传文件到dfs

~/hadoop-1.0.4/bin/hadoop dfs -mkdir input

~/hadoop-1.0.4/bin/hadoop dfs -put ~/hpro/file0* input

运行程序

~/hadoop-1.0.4/bin/hadoop jar ~/hpro/wordcount.jar com.vs.example.WordCount input output

然后就可以在管理控制台查看运行记录了

http://localhost:50030/jobtracker.jsp

也可以把输出文件拉下来,查看

~/hadoop-1.0.4/bin/hadoop dfs -get output ~/hpro/

~/hpro/outpu就是输出的文件,进入可以查看运行结果

cd hpro/output
gedit part-00000 
文件part-00000的内容如下

Bye	1
Goodbye	1
Hadoop	2
Hello	2
World	2

Good Luck!


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值