hadoop的FileSplit简单使用

FileSplit类继承关系:

FileSplit类中的属性和方法:

作业输入:

[java] view plaincopy
  1. hadoop@hadoop:/home/hadoop/blb$ hdfs dfs -text /user/hadoop/libin/input/inputpath1.txt
  2. hadoop  a
  3. spark   a
  4. hive    a
  5. hbase   a
  6. tachyon a
  7. storm   a
  8. redis   a
  9. hadoop@hadoop:/home/hadoop/blb$ hdfs dfs -text /user/hadoop/libin/input/inputpath2.txt
  10. hadoop  b
  11. spark   b
  12. kafka   b
  13. tachyon b
  14. oozie   b
  15. flume   b
  16. sqoop   b
  17. solr    b
  18. hadoop@hadoop:/home/hadoop/blb$
[java] view plaincopy
  1. hadoop@hadoop:/home/hadoop/blb$ hdfs dfs -text /user/hadoop/libin/input/inputpath1.txt
  2. hadoop  a
  3. spark   a
  4. hive    a
  5. hbase   a
  6. tachyon a
  7. storm   a
  8. redis   a
  9. hadoop@hadoop:/home/hadoop/blb$ hdfs dfs -text /user/hadoop/libin/input/inputpath2.txt
  10. hadoop  b
  11. spark   b
  12. kafka   b
  13. tachyon b
  14. oozie   b
  15. flume   b
  16. sqoop   b
  17. solr    b
  18. hadoop@hadoop:/home/hadoop/blb$

代码:

[java] view plaincopy
  1. import java.io.IOException;
  2. import org.apache.hadoop.conf.Configuration;
  3. import org.apache.hadoop.fs.Path;
  4. import org.apache.hadoop.io.LongWritable;
  5. import org.apache.hadoop.io.NullWritable;
  6. import org.apache.hadoop.io.Text;
  7. import org.apache.hadoop.mapred.SplitLocationInfo;
  8. import org.apache.hadoop.mapreduce.Job;
  9. import org.apache.hadoop.mapreduce.Mapper;
  10. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  11. import org.apache.hadoop.mapreduce.lib.input.FileSplit;
  12. import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
  13. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  14. import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
  15. import org.apache.hadoop.util.GenericOptionsParser;
  16. public class GetSplitMapReduce {
  17. public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
  18. Configuration conf = new Configuration();
  19. String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
  20. if(otherArgs.length!=2){
  21. System.err.println("Usage databaseV1 <inputpath> <outputpath>");
  22. }
  23. Job job = Job.getInstance(conf, GetSplitMapReduce.class.getSimpleName() + "1");
  24. job.setJarByClass(GetSplitMapReduce.class);
  25. job.setMapOutputKeyClass(Text.class);
  26. job.setMapOutputValueClass(Text.class);
  27. job.setOutputKeyClass(Text.class);
  28. job.setOutputValueClass(NullWritable.class);
  29. job.setMapperClass(MyMapper1.class);
  30. job.setNumReduceTasks(0);
  31. job.setInputFormatClass(TextInputFormat.class);
  32. job.setOutputFormatClass(TextOutputFormat.class);
  33. FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
  34. FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
  35. job.waitForCompletion(true);
  36. }
  37. public static class MyMapper1 extends Mapper<LongWritable, Text, Text, NullWritable>{
  38. @Override
  39. protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, NullWritable>.Context context)
  40. throws IOException, InterruptedException {
  41. FileSplit fileSplit=(FileSplit) context.getInputSplit();
  42. String pathname=fileSplit.getPath().getName();  //获取目录名字
  43. int depth = fileSplit.getPath().depth();    //获取目录深度
  44. Class<? extends FileSplit> class1 = fileSplit.getClass(); //获取当前类
  45. long length = fileSplit.getLength();    //获取文件长度
  46. SplitLocationInfo[] locationInfo = fileSplit.getLocationInfo(); //获取位置信息
  47. String[] locations = fileSplit.getLocations();  //获取位置
  48. long start = fileSplit.getStart(); //The position of the first byte in the file to process.
  49. String string = fileSplit.toString();
  50. //fileSplit.
  51. context.write(new Text("===================================================================================="), NullWritable.get());
  52. context.write(new Text("pathname--"+pathname), NullWritable.get());
  53. context.write(new Text("depth--"+depth), NullWritable.get());
  54. context.write(new Text("class1--"+class1), NullWritable.get());
  55. context.write(new Text("length--"+length), NullWritable.get());
  56. context.write(new Text("locationInfo--"+locationInfo), NullWritable.get());
  57. context.write(new Text("locations--"+locations), NullWritable.get());
  58. context.write(new Text("start--"+start), NullWritable.get());
  59. context.write(new Text("string--"+string), NullWritable.get());
  60. }
  61. }
  62. }
