当前位置: 首页 > news >正文

博物馆网站开发做网站乱码

博物馆网站开发,做网站乱码,做网站后台怎么搭建,东莞做网站乐云seo文章目录 自定义InputFormat1. 需求2. 输入数据3. 期望输出文件格式4. 需求分析5. 代码实现WholeFileInputformatWholeRecordReader☆☆SequenceFileMapperSequenceFileReducer ☆ 自定义InputFormat 在企业开发中,Hadoop框架自带的InputFormat类型不能满足所有应用…

文章目录

    • 自定义InputFormat
        • 1. 需求
        • 2. 输入数据
        • 3. 期望输出文件格式
        • 4. 需求分析
        • 5. 代码实现
            • WholeFileInputformat
            • WholeRecordReader☆☆
            • SequenceFileMapper
            • SequenceFileReducer

自定义InputFormat

在企业开发中,Hadoop框架自带的InputFormat类型不能满足所有应用场景,需要自定义InputFormat来解决实际问题。

自定义InputFormat步骤如下

  1. 自定义一个类继承FileInputFormat。
  2. 改写RecordReader,实现一次读取一个完整文件封装为KV。
  3. 在输出时使用SequenceFileOutPutFormat输出合并文件

无论HDFS还是MapReduce,在处理小文件时效率都非常低,但又难免面临处理大量小文件的场景,此时,就需要有相应解决方案。可以自定义InputFormat实现小文件的合并。

1. 需求

将多个小文件合并成一个SequenceFile文件(SequenceFile文件是Hadoop用来存储二进制形式的key-value对的文件格式),SequenceFile里面存储着多个文件,存储的形式为文件路径+名称为key,文件内容为value

2. 输入数据

在这里插入图片描述

3. 期望输出文件格式

在这里插入图片描述

4. 需求分析

在这里插入图片描述

