成都网站建设设计

将想法与焦点和您一起共享

实现WordCount的方法有哪些

这篇文章将为大家详细讲解有关实现WordCount的方法有哪些,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

成都创新互联公司专注于白朗网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供白朗营销型网站建设,白朗网站制作、白朗网页设计、白朗网站官网定制、重庆小程序开发服务,打造白朗网络公司原创品牌,更为您提供白朗网站排名全网营销落地服务。

1、精简的Shell

cat  /home/sev7e0/access.log | tr -s ' ' '\n' | sort | uniq -c | sort -r | awk '{print $2, $1}'
#cat  命令一次性展示出文本内容
#tr -s ' ' '\n'  将文本中空格使用回车键替换
#sort   串联排序所有指定文件并将结果写到标准输出。
#uniq -c    从输入文件或者标准输入中筛选相邻的匹配行并写入到输出文件或标准输出,-c 在每行前加上表示相应行目出现次数的前缀编号
#sort | uniq -c     同时使用用来统计出现的次数
#sort -r    把结果逆序排列
#awk '{print $2,$1}'    将结果输出,文本在前,计数在后

2、反人类的MapReduce

//mapreduce方式
public static void main(String[] args) throws Exception {

    Configuration conf = new Configuration();
//        conf.set("fs.defaultFS", "hdfs://spark01:9000");
//        conf.set("yarn.resourcemanager.hostname", "spark01");

    Path out = new Path(args[1]);
    FileSystem fs = FileSystem.get(conf);

    //判断输出路径是否存在,当路径存在时mapreduce会报错
    if (fs.exists(out)) {
        fs.delete(out, true);
        System.out.println("ouput is exit  will delete");
    }
    
    // 创建任务
    Job job = Job.getInstance(conf, "wordcountDemo");
    // 设置job的主类
    job.setJarByClass(WordCount.class); // 主类

    // 设置作业的输入路径
    FileInputFormat.setInputPaths(job, new Path(args[0]));

    //设置map的相关参数
    job.setMapperClass(WordCountMapper.class);
    job.setMapOutputKeyClass(Text.class);
    job.setMapOutputValueClass(LongWritable.class);
    
    //设置reduce相关参数
    job.setReducerClass(WordCountReduce.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(LongWritable.class);

    //设置作业的输出路径
    FileOutputFormat.setOutputPath(job, out);
    job.setNumReduceTasks(2);
    System.exit(job.waitForCompletion(true) ? 0 : 1);
}

3、好用的spark

//spark版wordcount
sc.textFile("/home/sev7e0/access.log").flatMap(_.split(" ")).map((_, 1)).reduceByKey(_+_).foreach(println(_))

关于实现WordCount的方法有哪些就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


网站题目:实现WordCount的方法有哪些
分享URL:http://chengdu.cdxwcx.cn/article/piepje.html