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对象,丢入队列 ...
随机推荐
- Go语言基础之19--web编程基础
一.web编程基础 1.1 web工作方式 1.2 HTTP协议详解 a.http 请求包体 GET /domains/example/ HTTP/1.1 //请求行: 请求方法 请求URI HTTP ...
- jinkens 检查svn更新就构建
以下的配置就是,svn上的文件一旦有变动,一分钟后就会触发jinkens的job(构建)
- python 读取文件使用chunksize后逐块迭代操作
chunkers=pd.read_csv('dd.csv',chunksize=10000) tot=pd.Series([]) for piece in chunkers: tot=tot.add( ...
- [转]分享20佳好玩的 jQuery 游戏
本文转自:http://www.cnblogs.com/lhb25/archive/2011/04/17/2001089.html jQuery是时下最流行的 JavaScript 库.现在,除了HT ...
- 转:自定义控件三部曲之动画篇——alpha、scale、translate、rotate、set的xml属性及用法
第一篇: 一.概述 Android的animation由四种类型组成:alpha.scale.translate.rotate,对应android官方文档地址:<Animation Resour ...
- pat1091. Acute Stroke (30)
1091. Acute Stroke (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue One impo ...
- c++ 面试整理
1. 继承方式 public 父类的访问级别不变 protected 父类的public成员在派生类编程protected,其余的不变 private 父类的所有成员变成pr ...
- eleme 项目使用到的库
探索eleme用到的库 xml re库 通过regex = re.compile(pattern)返回一个pattern对象, 通过该对象匹配正则表达式的字符串, 最好在模式中使用r'some'原始字 ...
- Python实现抓取CSDN热门文章列表
1.使用工具: Python3.5 BeautifulSoup 2.抓取网站: csdn热门文章列表 http://blog.csdn.net/hot.html 3.分析网站代码: 4.实现代码: _ ...
- C语言的各种输入情况介绍(ACM中常用到)
1.最简单的输入输出形式: 计算a+b的值: scanf("%d%d",&a,&b); printf("%d\n",a+b);--------- ...