5. 代码实现
WholeFileInputformat
/*** @Date 2020/7/9 23:10* @Version 10.21* @Author DuanChaojie*/
public class WholeFileInputformat extends FileInputFormat<Text,BytesWritable> {@Overrideprotected boolean isSplitable(JobContext context, Path filename) {return false;}@Overridepublic RecordReader<Text, BytesWritable> createRecordReader(InputSplit split, TaskAttemptContext context) throws IOException, InterruptedException {WholeRecordReader recordReader = new WholeRecordReader();recordReader.initialize(split,context);return recordReader;}
}
WholeRecordReader☆☆
/*** @Date 2020/7/9 23:23* @Version 10.21* @Author DuanChaojie*/
public class WholeRecordReader extends RecordReader {private Configuration conf;private FileSplit split;private boolean isProgress= true;private Text k = new Text();private BytesWritable v = new BytesWritable();@Overridepublic void initialize(InputSplit split, TaskAttemptContext context) throws IOException, InterruptedException {this.split = (FileSplit)split;conf = context.getConfiguration();}/*** 核心逻辑* @return* @throws IOException* @throws InterruptedException*/@Overridepublic boolean nextKeyValue() throws IOException, InterruptedException {if (isProgress) {// 1.定义缓冲区byte[] contents = new byte[(int) split.getLength()];FileSystem fs = null;FSDataInputStream fis = null;try {// 2. 获取文件系统Path path = split.getPath();fs = path.getFileSystem(conf);// 3.读取数据fis = fs.open(path);// 4.读取文件内容IOUtils.readFully(fis, contents, 0, contents.length);// 5.输出文件内容v.set(contents,0,contents.length);// 6.获取文件路径及名称String name = split.getPath().toString();// 7,设置输出的key值k.set(name);} catch (Exception e) {e.printStackTrace();}finally {IOUtils.closeStream(fis);}isProgress = false;return true;}return false;}@Overridepublic Object getCurrentKey() throws IOException, InterruptedException {return k;}@Overridepublic Object getCurrentValue() throws IOException, InterruptedException {return v;}@Overridepublic float getProgress() throws IOException, InterruptedException {return 0;}@Overridepublic void close() throws IOException {}
}
SequenceFileMapper
/*** @Date 2020/7/9 23:36* @Version 10.21* @Author DuanChaojie*/
public class SequenceFileMapper extends Mapper<Text, BytesWritable, Text,BytesWritable> {@Overrideprotected void map(Text key, BytesWritable value, Context context) throws IOException, InterruptedException {context.write(key,value);}
}
SequenceFileReducer
public class SequenceFileReducer extends Reducer<Text, BytesWritable, Text,BytesWritable> {@Overrideprotected void reduce(Text key, Iterable<BytesWritable> values, Context context) throws IOException, InterruptedException {context.write(key,values.iterator().next());}
}
public class SequenceFileDriver {public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {// 输入输出路径需要根据自己电脑上实际的输入输出路径设置args = new String[] { "e:/file/input/inputinputformat", "e:/file/output2" };// 1 获取job对象Configuration conf = new Configuration();Job job = Job.getInstance(conf);// 2 设置jar包存储位置、关联自定义的mapper和reducerjob.setJarByClass(SequenceFileDriver.class);job.setMapperClass(SequenceFileMapper.class);job.setReducerClass(SequenceFileReducer.class);// 3 设置map输出端的kv类型job.setMapOutputKeyClass(Text.class);job.setMapOutputValueClass(BytesWritable.class);// 4 设置最终输出端的kv类型job.setOutputKeyClass(Text.class);job.setOutputValueClass(BytesWritable.class);// 5 设置输入输出路径FileInputFormat.setInputPaths(job, new Path(args[0]));FileOutputFormat.setOutputPath(job, new Path(args[1]));// 6设置输入的inputFormatjob.setInputFormatClass(WholeFileInputformat.class);// 7设置输出的outputFormatjob.setOutputFormatClass(SequenceFileOutputFormat.class);// 8提交jobboolean result = job.waitForCompletion(true);System.exit(result ? 0 : 1);}
}

完全与期望输出结果一致!

http://www.yayakq.cn/news/842173/

相关文章:

  • 用v9做的网站上传服务器最流行的网络营销方式
  • 毕业设计可以做哪些简单网站央企网站群建设中标公告
  • 新网 网站备案杭州网站建设图片
  • 海南省建设集团有限公司网站seo整站优化服务
  • 重庆网站建设大概需要多少钱企业网站带手机源码
  • 淘客网站推广免备案长沙官网网站制作公司
  • 如何查询网站接入商十堰网络公司排行榜
  • 北京通州网站制作公司带动画的网站模板
  • 网站建设流北京模板网站开发全包
  • 淄博网站建设 招聘微信朋友圈推广
  • 福建省建设系统网站国际电商怎么做
  • 网站改版 升级的目的是什么广告创意与设计
  • 网站建设方面的书籍推荐建设外包网站
  • 国家示范校建设成果网站常熟做网站公司
  • 如果自己做网站设计网站企业网站建设公司
  • 网站建设需要哪些岗位上海网站关键词优化方法
  • 海口企业建站系统模板长春网站制作招聘信息
  • 售后软件网站开发wordpress单栏主题
  • 忻州宁武网站建设邯郸微信小程序制作公司
  • 建设项目验收在哪个网站公示wordpress主题菜单
  • 三网合一网站远吗icp备案查询怎么查询
  • 利用php做网站盱眙在仕德伟做网站的有几家
  • 网站搜索框宁波住房建设局网站
  • 一个jsp做的购物小网站佛山网站的建设
  • 网站开发与设计实训心得两千字服装定制官网
  • 揭阳网站定制建设网站找网络公司
  • 宁乡做网站wordpress生成客户端
  • 做网站的网络公司税收优惠微商城系统哪家强
  • 网站维护与排名新传奇网页游戏
  • 网站建站网站jp586 vipwordpress手动上传图片