淘宝数据仓库团队,在“HADOOP中一种非典型两表JOIN的处理方法”这篇文章里, 无私地 share 了他们的方法。我就着他们的写一个续,权当讨论,抛砖引玉。淘宝团队的文章主要说的是大规模数据情况下如何计算,我这篇接着他们最后的问题,即“多对多”的情况说一下思路。
要解决的问题可以简化描述一下:
- 有两组数据,input1 { P1, U1; P1, U2; P2, U3; P3, U4; P4, U4 },input2 { P1, C1; P1, C2; P2, C3; P3, C3; P3, C4; P4, C4 }。
- 要求执行类似于数据库两表 Inner Join 的操作,以 P 为 key,建立起 U 和 C 直接的对应关系,即最终结果为 output { U1, C1; U1, C2; U2, C1; U2, C2; U3, C3; U4, C3; U4, C4 }。
在数据库里,使用类似的 SQL 可以达到要求:SELECT DISTINCT(U, C) FROM input1 INNER JOIN input2 ON input1.P=input2.P。但如果要放在 Hadoop 里面求解,就需要动些脑筋了。
研究这个问题,首先需要理解 Hadoop 的运行机制。简单来讲,Hadoop 分为 Map 和 Reduce 两个操作:Map 操作将输入(如一行数据)格式化为 <key: value1><key: value2><key: value3> … <key: valueN>这样的一组结果,作为 Map 的输出。Hadoop 在 Map 和 Reduce 之间,会自动把 Map 的输出按照 key 合并起来,作为 Reduce 的输入。Reduce 得到这样一个 {key: [value1, value2, value3, ..., valueN]} 的输入之后,就可以进行自己的处理,完成最终计算了。
针对于我们这里要解决的问题,步骤如下。
- 将 Map 的输入构造为下面的格式:来自于 input1 的输入格式化为 {<input1, P1>: U1, U2};来自于 input2 的输入格式化为 {<input2, P1>: C1, C2}。
- 在 Map 操作内,将数据转化为 {P1: <input1, U1>},{P1: <input1, U2>},{P1: <input2, C1>},{P1: <input2, C2>},作为 Reduce 操作的输入。
- 经过 Hadoop 内部自己的操作,实际 Reduce 操作的输入为:{P1: <input1, U1>, <input1, U2>, <input2, C1>, <input2, C2>}。
- Reduce 里操作会复杂一下。首先需要执行一次 regroup,得到如下的结果 {<input1>: <input1, U1>, <input1, U2>; <input2>: <input2, C1>, <input2, C2>}。把这个结果拆开,可以得到两个集合:{<input1>, <input2>} 与 {[<input1, U1>, <input1, U2>], [<input2, C1>, <input2, C2>]}。
- 循环集合2,即可以得到最终结果。不过在 Reduce 里面作这个循环是需要一定技巧的,讲起来比较绕,大家就直接看后面的代码吧。
- 在此 Reduce 的结果之上,再跑一个 Map/Reduce,还可以得到 <U, C>的次数,作为每个组合的权重。
对于大数据量,需要启用 Hadoop 的数据压缩功能。
这是一个通用地解决 Inner Join 问题的思路,在 Hadoop 的 contrib package 里有具体的代码实现,参见 org.apache.hadoop.contrib.utils.join。
国内还有哪个 team 在用 Hadoop?欢迎交流!
读了一下 Google 关于 Blog Ranking 的 Patent,总结如下。
正面的指标:
- [0038] 订阅数
统计 blog 在各种 reader 中被订阅的数量。被订阅的越多,ranking 越高。但同时会使用一些方法处理“subscriptions spam”,诸如验证订制人和 IP 的唯一性。
- [0039] 搜索点击数
统计 blog 作为搜索结果时被点击的次数。点击次数越多,ranking 越高。
- [0040] 在其他 blogger 的 blogroll 里的出现次数
blogger 通常会使用 blogroll 来整理指到其他 blogger 的链接集合。统计所有 blogroll 中,指向某个 blog 的链接越多,ranking 越高。
- [0041] 来自高质量的 blogroll 的链接数
高质量的 blogroll 的链接大多都指向著名的或值得信任的 blog。
- [0042] 来自高质量的 blog 的 blogroll 的链接数
这里的假定是著名的或值得信任的 blogger 不会放指向 spam blog 的链接。
- [0043] 有Tag
blog 作者如果分析了 blog 内容,归类并打上了 tag,起码可以说明作者的态度比较认真。
- [0044] 来自邮件和聊天记录的链接数
如果在 Email 正文里或者聊天记录里出现了指向 blog 的链接,会加分。GEmail 和 Gtalk 被用在了这里。
- [0045] PageRank
PageRank 越高对应的 blog 也就越重要。考虑到blog的更新比较频繁,最新的 blog post 可能还没有PR。这时可以用对应的 blog 的 PR 来代替。
其中 [0040-0042],其实是类似于传统网页间 PageRank 计算的一套模式,只不过这里把它限制在了 blog 之间。
负面的指标:
- [0047] 更新频率异常
更新过于频繁或者非常有规律,会被认为是在 spam,ranking 会降低。这里提醒喜欢在每天的固定时间更新 blog 的朋友注意一下了。
- [0048] feed 内容和 blog 内容的不一致
spammer 有可能会为了提升自己的 ranking 而把有价值的内容放到 feed 里面,同时在 blog 内容里面放一些指向不相关内容的广告链接。为了惩罚这种情况,对于 feed 内容和 blog 内容不一致的情况,要降低 ranking。
- [0049] 出现重复内容
有些 spammer 为了让某些内容能够多次长时间的出现在 feed 里面,会重复发布同样的内容。这样的情况会被惩罚。
- [0050] 垃圾词过多
通过词频统计(bi-gram 或者 tri-gram 等),如果 blog 内容里垃圾词的比较过高,会降低 ranking。
- [0051] 多数 blog 长度相近
这个主要是针对使用机器自动生成 blog 的情况。
- [0052] 链接异常
当 blog 里的链接多为指向单一网页,或者单一的外站,会被认为是在 spam,ranking 会降低。
- [0053] 广告太多
如果一个 blog 页面内含有过多的广告,会降低 ranking。
- [0054] 广告出现在正文里
一般 blog 页面会包括三方面的内容:最近发表的 blog,blogroll 和 metadata。如果广告出现在正文里,会降低 ranking。不知道 adsense 的广告有没有特殊待遇?
最近忙,paper 看得多,blog 看得少,险些错过一些非常有意思的文章。上一次提到的 "Introduction to Google Search Quality" 算一篇,这次要说的是另外一篇 "Are Machine-Learned Models Prone to Catastrophic Errors?"。 不过这两个 blog 都被我们伟大的 GFW 拌掉了。
Peter Norvig 这样的大师的意见,我们需要仔细体会。我整理一下我感兴趣的。
- tow phase of google search algorithms
- An offline phase, which is time-consuming and query-independent.
- An on-line phrase, in response to a user query in a few milliseconds.
- Tons of training data … from the armies of "raters" employed by Google
- The big surprise is that Google still uses the manually-crafted formula for its search results, despite the fact that, their best machine-learned model is now as good as, and sometimes better than, the hand-tuned formula on the results quality metrics that Google uses.
- two reasons
- the human experts who created the algorithm believe they can do better than a machine-learned model
- Google's search team worries that machine-learned models may be susceptible to catastrophic errors on unforeseen query types, which is different from the training data.
- Nassim Taleb divides Black Swan phenomena into two classes
- The current generation of machine learning algorithms can work well in Mediocristan but not in Extremistan.
So the thing is, how to figure out whether new machine learning algorithms can be devised that work well in Extremistan, or prove that it cannot be done?