您当前的位置: 首页 > 

梁云亮

暂无认证

  • 2浏览

    0关注

    1211博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

FileInputFormat 之 KeyValueInputFormat

梁云亮 发布时间:2019-12-06 13:30:28 ,浏览量:2

前置课程: HDFS开发环境搭建

数据

在这里插入图片描述

代码实现 Mapper
public class KVInputFormatMapper extends Mapper {
    protected void map(Text key, Text value, Context context) throws IOException, InterruptedException {
        context.write(key, new LongWritable(1));
        context.write(value, new LongWritable(1));
    }
}
测试代码
public class KVInputFormatDriver {
    public static void main(String[] args) throws Exception {
        // 数据输入路径和输出路径
        args = new String[2];
        args[0] = "src/main/resources/kv/kvi2/";
        args[1] = "src/main/resources/kv/kvo2";

        Configuration cfg = new Configuration();// 读取配置文件
        cfg.set("mapreduce.framework.name", "local");
        cfg.set("fs.defaultFS", "file:///");
        cfg.set(KeyValueLineRecordReader.KEY_VALUE_SEPARATOR, "\t");

        final FileSystem filesystem = FileSystem.get(cfg);
        if (filesystem.exists(new Path(args[0]))) {
            filesystem.delete(new Path(args[1]), true);
        }

        // 新建一个任务
        Job job = Job.getInstance(cfg);
        job.setJarByClass(KVInputFormatDriver.class);  // 设置主类

        job.setInputFormatClass(KeyValueTextInputFormat.class);//设置输入格式

        job.setMapperClass(KVInputFormatMapper.class);   // Mapper
      
  job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);

        job.setNumReduceTasks(0);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);

        FileInputFormat.addInputPath(job, new Path(args[0]));        // 输入路径
        FileOutputFormat.setOutputPath(job, new Path(args[1]));        // 输出路径

        // 提交任务
        int ec = job.waitForCompletion(true) ? 0 : 1;
        System.exit(ec);
    }

}  
结果:

在这里插入图片描述

关注
打赏
1665409997
查看更多评论
立即登录/注册

微信扫码登录

0.0413s