单词计数

  • 需求:输入小说文本,输出每个单词出现的次数
  • 实现:分map、combine、reduce三个阶段实现

  1 /*  Data Analysis with Java
2 * John R. Hubbard
3 * Aug 4, 2017
4 */
5
6 package com.hongfeng.Chapter11;
7
8 import java.io.File;
9 import java.io.IOException;
10 import java.io.PrintWriter;
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.HashMap;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Scanner;
17
18 public class Example1 {
19 public static void main(String[] args) {
20 try {
21 File tempFile = new File("data/Temp.dat");
22 map("data/sonnets/", 80, tempFile);
23
24 Map<String,StringBuilder> hashTable = new HashMap(2500);
25 combine(tempFile, hashTable);
26
27 File outFile = new File("data/Output.dat");
28 reduce(hashTable, outFile);
29 } catch (IOException e) {
30 System.err.println(e);
31 }
32 }
33
34 public static void map(String src, int n, File temp) throws IOException {
35 PrintWriter writer = new PrintWriter(temp);
36 for (int i = 0; i < n; i++) {
37 String filename = String.format("%sSonnet%03d.txt", src, i+1);
38 map(filename, writer);
39 }
40 writer.close();
41 }
42
43 public static void combine(File temp, Map<String,StringBuilder> table)
44 throws IOException {
45 Scanner scanner = new Scanner(temp);
46 while (scanner.hasNext()) {
47 String word = scanner.next();
48 StringBuilder value = table.get(word);
49 if (value == null) {
50 value = new StringBuilder("");
51 }
52 table.put(word, value.append(" 1"));
53 scanner.nextLine(); // scan past the rest of the line (a "1")
54 }
55 scanner.close();
56 }
57
58 public static void reduce(Map<String,StringBuilder> table, File out)
59 throws IOException {
60 PrintWriter writer = new PrintWriter(out);
61 for (Map.Entry<String, StringBuilder> entry : table.entrySet()) {
62 String key = entry.getKey(); // e.g., "speak"
63 String value = entry.getValue().toString(); // e.g., "1 1 1 1 1"
64 reduce(key, value, writer);
65 }
66 writer.close();
67 }
68
69
70 /* Writes the pair (word, 1) for each word in the specified file.
71 */
72 public static void map(String filename, PrintWriter writer)
73 throws IOException {
74 Scanner input = new Scanner(new File(filename));
75 input.useDelimiter("[.,:;()?!\"\\s]+");
76 while (input.hasNext()) {
77 String word = input.next();
78 writer.printf("%s 1%n", word.toLowerCase());
79 }
80 input.close();
81 }
82
83 /* Counts the 1s in the value argument and writes (key, count) to file.
84 */
85 public static void reduce(String key, String value, PrintWriter writer)
86 throws IOException {
87 int count = (value.length() + 1)/2; // e.g. "1 1 1 1 1" => 5
88 writer.printf("%s %d%n", key, count);
89 }
90
91 private static void sort(File file) throws IOException {
92 Scanner input = new Scanner(file);
93 List<String> list = new ArrayList();
94 while (input.hasNext()) {
95 list.add(input.nextLine());
96 }
97 input.close();
98 Collections.sort(list);
99 PrintWriter output = new PrintWriter(file);
100 for (String string : list) {
101 output.println(string);
102 }
103 output.close();
104 }
105 }

参考

java稀疏矩阵乘法

https://www.cnblogs.com/a1439775520/p/13074387.html

mapreduce实现稀疏矩阵乘法

https://my.oschina.net/ssrs2202/blog/494516?p=1

https://blog.csdn.net/liuxinghao/article/details/39958957

hashmap自定义键

https://blog.csdn.net/weixin_30502965/article/details/95265093

https://blog.csdn.net/weixin_33881426/article/details/112074005

https://blog.csdn.net/u011311291/article/details/87873756

https://blog.csdn.net/Revivedsun/article/details/96225010