[java] view plaincopy
  1. import java.io.IOException;
  2. import org.apache.hadoop.conf.Configuration;
  3. import org.apache.hadoop.fs.Path;
  4. import org.apache.hadoop.io.LongWritable;
  5. import org.apache.hadoop.io.NullWritable;
  6. import org.apache.hadoop.io.Text;
  7. import org.apache.hadoop.mapred.SplitLocationInfo;
  8. import org.apache.hadoop.mapreduce.Job;
  9. import org.apache.hadoop.mapreduce.Mapper;
  10. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  11. import org.apache.hadoop.mapreduce.lib.input.FileSplit;
  12. import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
  13. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  14. import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
  15. import org.apache.hadoop.util.GenericOptionsParser;
  16. public class GetSplitMapReduce {
  17. public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
  18. Configuration conf = new Configuration();
  19. String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
  20. if(otherArgs.length!=2){
  21. System.err.println("Usage databaseV1 <inputpath> <outputpath>");
  22. }
  23. Job job = Job.getInstance(conf, GetSplitMapReduce.class.getSimpleName() + "1");
  24. job.setJarByClass(GetSplitMapReduce.class);
  25. job.setMapOutputKeyClass(Text.class);
  26. job.setMapOutputValueClass(Text.class);
  27. job.setOutputKeyClass(Text.class);
  28. job.setOutputValueClass(NullWritable.class);
  29. job.setMapperClass(MyMapper1.class);
  30. job.setNumReduceTasks(0);
  31. job.setInputFormatClass(TextInputFormat.class);
  32. job.setOutputFormatClass(TextOutputFormat.class);
  33. FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
  34. FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
  35. job.waitForCompletion(true);
  36. }
  37. public static class MyMapper1 extends Mapper<LongWritable, Text, Text, NullWritable>{
  38. @Override
  39. protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, NullWritable>.Context context)
  40. throws IOException, InterruptedException {
  41. FileSplit fileSplit=(FileSplit) context.getInputSplit();
  42. String pathname=fileSplit.getPath().getName();  //获取目录名字
  43. int depth = fileSplit.getPath().depth();    //获取目录深度
  44. Class<? extends FileSplit> class1 = fileSplit.getClass(); //获取当前类
  45. long length = fileSplit.getLength();    //获取文件长度
  46. SplitLocationInfo[] locationInfo = fileSplit.getLocationInfo(); //获取位置信息
  47. String[] locations = fileSplit.getLocations();  //获取位置
  48. long start = fileSplit.getStart(); //The position of the first byte in the file to process.
  49. String string = fileSplit.toString();
  50. //fileSplit.
  51. context.write(new Text("===================================================================================="), NullWritable.get());
  52. context.write(new Text("pathname--"+pathname), NullWritable.get());
  53. context.write(new Text("depth--"+depth), NullWritable.get());
  54. context.write(new Text("class1--"+class1), NullWritable.get());
  55. context.write(new Text("length--"+length), NullWritable.get());
  56. context.write(new Text("locationInfo--"+locationInfo), NullWritable.get());
  57. context.write(new Text("locations--"+locations), NullWritable.get());
  58. context.write(new Text("start--"+start), NullWritable.get());
  59. context.write(new Text("string--"+string), NullWritable.get());
  60. }
  61. }
  62. }

对应inputpath2.txt文件的输出:

