初探大数据之基于Hadoop的MapRduce和spark(scala语言编写)的wordcount示例

一、Hadoop的MapRduce编写WordCount

简单版的WordCount示例需要三个java类,*Mapper、*Reducer和*Driver,其中*Mapper和*Reducer这两个类有需要继承的父类。

1、创建WordCountMapper类,继承Mapper,并且重写其中的map方法

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable>{

	@Override
	protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context)
			throws IOException, InterruptedException {
		//获取文本中的每一行
		String lines = value.toString();
		//每一行按照空格键进行分割,得到单词数组
		String[] words = lines.split(" ");
		//把每个单词输出到reducer
		for (String word : words) {
			context.write(new Text(word), new IntWritable(1));
		}
		
	}

}

2、创建WordCountReducer类,继承Reducer,并且重写其中的reduce方法

import java.io.IOException;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable>{

	@Override
	protected void reduce(Text key, Iterable<IntWritable> values,
			Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
		int count = 0;
		for (IntWritable value : values) {
			count += value.get();
		}
		context.write(key, new IntWritable(count));
	}
	
}

3、创建WordCountDriver类

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.output.FileOutputFormat;

public class WordCountDriver {
	
	public static void main(String[] args) throws Exception {
		// 创建hadoop的配置信息对象
		Configuration conf = new Configuration();
		
		// 配置提交到yarn上运行,windows和Linux变量不一样
		Job job = Job.getInstance(conf);
		
		//指定job任务的main方法所在类
		job.setJarByClass(WordCountDriver.class);
		
		//指定job任务的mapper和reducer
		job.setMapperClass(WordCountMapper.class);
		job.setReducerClass(WordCountReducer.class);
		
		//指定mapper的输出kv类型
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(IntWritable.class);
		
		//指定reducer的输出kv类型
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(IntWritable.class);
		
		//指定job输入文件的原始目录
		FileInputFormat.setInputPaths(job, args[0]);
		
		//指定job输出目录
		FileOutputFormat.setOutputPath(job, new Path(args[1]));
		
		//提交任务
//		job.submit();
		boolean result = job.waitForCompletion(true);
		System.exit(result?0:1);
	}
}

4、打成jar包命名为hadoop_wordcount.jar,并且拷贝到hadoop集群中

5、执行hadoop_wordcount.jar程序

(1)启动hadoop集群

小提示:namedode和resourcemanager如果不是同一台机器,不能在namenode所在机器上启动yarn,应该在                          resourcemanager所在机器上启动yarn,所以,不能在namenode所在机器上直接使用sbin/start-all.sh命令,这样启动                    resourcemanager会失败的(亲自踩过这个坑) 

sbin/start-hdfs.sh    --启动hdfs

sbin/start-yarn.sh    --启动yarn

(2)执行jar包程序

hadoop  jar  hadoop_wordcount.jar  com.hadoop.wordcount.WordCountDriver  /input/input.txt  /output

以上代码解释:“hadoop  jar”是hadoop执行jar包的命令

                         “hadoop_wordcount.jar”是jar包的名称

                         “com.hadoop.wordcount.WordCountDriver”是驱动程序(WordCountDriver)所在的全路径类名

                         “/input/input.txt”是输入文件的位置,默认这个路径是存在hdfs上的

                         “/output”是输出文件存放的位置,默认是输出到hdfs上,程序执行之后,会自动创建output目录,所以刚开始不存 在这个目录

二、spark中运行WordCount示例,在spark-shell中演示,基于scala编写

1、启动spark集群

sbin/start-all.sh

2、启动spark-shell

bin/spark-shell

3、代码编写

//创建一个RDD,这里就用spark目录下的README.md文本做演示

val  textRDD = sc.textFile("file:///spark/README.md")

//对单词进行分割,并且做成一个RDD

val wordRDD = textRDD.flatMap(_.split(" "))

//把每个单词做成一个(key,1)的形式

val mapRDD = wordRDD.map((_,1))

//进行单词的统计

val countRDD = mapRDD.reduceByKey(_+_)

//保存到文件中

countRDD.saveAsTextFile("file:///spark/output")

保存成功之后,在output目录生成“part-”开头的文件,然后就可以查看了。

(有错误的地方,希望大牛们能够指出,小弟一定改正)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值