spark lac 分析停留最長的兩個地方

hgs19921112發表於2018-10-11
package hgs.spark.othertest
import org.apache.spark.SparkConf
import org.apache.spark.SparkContext
object FindTheTop2 {
  def main(args: Array[String]): Unit = {
    
    val conf = new  SparkConf().setAppName("FindTheTop2").setMaster("local[3]")
    val sc = new SparkContext(conf)
    val rdd1 = sc.textFile("D:\\bs_log")  
    //rdd_phone_lac_time:(18688888888 16030401EAFB68F1E3CDF819735E1C66,-20160327082400,1), (18611132889 16030401EAFB68F1E3CDF819735E1C66,-20160327082500,1)
    //先對映為上一行為例的map(K,V)
    val rdd_phone_lac_time = rdd1.map(x=>{
      val list = x.split(",")
      if(Integer.parseInt(list(3))==1)
      (list(0)+" "+list(2),-list(1).toLong)
      else{
       (list(0)+" "+list(2),list(1).toLong)
         }
     }
    )
    //根據rdd_phone_lac_time 的key進行reduce,將所有key相同的資料相加
    val rdd_reduce_phone_lackey = rdd_phone_lac_time.reduceByKey((x,y)=>x+y)
    //(18688888888,CompactBuffer((18688888888 16030401EAFB68F1E3CDF819735E1C66,87600), (18688888888 9F36407EAD0629FC166F14DDE7970F68,51200), (18688888888 CC0710CC94ECC657A8561DE549D940E0,1300)))
    //取top2,mapValues對values操作,返回的是map(K,V),K是原始的K,V是操作後得到的V
    val rdd_reduce_phone_lackey_groupyed = rdd_reduce_phone_lackey.groupBy(x=>x._1.split(" ")(0))
    val rdd_top2 = rdd_reduce_phone_lackey_groupyed.mapValues(x=>{
      x.toList.sortBy(_._2).reverse.take(2)    
    })
    //(16030401EAFB68F1E3CDF819735E1C66,(18688888888,16030401EAFB68F1E3CDF819735E1C66,87600))
    //下面需要與另一個map根據特定的欄位例如16030401EAFB68F1E3CDF819735E1C66進行join,所以需要將‘18688888888 16030401EAFB68F1E3CDF819735E1C66’拆開,將第二個作為K,返回新的map
    val rdd_result = rdd_top2.flatMap(x=>{
         x._2.map(y=>{
           val li = y._1.split(" ")
           (li(1),(li(0),li(1),y._2))
         })
         
       })
       //該檔案中即是需要與上面的結果進行join
     val lati_longti = sc.textFile("D:\\lac_info", 1)
     //(9F36407EAD0629FC166F14DDE7970F68,(116.304864,40.050645))
     //對映成如上一行的map
     val rdd_coordinate = lati_longti.map(f=>{
       val li = f.split(",")
       (li(0),(li(0),li(1),li(2)))
     })
     //進行join
     //rdd_coordinate 與rdd_result的結構型別已改是一樣的,即K,V的型別對應,否則無法join
     val join_resultWithcoordinate = rdd_coordinate.join(rdd_result)
    // rdd_coordinate.to
     //println(rdd_result.collect().length)
     //儲存檔案
    join_resultWithcoordinate.saveAsTextFile("d:\\dest")
    sc.stop()
    
  }
}


樣例資料

D:\\bs_log
18688888888,20160327082400,16030401EAFB68F1E3CDF819735E1C66,1
18611132889,20160327082500,16030401EAFB68F1E3CDF819735E1C66,1
18688888888,20160327170000,16030401EAFB68F1E3CDF819735E1C66,0
18611132889,20160327075000,9F36407EAD0629FC166F14DDE7970F68,1
18688888888,20160327075100,9F36407EAD0629FC166F14DDE7970F68,1
18611132889,20160327081000,9F36407EAD0629FC166F14DDE7970F68,0
18688888888,20160327081300,9F36407EAD0629FC166F14DDE7970F68,0
18688888888,20160327175000,9F36407EAD0629FC166F14DDE7970F68,1
18611132889,20160327182000,9F36407EAD0629FC166F14DDE7970F68,1
18688888888,20160327220000,9F36407EAD0629FC166F14DDE7970F68,0
18611132889,20160327230000,9F36407EAD0629FC166F14DDE7970F68,0
18611132889,20160327180000,16030401EAFB68F1E3CDF819735E1C66,0
18611132889,20160327081100,CC0710CC94ECC657A8561DE549D940E0,1
18688888888,20160327081200,CC0710CC94ECC657A8561DE549D940E0,1
18688888888,20160327081900,CC0710CC94ECC657A8561DE549D940E0,0
18611132889,20160327082000,CC0710CC94ECC657A8561DE549D940E0,0
18688888888,20160327171000,CC0710CC94ECC657A8561DE549D940E0,1
18688888888,20160327171600,CC0710CC94ECC657A8561DE549D940E0,0
18611132889,20160327180500,CC0710CC94ECC657A8561DE549D940E0,1
18611132889,20160327181500,CC0710CC94ECC657A8561DE549D940E0,0
D:\\lac_info  
9F36407EAD0629FC166F14DDE7970F68,116.304864,40.050645,6
CC0710CC94ECC657A8561DE549D940E0,116.303955,40.041935,6
16030401EAFB68F1E3CDF819735E1C66,116.296302,40.032296,6
資料結果:
(16030401EAFB68F1E3CDF819735E1C66,((16030401EAFB68F1E3CDF819735E1C66,116.296302,40.032296),(18688888888,16030401EAFB68F1E3CDF819735E1C66,87600)))
(16030401EAFB68F1E3CDF819735E1C66,((16030401EAFB68F1E3CDF819735E1C66,116.296302,40.032296),(18611132889,16030401EAFB68F1E3CDF819735E1C66,97500)))
(9F36407EAD0629FC166F14DDE7970F68,((9F36407EAD0629FC166F14DDE7970F68,116.304864,40.050645),(18688888888,9F36407EAD0629FC166F14DDE7970F68,51200)))
(9F36407EAD0629FC166F14DDE7970F68,((9F36407EAD0629FC166F14DDE7970F68,116.304864,40.050645),(18611132889,9F36407EAD0629FC166F14DDE7970F68,54000)))


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31506529/viewspace-2216122/,如需轉載,請註明出處,否則將追究法律責任。

相關文章