[java] view plaincopy
  1. hadoop@hadoop:/home/hadoop/blb$ hdfs dfs -text /user/hadoop/libin/out2/part-m-00000
  2. ====================================================================================
  3. pathname--inputpath2.txt
  4. depth--5
  5. class1--class org.apache.hadoop.mapreduce.lib.input.FileSplit
  6. length--66
  7. locationInfo--null
  8. locations--[Ljava.lang.String;@4ff41ba0
  9. start--0
  10. string--hdfs://hadoop:9000/user/hadoop/libin/input/inputpath2.txt:0+66
  11. ====================================================================================
  12. pathname--inputpath2.txt
  13. depth--5
  14. class1--class org.apache.hadoop.mapreduce.lib.input.FileSplit
  15. length--66
  16. locationInfo--null
  17. locations--[Ljava.lang.String;@2341ce62
  18. start--0
  19. string--hdfs://hadoop:9000/user/hadoop/libin/input/inputpath2.txt:0+66
  20. ====================================================================================
  21. pathname--inputpath2.txt
  22. depth--5
  23. class1--class org.apache.hadoop.mapreduce.lib.input.FileSplit
  24. length--66
  25. locationInfo--null
  26. locations--[Ljava.lang.String;@35549603
  27. start--0
  28. string--hdfs://hadoop:9000/user/hadoop/libin/input/inputpath2.txt:0+66
  29. ====================================================================================
  30. pathname--inputpath2.txt
  31. depth--5
  32. class1--class org.apache.hadoop.mapreduce.lib.input.FileSplit
  33. length--66
  34. locationInfo--null
  35. locations--[Ljava.lang.String;@4444ba4f
  36. start--0
  37. string--hdfs://hadoop:9000/user/hadoop/libin/input/inputpath2.txt:0+66
  38. ====================================================================================
  39. pathname--inputpath2.txt
  40. depth--5
  41. class1--class org.apache.hadoop.mapreduce.lib.input.FileSplit
  42. length--66
  43. locationInfo--null
  44. locations--[Ljava.lang.String;@7c23bb8c
  45. start--0
  46. string--hdfs://hadoop:9000/user/hadoop/libin/input/inputpath2.txt:0+66
  47. ====================================================================================
  48. pathname--inputpath2.txt
  49. depth--5
  50. class1--class org.apache.hadoop.mapreduce.lib.input.FileSplit
  51. length--66
  52. locationInfo--null
  53. locations--[Ljava.lang.String;@dee2400
  54. start--0
  55. string--hdfs://hadoop:9000/user/hadoop/libin/input/inputpath2.txt:0+66
  56. ====================================================================================
  57. pathname--inputpath2.txt
  58. depth--5
  59. class1--class org.apache.hadoop.mapreduce.lib.input.FileSplit
  60. length--66
  61. locationInfo--null
  62. locations--[Ljava.lang.String;@d7d8325
  63. start--0
  64. string--hdfs://hadoop:9000/user/hadoop/libin/input/inputpath2.txt:0+66
  65. ====================================================================================
  66. pathname--inputpath2.txt
  67. depth--5
  68. class1--class org.apache.hadoop.mapreduce.lib.input.FileSplit
  69. length--66
  70. locationInfo--null
  71. locations--[Ljava.lang.String;@2b2cf90e
  72. start--0
  73. string--hdfs://hadoop:9000/user/hadoop/libin/input/inputpath2.txt:0+66
[java] view plaincopy
  1. hadoop@hadoop:/home/hadoop/blb$ hdfs dfs -text /user/hadoop/libin/out2/part-m-00000
  2. ====================================================================================
  3. pathname--inputpath2.txt
  4. depth--5
  5. class1--class org.apache.hadoop.mapreduce.lib.input.FileSplit
  6. length--66
  7. locationInfo--null
  8. locations--[Ljava.lang.String;@4ff41ba0
  9. start--0
  10. string--hdfs://hadoop:9000/user/hadoop/libin/input/inputpath2.txt:0+66
  11. ====================================================================================
  12. pathname--inputpath2.txt
  13. depth--5
  14. class1--class org.apache.hadoop.mapreduce.lib.input.FileSplit
  15. length--66
  16. locationInfo--null
  17. locations--[Ljava.lang.String;@2341ce62
  18. start--0
  19. string--hdfs://hadoop:9000/user/hadoop/libin/input/inputpath2.txt:0+66
  20. ====================================================================================
  21. pathname--inputpath2.txt
  22. depth--5
  23. class1--class org.apache.hadoop.mapreduce.lib.input.FileSplit
  24. length--66
  25. locationInfo--null
  26. locations--[Ljava.lang.String;@35549603
  27. start--0
  28. string--hdfs://hadoop:9000/user/hadoop/libin/input/inputpath2.txt:0+66
  29. ====================================================================================
  30. pathname--inputpath2.txt
  31. depth--5
  32. class1--class org.apache.hadoop.mapreduce.lib.input.FileSplit
  33. length--66
  34. locationInfo--null
  35. locations--[Ljava.lang.String;@4444ba4f
  36. start--0
  37. string--hdfs://hadoop:9000/user/hadoop/libin/input/inputpath2.txt:0+66
  38. ====================================================================================
  39. pathname--inputpath2.txt
  40. depth--5
  41. class1--class org.apache.hadoop.mapreduce.lib.input.FileSplit
  42. length--66
  43. locationInfo--null
  44. locations--[Ljava.lang.String;@7c23bb8c
  45. start--0
  46. string--hdfs://hadoop:9000/user/hadoop/libin/input/inputpath2.txt:0+66
  47. ====================================================================================
  48. pathname--inputpath2.txt
  49. depth--5
  50. class1--class org.apache.hadoop.mapreduce.lib.input.FileSplit
  51. length--66
  52. locationInfo--null
  53. locations--[Ljava.lang.String;@dee2400
  54. start--0
  55. string--hdfs://hadoop:9000/user/hadoop/libin/input/inputpath2.txt:0+66
  56. ====================================================================================
  57. pathname--inputpath2.txt
  58. depth--5
  59. class1--class org.apache.hadoop.mapreduce.lib.input.FileSplit
  60. length--66
  61. locationInfo--null
  62. locations--[Ljava.lang.String;@d7d8325
  63. start--0
  64. string--hdfs://hadoop:9000/user/hadoop/libin/input/inputpath2.txt:0+66
  65. ====================================================================================
  66. pathname--inputpath2.txt
  67. depth--5
  68. class1--class org.apache.hadoop.mapreduce.lib.input.FileSplit
  69. length--66
  70. locationInfo--null
  71. locations--[Ljava.lang.String;@2b2cf90e
  72. start--0
  73. string--hdfs://hadoop:9000/user/hadoop/libin/input/inputpath2.txt:0+66

对应inputpath1.txt文件的输出:

[java] view plaincopy
  1. hadoop@hadoop:/home/hadoop/blb$ hdfs dfs -text /user/hadoop/libin/out2/part-m-00001
  2. ====================================================================================
  3. pathname--inputpath1.txt
  4. depth--5
  5. class1--class org.apache.hadoop.mapreduce.lib.input.FileSplit
  6. length--58
  7. locationInfo--null
  8. locations--[Ljava.lang.String;@4ff41ba0
  9. start--0
  10. string--hdfs://hadoop:9000/user/hadoop/libin/input/inputpath1.txt:0+58
  11. ====================================================================================
  12. pathname--inputpath1.txt
  13. depth--5
  14. class1--class org.apache.hadoop.mapreduce.lib.input.FileSplit
  15. length--58
  16. locationInfo--null
  17. locations--[Ljava.lang.String;@2341ce62
  18. start--0
  19. string--hdfs://hadoop:9000/user/hadoop/libin/input/inputpath1.txt:0+58
  20. ====================================================================================
  21. pathname--inputpath1.txt
  22. depth--5
  23. class1--class org.apache.hadoop.mapreduce.lib.input.FileSplit
  24. length--58
  25. locationInfo--null
  26. locations--[Ljava.lang.String;@35549603
  27. start--0
  28. string--hdfs://hadoop:9000/user/hadoop/libin/input/inputpath1.txt:0+58
  29. ====================================================================================
  30. pathname--inputpath1.txt
  31. depth--5
  32. class1--class org.apache.hadoop.mapreduce.lib.input.FileSplit
  33. length--58
  34. locationInfo--null
  35. locations--[Ljava.lang.String;@4444ba4f
  36. start--0
  37. string--hdfs://hadoop:9000/user/hadoop/libin/input/inputpath1.txt:0+58
  38. ====================================================================================
  39. pathname--inputpath1.txt
  40. depth--5
  41. class1--class org.apache.hadoop.mapreduce.lib.input.FileSplit
  42. length--58
  43. locationInfo--null
  44. locations--[Ljava.lang.String;@7c23bb8c
  45. start--0
  46. string--hdfs://hadoop:9000/user/hadoop/libin/input/inputpath1.txt:0+58
  47. ====================================================================================
  48. pathname--inputpath1.txt
  49. depth--5
  50. class1--class org.apache.hadoop.mapreduce.lib.input.FileSplit
  51. length--58
  52. locationInfo--null
  53. locations--[Ljava.lang.String;@dee2400
  54. start--0
  55. string--hdfs://hadoop:9000/user/hadoop/libin/input/inputpath1.txt:0+58
  56. ====================================================================================
  57. pathname--inputpath1.txt
  58. depth--5
  59. class1--class org.apache.hadoop.mapreduce.lib.input.FileSplit
  60. length--58
  61. locationInfo--null
  62. locations--[Ljava.lang.String;@d7d8325
  63. start--0
  64. string--hdfs://hadoop:9000/user/hadoop/libin/input/inputpath1.txt:0+58
  65. hadoop@hadoop:/home/hadoop/blb$

FileSplit简单使用相关推荐

  1. 利用python画分形图_「分形」python简单的分形图片 - seo实验室

    分形 康托集 # 康托集 import pygame pygame.init() screen = pygame.display.set_caption('康托集') screen = pygame. ...

  2. 简单的MapReduce实践

    简单的MapReduce实践 文章目录 简单的MapReduce实践 操作环境 实现文件合并和去重操作 新建项目 新建Java程序 打包程序 运行程序 实现文件的倒排索引 第一步,Map 第二步,Co ...

  3. eclipse创建神经网络_使用Eclipse Deeplearning4j构建简单的神经网络

    eclipse创建神经网络 神经网络导论 深度学习包含深度神经网络和深度强化学习,它们是机器学习的子集,而机器学习本身就是人工智能的子集. 广义地说,深度神经网络执行机器感知,该机器感知从原始数据中提 ...

  4. 使用Eclipse Deeplearning4j构建简单的神经网络

    神经网络导论 深度学习既包含深度神经网络又包含深度强化学习,这是机器学习的子集,而机器学习本身就是人工智能的子集. 广义上讲,深度神经网络执行机器感知,该机器感知从原始数据中提取重要特征,并对每个观察 ...

  5. java FileSplit类

    来源与去向 通过inputformat的getsplits方法产生 传递给inputformat的createRecordReader方法. /** A section of an input fil ...

  6. C/C++简单实现文件分块

    C语言简单实现文件分块 模块1:分割文件 指定目标输入文件(文件名或文件路径)和分割尺寸,要求分割尺寸(单位:MB)为正整数,且范围在[MIN_SIZE, MAX_SIZE]. 分割后产生块文件,命名 ...

  7. 在docker上安装部署tomcat项目 超简单,拿来主义

    在docker中部署tomcat,非常简单,而且省去了手动安装jdk等步骤,只需要将war包复制在容器tomcat实例中的webapps下面即可.以下将详细讲解流程: 在windows中打好包以后用w ...

  8. Linux下tomcat的安装与卸载以及配置(超简单)

    无敌简单的几步 1.安装 //首先你需要下载好tomcat包 sudo tar -xvzf apache-tomcat-7.0.85.tar.gz(这里是包名) -C 你要放的位置 2.卸载 rm - ...

  9. Docker安装Apache与运行简单的web服务——httpd helloworld

    Docker运行简单的web服务--httpd helloworld目录[阅读时间:约5分钟] 一.Docker简介 二.Docker的安装与配置[CentOS环境] 三.Docker运行简单的web ...

  10. Docker的安装、镜像源更换与简单应用

    Docker的安装.镜像源更换与简单应用[阅读时间:约20分钟] 一.概述 二.系统环境&项目介绍 1.系统环境 2.项目的任务要求 三.Docker的安装 四.Docker的简单应用 1. ...

最新文章

  1. CCNA 第一章 网际互联
  2. 《周志华机器学习详细公式推导版》发布,Datawhale开源项目pumpkin-book
  3. php 不可以连接远程mysql数据库
  4. mysql-主从服务器同步搭建
  5. MIT媒体实验室主任辞去一切职务:拿了爱泼斯坦170万美金,涉及程序违规,麻省理工宣布彻查...
  6. 文件上传 java web_JavaWeb 文件上传下载
  7. 【python】排序算法的稳定性冒泡排序(画图详细讲解)
  8. [日常] DNS的迭代查询过程
  9. shell:读取文件的每一行内容并输出
  10. java 月份_java+javascript获得两个日期之间的所有月份
  11. [C++] C++ Primer 笔记
  12. React学习整理(一):React 安装
  13. svn checkout 提示“由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。”解决方法...
  14. 第四次作业:猫狗大战挑战赛
  15. winform 线程 句柄不断增加_多线程讲解
  16. 软件工程的极端所有权
  17. 如何录制网络视频,屏幕录制软件哪个好
  18. steam服务器连接不稳定WIN10,小编操作win10系统steam连接不稳的解决步骤
  19. hexo博客中如何插入图片
  20. Java实现微信授权登录

热门文章

  1. 计算机频率原理,频率计数器的工作原理和发展
  2. matlab电压模块,matlab simpowersystems电路仿真模块.doc
  3. 甄零一诺合同——专注合同信息化管理
  4. 计算机应用基础教案 电子书,计算机应用基础教案(全套)-20210511075659.pdf-原创力文档...
  5. 儿童时间管理表,让孩子学会善待时间
  6. 菲尼克斯电源模块UNO-PS1AC24DC100W的组装
  7. 基于 软件体系结构(第3版)考试重点和复习指南
  8. Flash 短片轻松学
  9. WTL入门(五) 自定义控件
  10. 可用性和可靠性的区别