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

简介

TextInputFormat可以将文本文件分块并逐行读入以便Map节点进行处理。
读入一行时,所产生的主键Key就是当前行在整个文本文件中的字符偏移量,而value就是该行的内容。

示例:单词个数统计

准备工作

在hdfs的根目录下创建input文件夹,然后在里面放置4个大小分别为1.5M、35M、5.5M、6.5M的小文件作为输入数据

具体代码

Mapper类

public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {private Text mapOutputKey = new Text();private IntWritable mapOutputValue = new IntWritable();@Overrideprotected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {String linevalue = value.toString();  //1.将读取的文件变成,偏移量+内容//读取一行数据StringTokenizer st = new StringTokenizer(linevalue);//使用空格分隔while (st.hasMoreTokens()) {//判断是否还有分隔符,有的话代表还有单词String word = st.nextToken();//返回从当前位置到下一个分隔符之间的字符串(单词)mapOutputKey.set(word);mapOutputValue.set(1);context.write(mapOutputKey, mapOutputValue);}}
}

Reducer类

public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {private IntWritable outputValue = new IntWritable();@Overrideprotected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {int sum = 0;    //汇总for (IntWritable value : values) {sum += value.get();}outputValue.set(sum);context.write(key, outputValue);}
}

Driver类

public class WordCountDriver {public static void main(String[] args) throws Exception {//需要在resources下面提供core-site.xml文件args = new String[]{"/input/","/output/"};Configuration cfg = new Configuration();   //获取配置Job job = Job.getInstance(cfg, WordCountDriver.class.getSimpleName());job.setJarByClass(WordCountDriver.class);//设置map与需要设置的内容类 + 输出key与valuejob.setMapperClass(WordCountMapper.class);job.setMapOutputKeyClass(Text.class);job.setMapOutputValueClass(IntWritable.class);//设置reducejob.setReducerClass(WordCountReducer.class);job.setOutputKeyClass(Text.class);job.setOutputValueClass(IntWritable.class);//设置input与outputFileInputFormat.addInputPath(job, new Path(args[0]));Path op1 = new Path(args[1]);FileOutputFormat.setOutputPath(job, op1);FileSystem fs = FileSystem.get(cfg);if (fs.exists(op1)) {fs.delete(op1, true);System.out.println("存在此输出路径,已删除!!!");}//将job交给Yarnboolean issucess = job.waitForCompletion(true);int status=  issucess ? 0 : 1;System.exit(status);}
}

FileInputFormat 之 TextInputFormat相关推荐

  1. 8、FileInputFormat

    InputFormat InputFormat FileInputFormat切片分析 FileInputFormat默认切片策略 TextInputFormat KeyValueInputForma ...

  2. 辅助排序和Mapreduce整体流程

    一.辅助排序 需求:先有一个订单数据文件,包含了订单id.商品id.商品价格,要求将订单id正序,商品价格倒序,且生成结果文件个数为订单id的数量,每个结果文件中只要一条该订单最贵商品的数据. 思路: ...

  3. 【Hive任务优化】—— Map、Reduce数量调整

    文章目录 一.如何调整任务map数量 1.FileInputFormat的实现逻辑介绍 1.1 getSplits方法实现 2.CombineFileInputFormat的实现逻辑介绍 2.1 ge ...

  4. MapReduce分布式计算和编程原理总结

    ​​​​​​ inputformat 在MapReduce程序的开发过程中,往往需要用到FileInputFormat与TextInputFormat,TextInputFormat这个类继FileI ...

  5. hadoop源码阅读(一)(InputFormat源码)

    InputFormat Inputformat的介绍 1. InputFormat的继承树 1.1 InputFormat源码: 1.2 FileInputFormat源码: 1.3 TextInpu ...

  6. Hadoop中的FileInputFormat切片机制、FileInputFormat切片大小的参数配置、TextInputFormat、CombineTextInputFormat切片机制

    文章目录 13.MapReduce框架原理 13.1InputFormat数据输入 13.1.4FileInputFormat切片机制 13.1.4.1切片机制 13.1.4.2案例分析 13.1.4 ...

  7. Hadoop的TextInputFormat的作用,如何自定义实现的

    代码先行:[源代码] 二话不说,代码先行! /*** Licensed to the Apache Software Foundation (ASF) under one* or more contr ...

  8. MapReduce InputFormat之FileInputFormat

    一:简单认识InputFormat类 InputFormat主要用于描述输入数据的格式,提供了以下两个功能: 1).数据切分,按照某个策略将输入数据且分成若干个split,以便确定Map Task的个 ...

  9. 大数据培训FileInputFormat实现类

    FileInputFormat实现类 思考:在运行MapReduce程序时,输入的文件格式包括:基于行的日志文件. 二进制格式文件.数据库表等.那么,针对不同的数据类型,MapReduce是如 何读取 ...

最新文章

  1. 基于HTML5的3D网络拓扑树呈现
  2. PostgreSQL 编译安装
  3. r语言 服务器网页版ide RStudio Server 简介
  4. 【loj#6220】sum
  5. 直播 | 北邮博士生纪厚业:异质图神经网络在阿里推荐业务中的探索
  6. ansible puppet saltstack三款自动化运维工具的对比
  7. java 方法 示例_Java语言环境getDisplayVariant()方法与示例
  8. CentOS8-Tomcat7安装并设置开机自启动
  9. wincc上下文不存在或无效是_wincc安装
  10. 动手学习深度学习keras版——从零开始实现Vnet 2D版
  11. 知识图谱-知识体系与知识融合-实体消歧
  12. layui设置按钮不可点击_layui upload 模块点击选择文件按钮的禁用与启用功能
  13. 全通系统定义、零极点关系、应用
  14. App兼容性测试/MONKEY配置和安装
  15. 学习Java的第五天 | 定义方法 | 引用数据类型——类 | 访问权限-修饰符 | 全局变量与局部变量 | toString | 构造方法 | 方法重载
  16. 【调剂】重庆理工大学2022年硕士研究生招生调剂公告
  17. 为什么https比http更安全?_货车拉钢卷为什么都是立式运输,平放不是更安全吗?...
  18. python微软雅黑字体_win10+python3.7下matplotlib显示中文,可使用微软雅黑.md
  19. MindManager 思维导图使用
  20. 腾讯通RTX无法正常拉取组织架构的解决方法

热门文章

  1. 怎么用软件做亚马逊跟卖
  2. c++操作符重载(数组类重载[]、=、==、!=操作符)
  3. [另类方式破解]支付宝的小程序sign验签参数算法
  4. 人生感悟2 低调做人,高调做事
  5. 用了 TCP 协议,数据一定不会丢吗?
  6. C语言每日一练——第154天:牛顿迭代法求方程根
  7. Java学习笔记-Pair和Map
  8. c语言圆桌算法,算法 – 人们可以在圆桌中坐多少种不同的方式?
  9. 各游戏引擎接入安卓SDK
  10. 虚拟机VMware显示“内部错误”怎么办?