SparkCore代码笔记04——sparkcore 求各省ip访问量

任务:

http.log中有访问记录,第二项是ip地址

20090121000132095572000|125.213.100.123|show.51.com。。。。。。。。。。。。。。。。。。。。。。。。。。。。

ip.txt中有ip的基本信息

startIP  endIP        startIP(long类型的)  endIP(long类型) 洲 国家 省、、、、、

1.0.1.0|1.0.3.255|16777472|16778239|亚洲|中国|福建|福州||电信|350100|China|CN|119.306239|26.075302

 

需求:求出每个省的访问数量

package scalaBase.day12

import org.apache.avro.TestAnnotation
import org.apache.spark.broadcast.Broadcast
import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

object IpSearch {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf().setAppName("ipSearch").setMaster("local")
   val sc = new SparkContext(conf)

val rdd: RDD[String] = sc.textFile("D://MyBigData/Aspark/data/ipsearch/ip.txt")

    val rdd1: RDD[(String, String, String)] =rdd .map(x => {
      val f = x.split("\\|")
      val start = f(2)
      val end = f(3)
      val pro = f(6)
      (start, end, pro)
    })
    val broad: Broadcast[Array[(String, String, String)]] = sc.broadcast(rdd1.collect())

    val pro: RDD[(String, Int)] = sc.
      textFile("D://MyBigData/Aspark/data/ipsearch/http.log").map(x => {
      val f = x.split("\\|")
      val longIp = ipToLong(f(1))
      val ipInfo: Array[(String, String, String)] = broad.value
      val i = binarSeach(ipInfo, longIp)
      println(longIp)
      println(i+"__________"+ipInfo.length)
      (ipInfo(i)._3, 1)
    })
    val res: Array[(String, Int)] = pro.reduceByKey(_+_).collect()
println(res.toBuffer)


  }


//二分查找
  def binarSeach(arr: Array[(String, String, String)], ip: Long):Int= {
 var start=0
    var end=arr.length-1

    while (start<=end){
      var middle=(start+end)/2
      if(ip>=arr(middle)._1.toLong&&(ip<=arr(middle)._2.toLong)){
        return  middle
      }
      else if(ip<arr(middle)._1.toLong){
        end=middle-1
      }
      else {
        start=middle+1
      }
    }
    -1
  }

//将ip转换成long类型
  def ipToLong(ip:String):Long={
    val splited: Array[String] = ip.split("\\.")
    var ipNumber=0L
    for(i<-0 until splited.length){
      ipNumber = splited(i).toLong | ipNumber<<8L

    }
    ipNumber
  }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值