[Java] 数据分析 -- 大数据
单词计数
- 需求:输入小说文本,输出每个单词出现的次数
- 实现:分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] 数据分析 -- 大数据的更多相关文章
- 一句话了解JAVA与大数据之间的关系
大数据无疑是目前IT领域的最受关注的热词之一.几乎凡事都要挂上点大数据,否则就显得你OUT了.如果再找一个可以跟大数据并驾齐驱的IT热词,JAVA无疑是跟大数据并驾齐驱的一个词语.很多人在提到大数据的 ...
- python、Java、大数据和Android的薪资如何?
莫名其妙,从去年年底开始,Python这个东西在中国,突然一下子就火起来了,直至现在,他的热度更是超越了java,成为软件工程师最为关注的话题.Python之所以能火起来,很大一方面是因为大数据.人工 ...
- 1月中旬值得一读的10本技术新书(机器学习、Java、大数据等)!
1月中旬,阿里云云栖社区 联合 博文视点 为大家带来十本技术书籍(机器学习.Java.大数据等).以下为书籍详情,文末还有福利哦! 书籍名称:Oracle数据库问题解决方案和故障排除手册 内容简介 & ...
- 毕业生、程序猿转岗该如何选择Java、大数据和VR?
许久不见的朋友请我吃饭,期间给我介绍他一个弟弟,说明年要毕业了,还不知道找啥工作,说有培训机构让他学VR.大数据什么的,不知道前景咋样,想咨询一下我.相信很多朋友面临毕业,都不知道该从事哪个行业,自己 ...
- 毕业生、程序猿转岗该如何选择Java、大数据和VR?答案在这里!
许久不见的朋友请我吃饭,期间给我介绍他一个弟弟,说明年要毕业了,还不知道找啥工作,说有培训机构让他学VR.大数据什么的,不知道前景咋样,想咨询一下我.相信很多朋友面临毕业,都不知道该从事哪个行业,自己 ...
- Java转大数据开发全套视频资料
大数据在近两年可算是特别火,有很多人都想去学大数据,有java转大数据的,零基础学习大数据的.但是大数据真的好学吗. 我们先来了解一下什么是大数据. 大数据是指无法在一定时间内用常规软件工具对其内容进 ...
- java 与大数据学习较好的网站
C# C#中 Thread,Task,Async/Await,IAsyncResult 的那些事儿!https://www.cnblogs.com/doforfuture/p/6293926.html ...
- Java转型大数据开发全套教程,都在这儿!
众所周知,很多语言技术已经在长久的历史发展中掩埋,这期间不同的程序员也走出的自己的发展道路. 有的去了解新的发展趋势的语言,了解新的技术,利用自己原先的思维顺利改变自己的title. 比如我自己,也都 ...
- java excel大数据量导入导出与优化
package com.hundsun.ta.utils; import java.io.File; import java.io.FileOutputStream; import java.io.I ...
随机推荐
- windows一些知识
宽字节 1.什么是ASCII码? 一张存储了字母大小写与一些符号的表,用一个字节表示,最高位不使用,最多只能存储128个符号或字母,但世界上有很多种语言,这远远不够 2.什么是扩展ASCII码? 把最 ...
- ls:未找到命令
解决,别问为什么. 执行 export PATH=/bin:/usr/bin:$PATH
- CrashLoopBackOff的解决办法之一
问题来源 # kubectl get pods -n assembly NAME READY STATUS RESTARTS AGE alertmanager-858b7749c5-6jsfh 0/1 ...
- Scrapy框架的安装
Win+R 输入cmd打开命令行 我们先把pip升级到最新版,输入代码如下: pip install --upgrade pip 不过一般这种更新方式会经常性出错,安装文件在下载到一半时就会超时报错 ...
- 使用 nodejs 中的 http 模块实现几个超实用的工具
nodejs 方便了我们前端开发者进行一些服务端上的操作,可以进行无缝地衔接.像其他一些后端语言,如 php, golang, java 等,都需要一定的学习成本,而 nodejs 则就是为前端开发者 ...
- pandas(3):索引Index/MultiIndex
目录 一.索引概念 二.创建索引 ①导入数据时指定索引 ②导入数据后指定索引df.set_index() 三.常用的索引属性 四.常用索引方法 五.索引重置reset_index() 六.修改索引值( ...
- Java常用工具+类库合集
1 常用工具 JVisual vm:可以直接通过软件包下载,支持本地以及远程JVM监控 JMH:Java Microbenchmark Harness,测试基准组件,精度可达纳秒级 JITWatch: ...
- ssh+scp基本使用
1 ssh ssh一般用于连接服务器,可以使用密码认证与密钥认证的方式. 1.1 密码认证 直接使用ssh即可: ssh username@xxx.xxx.xxx.xxx username为用户名,后 ...
- 8. vue给标签动态绑定title
在利用vue开发时,如果标签宽度比较小,我们需要利用overflow:hidden;text-overflow:ellipsis;white-space: nowrap;对其进行隐藏,但隐藏后如何读其 ...
- Ionic5沉浸式状态栏 适配全面屏
1. 在platforms/android/app/src/main目录中找到AndroidManifest.xml文件,修改文件中manifest → application → activity标 ...