计算ip地址归属地

小灰灰 2021-11-01 07:22 440阅读 0赞

在很多场景下我们可能都会遇到根据用户某信息获取其位置信息的徐求,比较常见的可能就是根据ip或电话号码来计算了。这里介绍以ip地址来计算归属地,以具体需求来说明。使用scala语言编写spark程序来实现。

需求:根据访问日志的ip地址计算用户归属地信息,并且按其归属地分类统计访问量。

(1)、获取ip地址对应地理信息,作为字典数据。对于省市地区来说每一个行政区域都有一个ip段,将ip转换为十进制来计算。

(2)、加载规则,整理规则,取出计算需要的字段,将数据缓存到内存中。、

(3)、将访问的log与ip规则进行匹配(二分法)

(4)、分组

(5)、聚合(省份)

Log数据示例:

1558763-20190602144611805-431999909.png

字典数据示例:

1558763-20190602144631198-669218015.png

首先构造一个工具类,将ip规则数据进行处理:

  1. object IPUtils {
  2. def ip2Long(ip: String): Long = {
  3. val fragments = ip.split("[.]")
  4. var ipNum = 0L
  5. for (i <- 0 until fragments.length){
  6. ipNum = fragments(i).toLong | ipNum << 8L
  7. }
  8. ipNum
  9. }
  10. def readIpRules(path: String): Array[(Long, Long, String)] = {
  11. //读取ip规则
  12. val bf: BufferedSource = Source.fromFile(path)
  13. val lines: Iterator[String] = bf.getLines()
  14. //对ip规则进行整理,并放入到内存
  15. val rules: Array[(Long, Long, String)] = lines.map(line => {
  16. val fileds = line.split("[|]")
  17. val startNum = fileds(2).toLong
  18. val endNum = fileds(3).toLong
  19. val province = fileds(6)
  20. (startNum, endNum, province)
  21. }).toArray
  22. rules
  23. }
  24. /*
  25. *使用二分法查找
  26. */
  27. def binarySearch(lines: Array[(Long, Long, String)], ip: Long) : Int = {
  28. var low = 0
  29. var high = lines.length - 1
  30. while (low <= high) {
  31. val middle = (low + high) / 2
  32. if ((ip >= lines(middle)._1) && (ip <= lines(middle)._2))
  33. return middle
  34. if (ip < lines(middle)._1)
  35. high = middle - 1
  36. else {
  37. low = middle + 1
  38. }
  39. }
  40. -1
  41. }
  42. /*
  43. *将计算结果存储mysql
  44. */
  45. def data2MySQL(it: Iterator[(String, Int)]): Unit = {
  46. //一个迭代器代表一个分区,分区中有多条数据
  47. //先获得一个JDBC连接
  48. val conn: Connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/bigdata?characterEncoding=UTF-8", "root", "123568")
  49. //将数据通过Connection写入到数据库
  50. val pstm: PreparedStatement = conn.prepareStatement("INSERT INTO access_log VALUES (?, ?)")
  51. //将分区中的数据一条一条写入到MySQL中
  52. it.foreach(tp => {
  53. pstm.setString(1, tp._1)
  54. pstm.setInt(2, tp._2)
  55. pstm.executeUpdate()
  56. })
  57. //将分区中的数据全部写完之后,在关闭连接
  58. if(pstm != null) {
  59. pstm.close()
  60. }
  61. if (conn != null) {
  62. conn.close()
  63. }
  64. }
  65. }

