【Data Algorithms_Recipes for Scaling up with Hadoop and Spark】Chapter 8 Common Friends

:scala 版算法

package com.bbw5.dataalgorithms.spark

import org.apache.spark.SparkConf
import org.apache.spark.SparkContext
import scala.collection.mutable.HashMap

/**
 * The FindCommonFriends is a Spark program to find "common friends"
 * between all users.
 * @author baibaiw5
 */
object SparkFindCommonFriends {
  def main(args: Array[String]) {
    val sparkConf = new SparkConf().setAppName("SparkFindAssociationRules")
    val sc = new SparkContext(sparkConf)

    val data = sc.parallelize(Seq("100,200 300 400 500 600", "200,100 300 400", "300,100 200 400 500",
      "400,100 200 300", "500,100 300", "600,100"), 2)
    val friendsRdd = data.flatMap { l =>
      val a = l.split(",")
      val id = a(0).toInt
      val friends = a(1).split(" ").filter { x => x.trim().length() > 0 }.map(_.toInt).toList
      friends.map { f =>
        if (f < id) (f -> id) -> friends else (id -> f) -> friends
      }
    }

    friendsRdd.collect().foreach(println)

    val commonFriends = friendsRdd.groupByKey().mapValues { x =>
      val map = new HashMap[Int, Int]()
      x.foldLeft(map) { (map, a) =>
        a.foreach(i => map.put(i, map.getOrElse(i, 0) + 1))
        map
      }
      map.filter(p => p._2 == x.size).map(_._1)
    }

    commonFriends.collect().foreach(println)

  }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值