06-MapReduce(3)FileInputFormat实现类

目录

一、FileInputFormat实现类

二、常见实现类的介绍

三、KeyValueTextInputFormat使用案例 

1、需求

2、需求分析

3、代码实现

(1)建包

(2)编写KVTextMapper.java

(3)编写KVTextReducer.java

(4)编写KVTextDriver.java

 (5)设置输入输出参数

(6)运行

(7)查看结果

四、自定义InputFormat

自定义InputFormat实操 


一、FileInputFormat实现类

二、常见实现类的介绍

 

三、KeyValueTextInputFormat使用案例 

1、需求

统计输入文件中每一行的第一个单词相同的行数

(1)输入数据:

Hello hi good morning
Please
Hello bye quit
Fox panda mouse
Please give me a book

(2)期望输出结果

Fox 1
Hello 2
Please 2

2、需求分析

3、代码实现

(1)建包

在src/main/java下新建包com.wolf.mr.kv

(2)编写KVTextMapper.java

新建KVTextMapper.java,写入以下内容:

package com.wolf.mr.kv;

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

import java.io.IOException;

public class KVTextMapper extends Mapper<Text,Text,Text, IntWritable> {
    IntWritable v = new IntWritable(1);
    
    @Override
    protected void map(Text key, Text value, Context context) throws IOException, InterruptedException {
        // 1.pack obj
        
        // 2.write out
        context.write(key,v);
    }
}

(3)编写KVTextReducer.java

新建KVTextReducer.java,写入以下内容:

package com.wolf.mr.kv;

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

import java.io.IOException;

public class KVTextReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
    IntWritable v = new IntWritable();

    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        // 1. sum
        int sum = 0;
        for (IntWritable value : values) {
            sum += value.get();
        }
        v.set(sum);
        // 2. write out
        context.write(key,v);
    }
}

(4)编写KVTextDriver.java

package com.wolf.mr.kv;

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.KeyValueLineRecordReader;
import org.apache.hadoop.mapreduce.lib.input.KeyValueTextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class KVTextDriver {

    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        Configuration conf = new Configuration();
        conf.set(KeyValueLineRecordReader.KEY_VALUE_SEPERATOR," ");
        // 1. get job
        Job job = Job.getInstance(conf);
        job.setInputFormatClass(KeyValueTextInputFormat.class);
        // 2. set jar path
        job.setJarByClass(KVTextDriver.class);
        // 3. link mapper and reducer
        job.setMapperClass(KVTextMapper.class);
        job.setReducerClass(KVTextReducer.class);
        // 4. set mapper type key value
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        // 5  set final type
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        // 6. path
        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));
        // 7. submit job
        boolean result = job.waitForCompletion(true);
        System.exit(result ? 0 : 1);

    }
}

这里需要注意的就是这两行:

        conf.set(KeyValueLineRecordReader.KEY_VALUE_SEPERATOR," ");
        job.setInputFormatClass(KeyValueTextInputFormat.class);

 (5)设置输入输出参数

/home/wolf/kv.txt /home/wolf/output/out_KVText

(6)运行

(7)查看结果

可以看到,成功完成了需求。 

四、自定义InputFormat

虽然MapReduce已经提供了三种InputFormat实现方法,但是仍然无法满足全部的应用场景,因此,我们常常需要自定义InputFormat,以满足各种各样的要求。

自定义InputFormat实操 

这里先挖个坑,以后再回来学。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
使用MapReduce编程模型实现数据分析处理的步骤如下: 1. 确定数据分析处理的目标:首先,需要明确要分析哪些数据、分析的目的是什么。例如,要统计某个网站的日访问量。 2. 数据预处理:对数据进行必要的预处理,如去掉无用信息、格式化数据等。 3. 输入输出的定义:定义输入和输出的格式,例如输入为文本文件,输出为CSV文件。 4. Map函数:编写Map函数,将输入数据转换为键值对。 5. Reduce函数:编写Reduce函数,对Map函数输出的键值对进行相应的计算和处理。 6. 指定任务的输入输出路径:指定输入和输出路径,并将MapReduce作业提交到集群中执行。 7. 监控和调试:监控MapReduce作业的执行情况,根据需要进行调试。 下面是一个简单的MapReduce程序,用于统计文本文件中每个单词出现的次数: Mapper: ``` public class WordCountMapper extends 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, Context context) throws IOException, InterruptedException { String line = value.toString(); StringTokenizer tokenizer = new StringTokenizer(line); while (tokenizer.hasMoreTokens()) { word.set(tokenizer.nextToken()); context.write(word, one); } } } ``` Reducer: ``` public class WordCountReducer 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)); } } ``` 主函数: ``` public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = Job.getInstance(conf, "word count"); job.setJarByClass(WordCount.class); job.setMapperClass(WordCountMapper.class); job.setCombinerClass(WordCountReducer.class); job.setReducerClass(WordCountReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } ``` 在以上代码中,WordCountMapper用于将输入文本文件中的每个单词转换为键值对,其中键为单词,值为1;WordCountReducer用于将每个单词的值相加,得到每个单词出现的总次数;主函数用于指定输入输出路径,并将MapReduce作业提交到集群中执行。 使用MapReduce编程模型可以方便地实现数据分析处理,将大数据分成多个小数据进行分析处理,提高了数据处理的效率。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值