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的更多相关文章

  1. paip.提升效率---filter map reduce 的java 函数式编程实现

    #paip.提升效率---filter map reduce 的java 函数式编程实现 ======================================================= ...

  2. lodash用法系列(4),使用Map/Reduce转换

    Lodash用来操作对象和集合,比Underscore拥有更多的功能和更好的性能. 官网:https://lodash.com/引用:<script src="//cdnjs.clou ...

  3. 第一个map reduce程序

    完成了第一个mapReduce例子,记录一下. 实验环境: hadoop在三台ubuntu机器上部署 开发在window7上进行 hadoop版本2.2.0 下载了hadoop-eclipse-plu ...

  4. Python中的Map/Reduce

    MapReduce是一种函数式编程模型,用于大规模数据集(大于1TB)的并行运算.概念"Map(映射)"和"Reduce(归约)",是它们的主要思想,都是从函数 ...

  5. 高阶函数 filter map reduce

    const app=new Vue({ el:'#app', data:{ books:[{ id:1, name:"算法导论", data: '2006-1', price:39 ...

  6. 499 单词计数 (Map Reduce版本)

    原题网址:https://www.lintcode.com/problem/word-count-map-reduce/description 描述 使用 map reduce 来计算单词频率http ...

  7. 图解kubernetes scheduler基于map/reduce无锁设计的优选计算

    优选阶段通过分离计算对象来实现多个node和多种算法的并行计算,并且通过基于二级索引来设计最终的存储结果,从而达到整个计算过程中的无锁设计,同时为了保证分配的随机性,针对同等优先级的采用了随机的方式来 ...

  8. 图解kubernetes scheduler基于map/reduce模式实现优选阶段

    优选阶段通过分map/reduce模式来实现多个node和多种算法的并行计算,并且通过基于二级索引来设计最终的存储结果,从而达到整个计算过程中的无锁设计,同时为了保证分配的随机性,针对同等优先级的采用 ...

  9. MapReduce剖析笔记之三:Job的Map/Reduce Task初始化

    上一节分析了Job由JobClient提交到JobTracker的流程,利用RPC机制,JobTracker接收到Job ID和Job所在HDFS的目录,够早了JobInProgress对象,丢入队列 ...

随机推荐

  1. CodeForces - 984C——Finite or not?分数整除问题(数论,gcd)

    题目传送门 题目描述:给你一个p/q,让你求在b进制下,这个小数是不是有限小数. 思路: 先来膜拜一个大神的博客,如何求小数的二进制表达,(感谢博主肘子zhouzi).然后小数的其他进制表达也一样. ...

  2. jQuery中animate()方法以及$('body').animate({"scrollTop":top})不被Firefox支持问题的解决

    $("body").animate({"scrollTop":top}): 只被chrome支持,而不被Firefox支持 $("html" ...

  3. Linux内核硬件访问技术

    ① 驱动程序控制设备,主要是通过访问设备内的寄存器来达到控制目的.因此我们讨论如何访问硬件,就成了如何访问这些寄存器. ② 在Linux系统中,无论是内核程序还是应用程序,都只能使用虚拟地址,而芯片手 ...

  4. win安装wordcloud报错解决方案

    可以知道需要安装的是32位系统上的python 3.6.5版本的 ②登陆 https://www.lfd.uci.edu/~gohlke/pythonlibs/#wordcloud 网址找到相应文件 ...

  5. 1.Exadata技术演进

    V1-v2 和 HP Exadata 2-2 和 SUN 2011 3-2 4-2 5-2 2014底 2-2 混合运算 2-8 是大数据运算         问题1. 随着系统规模增加,传统数据库架 ...

  6. 分配一维动态数组or 二维动态数组的方法以及学习 new 方法or vector

    先来个开胃菜 // 使用new动态分配存储空间 #include<iostream> using std::cout; int main() { // 第1种方式 int *a=new i ...

  7. httpUrlConnection连接网络的用法(用到了handle传递消息,在主线程中更新UI)

    由于httpclient在Android5.0以后已经过时,所以官方推荐使用httpUrlConnection来连接网络,现将该连接的基本方法展示,如下 注意:记得加入<uses-permiss ...

  8. LeetCode 303.区域检索-数组不可变(accumulate()和for循环差异分析)

    给定一个整数数组  nums,求出数组从索引 i 到 j  (i ≤ j) 范围内元素的总和,包含 i,  j 两点. 示例: 给定 nums = [-2, 0, 3, -5, 2, -1],求和函数 ...

  9. jQuery源代码学习_工具函数_type

    jquery源代码学习_工具函数_type jquery里面有一个很重要的工具函数,$.type函数用来判断类型,今天写这篇文章,是来回顾type函数的设计思想,深入理解. 首先来看一下最终结果: 上 ...

  10. jquery中load()加载页面,刷新之后,加载的页面不显示的解决办法

    <script language="javascript" type="text/javascript"> $(function(){ $(&quo ...