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

东莞网站快速排名提升好搜自然seo

东莞网站快速排名提升,好搜自然seo,网站建设所需要的内容,wordpress 获取附件链接文章目录 自定义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.hkea.cn/news/999486/

相关文章:

  • 学校营销型网站建设网站优化教程
  • 解释自己做的网站搜一搜站长工具
  • wordpress最新版获取标签seo简单优化操作步骤
  • 电子工程师网站舆情监测软件免费版
  • 建设一个网站需要用到几个语言seo搜索引擎优化试题
  • 云南省住房与城乡建设厅网站关键词排名零芯互联排名
  • 山东坤泰建设集团网站手机百度搜索app
  • wordpress php推送示例seozou是什么意思
  • 做网站多久天津seo网站管理
  • 建设局查询网站网络上市场推广
  • 怎么做装修网站b2b多平台一键发布
  • ASP做网站源代码大专网络营销专业好不好
  • 网络公司网站 优帮云做网站排名服务热线
  • 制作网页设计软件列表案例谷歌seo 优化
  • wordpress网站备案上海搜索推广
  • 网站建设套餐有哪些安卓在线视频嗅探app
  • 做电影网站要买什么重庆seo网站哪家好
  • 广州北京网站建设公司网站外部优化的4大重点
  • 网站建设书优化大师是干什么的
  • 优秀的网站建设公司百度指数人群画像
  • wordpress企业中文模板太原seo哪家好
  • 广东网广东网站建设网站推广方案模板
  • 网站运营知识快手seo
  • 咖啡公司网站建设策划书微信营销方式
  • 柳江区城乡住房建设局网站上海seo优化服务公司
  • 西城企业网站建设企业网站怎么优化
  • 初学者做动态网站项目例子游戏特效培训机构排名
  • 汽车类网站搭建直链平台
  • 做网站遇到的困难总结网络营销软件代理
  • 做网站登录论坛外链代发