idea使用maven搭建Hadoop环境并运行第一个mapreduce程序实现WordCount

(一)MapReuduce入门之环境搭建

1,定义:mapReduce是一种分布式计算框架,Mapreduce 核心功能是将用户编写的业务逻辑代码和自带默认组件整合成一个完整的

分布式运算程序,并发运行在一个 hadoop 集群上。

2,特点:mapReduce易于编程、扩展性好、适合处理PB级别数据;但是他不适合处理实时数据,流失计算、有向图计算等。

3,mapreduce程序编写规则:

1)Mapper 阶段

(1)用户自定义的 Mapper 要继承自己的父类

(2)Mapper 的输入数据是 KV 对的形式(KV 的类型可自定义)

(3)Mapper 中的业务逻辑写在 map()方法中

(4)Mapper 的输出数据是 KV 对的形式(KV 的类型可自定义)

(5)map()方法(maptask 进程)对每一个<K,V>调用一次

2)Reducer 阶段

(1)用户自定义的 Reducer 要继承自己的父类

(2)Reducer 的输入数据类型对应 Mapper 的输出数据类型,也是 KV

(3)Reducer 的业务逻辑写在 reduce()方法中

(4)Reducetask 进程对每一组相同 k 的<k,v>组调用一次 reduce()方法

3)Driver 阶段

整个程序需要一个 Drvier 来进行提交,提交的是一个描述了各种必要信息的 job 对象。

现编写第一个MapReduce程序实现WordCount案例:

环境准备:

IDEA新建一个maven工程,引入hadoop对应版本的核心依赖,并将配置文件纳入到资源管理中:

<dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>2.7.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>2.7.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-hdfs</artifactId>
            <version>2.7.2</version>
</dependency>
image-20201118092341397

1,编写map程序

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

    Text k = new Text();
    IntWritable v = new IntWritable(1);
    //重写map方法,实现业务逻辑
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        //1,获取一行
        String line = value.toString();
        //2,切割
        String[] words = line.split(" ");
        for(String word:words){
            k.set(word);
            context.write(k,v);
        }

    }
}

2,编写reduce程序

public class WordCountReduce extends Reducer<Text,IntWritable,Text,IntWritable> {
    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        //子类构造方法中会有一个默认隐含的super()方法,用于调用父类构造
        //super.reduce(key, values, context);
        int sum = 0;
        for(IntWritable count:values){
            sum += count.get();
        }
        context.write(key,new IntWritable(sum));
    }
}

3,编写驱动类

public class WordCountDriver {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        //1,获取job
        Configuration configuration = new Configuration();
        Job job = Job.getInstance();
        //2,设置jar加载路径
        job.setJarByClass(WordCountDriver.class);
        job.setMapperClass(WordCountMap.class);
        job.setReducerClass(WordCountReduce.class);
        // 4 设置 map 输出
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        // 5 设置 Reduce 输出
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        // 6 设置输入和输出路径
        FileInputFormat.setInputPaths(job,new Path(args[0]));
        FileOutputFormat.setOutputPath(job,new Path(args[1]));
        // 7 提交
        boolean result = job.waitForCompletion(true);
        System.exit(result ? 0 : 1);
    }
}

然后使用工具将main文件打成jar包:

在project structre->加入Artifacts,选择From moudles from dependencies,选择自己的main类,并设置jar包输出目录;然后通过工具栏的Build,选择build Artifacts->build即可达成jar包。最后就是通过FTP工具,将jar包发送到集群环境中,使用以下命令即可运行。

hadoop jar jar包名 main类的全类名 输入目录 输出目录

image-20201117112544404

那么如何在本地运行mapreduce程序呢?在实际的生产开发中,程序需要在本地测试无误后再打成jar包发布到集群中的。

首先我们需要设置main类的configuration,提前添加好输入输出参数

image-20201118093855794

然后设置将文件系统修改为本地运行模式:

  • 3
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值