这篇文章主要介绍“WordCount怎么实现单词计数”,在日常操作中,相信很多人在WordCount怎么实现单词计数问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”WordCount怎么实现单词计数”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
创新互联公司是一家专注于网站制作、成都网站建设与策划设计,榆树网站建设哪家好?创新互联公司做网站,专注于网站建设10多年,网设计领域的专业建站公司;建站业务涵盖:榆树等地区。榆树做网站价格咨询:18982081108
一:
public class WordCount { public static class TokenizerMapper extends Mapper
二:
public class WordCount1 { public static final IntWritable ONE = new IntWritable(1); public static class WordCountMapper extends Mapper{ @Override protected void map(LongWritable key, Text value, Mapper .Context context) throws IOException, InterruptedException { String[] vs = value.toString().split("\\s"); for (String string : vs) { context.write(new Text(string) , ONE); } } } public static class WordCountReduce extends Reducer { @Override protected void reduce(Text key, Iterable values, Reducer .Context context) throws IOException, InterruptedException { int count =0; for (IntWritable v : values) { count += v.get(); } context.write(key,new IntWritable(count) ); } } public static void main(String[] args) { try { Configuration conf = new Configuration(); Job job = Job.getInstance(conf,"word count"); job.setJarByClass(WordCount1.class); //实现map函数,根据输入的 对生成中间结果。 job.setMapperClass(WordCountMapper.class); job.setMapOutputKeyClass(Text.class); job.setMapOutputValueClass(IntWritable.class); job.setReducerClass(WordCountReduce.class); FileInputFormat.addInputPath(job, new Path("hdfs://192.168.226.129:9000/rootdir/mapreduce.txt")); FileOutputFormat.setOutputPath(job, new Path("hdfs://192.168.226.129:9000/rootdir/data/studytest/"+System.currentTimeMillis()+"/")); System.exit(job.waitForCompletion(true) ? 0 : 1); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } }
例如:
输入:
xujun xujun xj , cxj xxxx cccc ddd xujun xj yyy jjj ccc yyy
输出:
, 1 ccc 1 cccc 1 cxj 1 ddd 1 jjj 1 xj 2 xujun 3 xxxx 1 yyy 2
到此,关于“WordCount怎么实现单词计数”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!