[Java] 数据分析 -- 大数据的更多相关文章

  1. 一句话了解JAVA与大数据之间的关系

    大数据无疑是目前IT领域的最受关注的热词之一.几乎凡事都要挂上点大数据,否则就显得你OUT了.如果再找一个可以跟大数据并驾齐驱的IT热词,JAVA无疑是跟大数据并驾齐驱的一个词语.很多人在提到大数据的 ...

  2. python、Java、大数据和Android的薪资如何?

    莫名其妙,从去年年底开始,Python这个东西在中国,突然一下子就火起来了,直至现在,他的热度更是超越了java,成为软件工程师最为关注的话题.Python之所以能火起来,很大一方面是因为大数据.人工 ...

  3. 1月中旬值得一读的10本技术新书(机器学习、Java、大数据等)!

    1月中旬,阿里云云栖社区 联合 博文视点 为大家带来十本技术书籍(机器学习.Java.大数据等).以下为书籍详情,文末还有福利哦! 书籍名称:Oracle数据库问题解决方案和故障排除手册 内容简介 & ...

  4. 毕业生、程序猿转岗该如何选择Java、大数据和VR?

    许久不见的朋友请我吃饭,期间给我介绍他一个弟弟,说明年要毕业了,还不知道找啥工作,说有培训机构让他学VR.大数据什么的,不知道前景咋样,想咨询一下我.相信很多朋友面临毕业,都不知道该从事哪个行业,自己 ...

  5. 毕业生、程序猿转岗该如何选择Java、大数据和VR?答案在这里!

    许久不见的朋友请我吃饭,期间给我介绍他一个弟弟,说明年要毕业了,还不知道找啥工作,说有培训机构让他学VR.大数据什么的,不知道前景咋样,想咨询一下我.相信很多朋友面临毕业,都不知道该从事哪个行业,自己 ...

  6. Java转大数据开发全套视频资料

    大数据在近两年可算是特别火,有很多人都想去学大数据,有java转大数据的,零基础学习大数据的.但是大数据真的好学吗. 我们先来了解一下什么是大数据. 大数据是指无法在一定时间内用常规软件工具对其内容进 ...

  7. java 与大数据学习较好的网站

    C# C#中 Thread,Task,Async/Await,IAsyncResult 的那些事儿!https://www.cnblogs.com/doforfuture/p/6293926.html ...

  8. Java转型大数据开发全套教程,都在这儿!

    众所周知,很多语言技术已经在长久的历史发展中掩埋,这期间不同的程序员也走出的自己的发展道路. 有的去了解新的发展趋势的语言,了解新的技术,利用自己原先的思维顺利改变自己的title. 比如我自己,也都 ...

  9. java excel大数据量导入导出与优化

    package com.hundsun.ta.utils; import java.io.File; import java.io.FileOutputStream; import java.io.I ...

随机推荐

  1. Java学习之String与int的相互转换

    •String 转 int 两种方式 int a = Integer.parseInt(s);int b = Integer.valueOf(s).intValue(); 代码 public clas ...

  2. 第1课:Linux操作系统基础【DevOps基础培训】

    第1课:Linux操作系统基础 --DevOps基础培训 1. 云主机.公网IP 1.1 公网ip和私网ip 只有公网ip是能够连接互联网的,私网IP 一般只用作局域网 我们能够上网靠的是isp组织分 ...

  3. Python中切片的应用

    Python中切片的应用 Python中可以通过切片实现对列表或者字符串取指定范围的操作,实际就是通过对列表或者字符串通过索引进行操作. 具体细节点击廖雪峰Python教程,其中的课后小问题在此记录下 ...

  4. centos 7升级gcc到10.2.0

    安装gcc 由于 Linux 操作系统的自由.开源,在其基础上衍生出了很多不同的 Linux 操作系统,如 CentOS.Ubuntu.Debian 等.这些 Linux 发行版中,大多数都默认装有 ...

  5. Spring Boot demo系列(一):Hello World

    2021.2.24 更新 1 新建工程 打开IDEA选择新建工程并选择Spring Initializer: 可以在Project JDK处选择JDK版本,下一步是选择包名,语言,构建工具以及打包工具 ...

  6. Ambassador-09-prefix正则表达式

    设置 prefix_regex: true,即prefix就可以设置成正则表达式 --- apiVersion: getambassador.io/v2 kind: Mapping metadata: ...

  7. git推送代码报错:fatal: The current branch master has no upstream branch. To push the current branch and set the remote as upstream

    情景再现 远程新建仓库,然后本地 git bash执行以下代码 git init git add . git commit -m 'xxx' git remote add origin https:/ ...

  8. HUAWEI防火墙双出口根据链路优先级主备备份

    组网图形 组网需求 通过配置根据链路优先级主备备份,FW可以在主接口链路故障时,使用备份接口链路转发流量,提高传输的可靠性. 如图1所示,企业从ISP1租用2条链路,带宽均为50M,从ISP2租用1条 ...

  9. yolov2算法浅见

    因为最近在复习yolo系列的算法,就借着这个机会总结一下自己对这个算法的理解,由于是第一次写算法类的博客,文中有什么错误和行文不通的地方还希望大家指正. yolov2与yolov1有很多改变. 最重要 ...

  10. 866. Prime Palindrome

    Find the smallest prime palindrome greater than or equal to N. Recall that a number is prime if it's ...