504. Inverted Index (Map Reduce) lintcode
https://www.lintcode.com/problem/inverted-index-map-reduce/description -- decription of the map reduce problem
1. click the submit button to view the problem.
2. logic of map reduce, each time, they only deal with one key value pair (for map and reduce).
given two documents as follows:
[{"id":1,"content":"This is the content of document1"}
{"id":2,"content":"This is the content of document2"}]
after map:
This 1, is 1, .. This 2, is 2,
hidden shuffle(sort and transport), how does it sort, accorind key or pair??
after reduce(merge) -- before reduce, already have the iterator of id
This <1,2>, is <1,2>;
Cautious!!!!!!!!!! if they are repeated element or duplicate , you probably get the <1,1,2>, if the appears twice in first docemnet.
solution -- check the prev and cur in the reduce of the value .
code
public class InvertedIndex {
public static class Map {
public void map(String key, Document value,
OutputCollector<String, Integer> output) {
// Write your code here
// Output the results into output buffer.
int id = value.id;
String content = value.content;
String[] words = content.split("\\s+");
//System.out.println(words[0]);
if(words.length<=0) return ;
//what if duplicate StackTraceElement
for(int i = 0; i<words.length; i++){
output.collect(words[i], id);
}
// Ps. output.collect(String key, int value);
}
}
public static class Reduce {
public void reduce(String key, Iterator<Integer> values,
OutputCollector<String, List<Integer>> output) {
// Write your code here
// Output the results into output buffer.
List<Integer> res = new ArrayList<>();
int prev = -1;
while(values.hasNext()){
int now = values.next();
if(prev!=now)
res.add(now);
prev = now;
}
output.collect( key, res);
// Ps. output.collect(String key, List<Integer> value);
}
}
}
skills:
iterator<Integer> iter = new ..
iter.hasNext(); iter.next()
string.split("\\s+")
504. Inverted Index (Map Reduce) lintcode的更多相关文章
- paip.提升效率---filter map reduce 的java 函数式编程实现
#paip.提升效率---filter map reduce 的java 函数式编程实现 ======================================================= ...
- lodash用法系列(4),使用Map/Reduce转换
Lodash用来操作对象和集合,比Underscore拥有更多的功能和更好的性能. 官网:https://lodash.com/引用:<script src="//cdnjs.clou ...
- 第一个map reduce程序
完成了第一个mapReduce例子,记录一下. 实验环境: hadoop在三台ubuntu机器上部署 开发在window7上进行 hadoop版本2.2.0 下载了hadoop-eclipse-plu ...
- Python中的Map/Reduce
MapReduce是一种函数式编程模型,用于大规模数据集(大于1TB)的并行运算.概念"Map(映射)"和"Reduce(归约)",是它们的主要思想,都是从函数 ...
- 高阶函数 filter map reduce
const app=new Vue({ el:'#app', data:{ books:[{ id:1, name:"算法导论", data: '2006-1', price:39 ...
- 499 单词计数 (Map Reduce版本)
原题网址:https://www.lintcode.com/problem/word-count-map-reduce/description 描述 使用 map reduce 来计算单词频率http ...
- 图解kubernetes scheduler基于map/reduce无锁设计的优选计算
优选阶段通过分离计算对象来实现多个node和多种算法的并行计算,并且通过基于二级索引来设计最终的存储结果,从而达到整个计算过程中的无锁设计,同时为了保证分配的随机性,针对同等优先级的采用了随机的方式来 ...
- 图解kubernetes scheduler基于map/reduce模式实现优选阶段
优选阶段通过分map/reduce模式来实现多个node和多种算法的并行计算,并且通过基于二级索引来设计最终的存储结果,从而达到整个计算过程中的无锁设计,同时为了保证分配的随机性,针对同等优先级的采用 ...
- MapReduce剖析笔记之三:Job的Map/Reduce Task初始化
上一节分析了Job由JobClient提交到JobTracker的流程,利用RPC机制,JobTracker接收到Job ID和Job所在HDFS的目录,够早了JobInProgress对象,丢入队列 ...
随机推荐
- hdu3068 求一个字符串中最长回文字符串的长度 Manacher算法
最长回文 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 你不得不知道的5个神奇的Docker工具
Docker社区非常活跃,每天都会推出大量有用的工具.要想持续追踪社区中发生的各项创新其实非常困难.为了帮助你,我收集了一些每天在日常工作中使用.令人感兴趣并且十分有用的Docker工具.这些工具消除 ...
- Linux 网络配置及常用服务配置(Redhat 6)
一.网络配置 1. VMWare 提供了三种网络工作模式供用户选择,他们分别是, ①Bridged(桥接模式): 如果网络中能提供多个IP地址,则使用桥接方式 ②NAT(网络地址转换模式): 如果网络 ...
- SvcUtil.exe导入WCF
1.SvcUtil.exe路径 vs2012.2013为C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\bin 2.添加外部工具 vs-工具-- ...
- 正确理解ThreadLocal:ThreadLocal中的值并不一定是完全隔离的
首先再讨论题主的这个观点之前我们要明确一下ThreadLocal的用途是什么? ThreadLocal并不是用来解决共享对象的多线程访问问题. 看了许多有关ThreadLocal的博客,看完之后会给人 ...
- toTop插件(三)
前言 当窗体内容过多会出现滚动, 点击回到顶部滚动条在在上边(大家都懂得,我语文学的不好,表达不清^_^) 看代码 CSS : .toTop{ position: fixed; width: 50px ...
- Win7 IIS 局域网中无法访问网页
安装好iis后,在局域网中无法浏览网页一,关闭防火墙即可 或者建立入站规则 打开控制面板——window防火墙——高级设置 在入站规则上右键新建入站规则,选择端口然后下一步 选择tcp和特定端口在端口 ...
- Android NDK开发 Android Studio使用新的Gradle构建工具配置NDK环境(一)
本文主要讲述了如何如何在Android Studio使用新的Gradle构建工具配置NDK环境,现在把相关的步骤整理出来分享给Android程序员兄弟们,希望给他们在配置NDK环境时带来帮助. 从An ...
- maya 安装不上
AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...
- [转]jQuery: get table column/row index remove table column (by column number)
本文转自:http://www.xinotes.org/notes/note/1087/ <!DOCTYPE html><html><head> <title ...