用SPARK查找二度好友

鉴于上篇用mapreduce实现查找二度好友,代码偏多而且复杂。如果用spark来实现的话,可以大大减少代码量,如下是笔者的实现代码:

package bigdata.testspark

import org.apache.spark.SparkConf
import org.apache.spark.SparkContext

object FindDegree2Friends {
  var files = "/home/typery/temp/hadoop_test/deg2friends/src_data/total_firends.txt";
  def main(args: Array[String]) {
    val conf = new SparkConf().setMaster("local[*]").setAppName("FIND_DEGREE2_FRIENDS");
    val sc = new SparkContext(conf);
    sc.setLogLevel("WARN")

    val srcDataRdd = sc.textFile(files).map(_.split("\t")).cache()
    val srcFriendRdd = srcDataRdd.map(f => (f(0), f(1))).union(srcDataRdd.map(f => (f(1), f(0)))).distinct().cache()
    val deg1FriendRdd = srcFriendRdd.map(f => ((f._1, f._2), 1)).cache()
    val deg2FriendRdd = srcFriendRdd.join(srcFriendRdd).map(f => (f._2._1, f._2._2)).filter(f => (f._1 != f._2)).map(f => ((f._1, f._2), 2)).cache()
    deg2FriendRdd.leftOuterJoin(deg1FriendRdd).filter(f => (f._2._2.isEmpty)).map(f => (f._1, 1)).reduceByKey((x, y) => (x + y)).foreach(f => (println(f._1 + "  has " + f._2 + " same friends!")))
  }
}

执行的计算结果如下:

(Tom,HanMeimei)  has 1 same friends!
(HanMeimei,Jim)  has 1 same friends!
(Tom,Tim)  has 1 same friends!
(Tim,Jim)  has 1 same friends!
(LiLei,Lucy)  has 1 same friends!
(Jim,Tom)  has 2 same friends!
(Tim,Kate)  has 2 same friends!
(Lucy,Kate)  has 2 same friends!
(Kate,LiLei)  has 1 same friends!
(Lucy,Tim)  has 2 same friends!
(Jim,Lucy)  has 1 same friends!
(Jim,HanMeimei)  has 1 same friends!
(HanMeimei,Tom)  has 1 same friends!
(Tim,Lucy)  has 2 same friends!
(Lucy,LiLei)  has 1 same friends!
(Kate,Tim)  has 2 same friends!
(Tom,Jim)  has 2 same friends!
(Lucy,Tom)  has 1 same friends!
(Jim,Tim)  has 1 same friends!
(Lucy,Jim)  has 1 same friends!
(LiLei,Lily)  has 1 same friends!
(Kate,Lucy)  has 2 same friends!
(Tom,Lucy)  has 1 same friends!
(Lily,HanMeimei)  has 3 same friends!
(LiLei,Kate)  has 1 same friends!
(HanMeimei,Lily)  has 3 same friends!
(Lily,LiLei)  has 1 same friends!
(Tim,Tom)  has 1 same friends!

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值