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. Flowerpot(又是尺取。。)

    题目:http://172.21.85.56/oj/exercise/problem?problem_id=21568 题目大意:老板需要你帮忙浇花.给出N滴水的坐标,y表示水滴的高度,x表示它下落到 ...

  2. ajax加载菊花loading效果

    Ajax异步请求的时候,一般都会利用一个动态的gif小图片来制作一个Ajax Loading,以便增加用户体验. 这里我们可以使用Spin.js,该js脚本压缩后5k,可以不用任何图片,任何外部CSS ...

  3. Dev GridView RowCellClick事件与MouseDown事件

    GridView处于可编辑状态,左键点击默认为“进入编辑”. 将GridView的OptionsColumn.AllowEdit设置为false后左键可触发RowCellClick.但有时候,既希望G ...

  4. Tcpdump命令抓包详细分析

    1 起因 前段时间,一直在调线上的一个问题:线上应用接受POST请求,请求body中的参数获取不全,存在丢失的状况.这个问题是偶发性的,大概发生的几率为5%-10%左右,这个概率已经相当高了.在排查问 ...

  5. vmware 三种连接方式 如何设置独立ip

    转载:https://blog.csdn.net/xiaoyangsavvy/article/details/73718473

  6. Python 内置的GUI库tkinter方法在py2和py3中的更改

    参考资料:   https://docs.python.org/3.4/library/tkinter.html#tkinter-moduleshttps://docs.python.org/2.7/ ...

  7. mgo03_linux7上安装mongo4.0

    下载地址https://www.mongodb.com/download-center#community tar -xvf mongodb-linux-x86_64-rhel70-4.0.0.tgz ...

  8. getResourceAsStream小结

    前提:我用的是gradle工程,文件放在resource下,resource对应的就是类路径,文件的路径和代码的路径保持一致,如Client的包名和peizhi.properties一致,例如Clie ...

  9. vue--钩子函数1

    最近在学习自定义指令,这里做个整理 vue允许注册自定义指令,在2.0中,代码复用和抽象的主要形式是组件.然而有的情况下仍需要对普通DOM元素进行底层操作,这时就会用到自定义指令. 全局指令direc ...

  10. Unity3d 破解

    在官网上或者其他地方下载unity3d后 运行注册机,需要详细的设置可以点击edit   一.点击browse,选择安装unity目录下的Editor文件夹 二.选择path,会提示Pathed 三. ...