使用spark调用方法进行计算:

  1. 1 object IpLoactAndAggByProvince {
  2. 2
  3. 3 def main(args: Array[String]): Unit = {
  4. 4
  5. 5 val conf = new SparkConf().setAppName("IpLoaction1").setMaster("local[4]")
  6. 6
  7. 7 val sc = new SparkContext(conf)
  8. 8
  9. 9 //取到HDFS中的ip规则
  10. 10 val rulesLines:RDD[String] = sc.textFile(args(0))
  11. 11 //整理ip规则数据
  12. 12 val ipRulesRDD: RDD[(Long, Long, String)] = rulesLines.map(line => {
  13. 13 val fields = line.split("[|]")
  14. 14 val startNum = fields(2).toLong
  15. 15 val endNum = fields(3).toLong
  16. 16 val province = fields(6)
  17. 17 (startNum, endNum, province)
  18. 18 })
  19. 19
  20. 20 //将分散在多个Executor中的部分IP规则收集到Driver端
  21. 21 val rulesInDriver: Array[(Long, Long, String)] = ipRulesRDD.collect()
  22. 22
  23. 23 //将Driver端的数据广播到Executor
  24. 24 //广播变量的引用(还在Driver端)
  25. 25 val broadcastRef: Broadcast[Array[(Long, Long, String)]] = sc.broadcast(rulesInDriver)
  26. 26
  27. 27 //创建RDD,读取访问日志
  28. 28 val accessLines: RDD[String] = sc.textFile(args(1))
  29. 29
  30. 30 //整理数据
  31. 31 val proviceAndOne: RDD[(String, Int)] = accessLines.map(log => {
  32. 32 //将log日志的每一行进行切分
  33. 33 val fields = log.split("[|]")
  34. 34 val ip = fields(1)
  35. 35 //将ip转换成十进制
  36. 36 val ipNum = MyUtils.ip2Long(ip)
  37. 37 //进行二分法查找,通过Driver端的引用或取到Executor中的广播变量
  38. 38 //(该函数中的代码是在Executor中别调用执行的,通过广播变量的引用,就可以拿到当前Executor中的广播的规则了)
  39. 39 //Driver端广播变量的引用是怎样跑到Executor中的呢?
  40. 40 //Task是在Driver端生成的,广播变量的引用是伴随着Task被发送到Executor中的
  41. 41 val rulesInExecutor: Array[(Long, Long, String)] = broadcastRef.value
  42. 42 //查找
  43. 43 var province = "未知"
  44. 44 val index = MyUtils.binarySearch(rulesInExecutor, ipNum)
  45. 45 if (index != -1) {
  46. 46 province = rulesInExecutor(index)._3
  47. 47 }
  48. 48 (province, 1)
  49. 49 })
  50. 50
  51. 51 //聚合
  52. 52 //val sum = (x: Int, y: Int) => x + y
  53. 53 val reduced: RDD[(String, Int)] = proviceAndOne.reduceByKey(_+_)
  54. 54
  55. 55 //将结果打印
  56. 56 //val r = reduced.collect()
  57. 57 //println(r.toBuffer)
  58. 58
  59. 59
  60. 60 /**
  61. 61 reduced.foreach(tp => {
  62. 62 //将数据写入到MySQL中
  63. 63 //问?在哪一端获取到MySQL的链接的?
  64. 64 //是在Executor中的Task获取的JDBC连接
  65. 65 val conn: Connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/bigdata?charatorEncoding=utf-8", "root", "123568")
  66. 66 //写入大量数据的时候,有没有问题?
  67. 67 val pstm = conn.prepareStatement("...")
  68. 68 pstm.setString(1, tp._1)
  69. 69 pstm.setInt(2, tp._2)
  70. 70 pstm.executeUpdate()
  71. 71 pstm.close()
  72. 72 conn.close()
  73. 73 })
  74. 74 */
  75. 75
  76. 76 //一次拿出一个分区(一个分区用一个连接,可以将一个分区中的多条数据写完在释放jdbc连接,这样更节省资源)
  77. 77 // reduced.foreachPartition(it => {
  78. 78 // val conn: Connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/bigdata?characterEncoding=UTF-8", "root", "123568")
  79. 79 // //将数据通过Connection写入到数据库
  80. 80 // val pstm: PreparedStatement = conn.prepareStatement("INSERT INTO access_log VALUES (?, ?)")
  81. 81 // //将一个分区中的每一条数据拿出来
  82. 82 // it.foreach(tp => {
  83. 83 // pstm.setString(1, tp._1)
  84. 84 // pstm.setInt(2, tp._2)
  85. 85 // pstm.executeUpdate()
  86. 86 // })
  87. 87 // pstm.close()
  88. 88 // conn.close()
  89. 89 // })
  90. 90
  91. 91 reduced.foreachPartition(it => MyUtils.data2MySQL(it))
  92. 92
  93. 93
  94. 94 sc.stop()
  95. 95
  96. 96
  97. 97
  98. 98 }
  99. 99 }

转载于:https://www.cnblogs.com/lsbigdata/p/10962985.html

发表评论

表情:
评论列表 (有 0 条评论,440人围观)

还没有评论,来说两句吧...

相关阅读

    相关 springboot获取IP归属

    在 Spring Boot 中获取 IP 归属地可以使用第三方 API 来实现。 一种方法是使用网络 API 来查询 IP 地址的归属地。例如,您可以使用淘宝的 IP 地址库

    相关 计算ip地址归属

    在很多场景下我们可能都会遇到根据用户某信息获取其位置信息的徐求,比较常见的可能就是根据ip或电话号码来计算了。这里介绍以ip地址来计算归属地,以具体需求来说明。使用scala语