访问量统计小练习

关键代码:


访问量统计小练习_第1张图片
图片.png
访问量统计小练习_第2张图片
图片.png
访问量统计小练习_第3张图片
图片.png

完整代码:

package test1107

object HttpUtilsTest {
  def get(url: String) = {
    import org.apache.commons.httpclient.HttpClient
    import org.apache.commons.httpclient.methods.GetMethod

    val client = new HttpClient()
    val method = new GetMethod(url)

    val code = client.executeMethod(method)

    if (code == 200) {
      method.getResponseBodyAsString
    } else {
      null
    }
  }

  def post(url: String, content: String) = {
    import org.apache.commons.httpclient.HttpClient
    import org.apache.commons.httpclient.methods.{PostMethod, StringRequestEntity}

    val client = new HttpClient()
    val method = new PostMethod(url)

    val r = new StringRequestEntity(content, "application/json", "utf-8")
    method.setRequestEntity(r)

    val code = client.executeMethod(method)

    if (code == 200) {
      method.getResponseBodyAsString
    } else {
      null
    }
  }


  //  def main(args: Array[String]): Unit = {
  //    //    println(get("http://localhost:8080/getInfo?yy=zhangsan&name=long"))
  //    val content = "{\"name\":\"lisi\",\"age\":100}"
  //    println(post("http://localhost:8080/saveuser?x=123", content))
  //  }

  def main(args: Array[String]): Unit = {
    import scala.io.Source
    //    Source.fromFile("C:\\pmt.json", "utf-8").getLines().take(100).foreach(println(_))
    Source.fromFile("D:\\pmt.json", "utf-8").getLines().filter(_.length > 0).toList.take(1000).map(line => {
      import com.alibaba.fastjson.JSON

      import scala.util.Try
      val json = JSON.parseObject(line)
      val ip = json.getString("ip")
      val requestmode = json.getInteger("requestmode")
      val processnode = json.getInteger("processnode")

      //      114.247.50.2
      val url = s"http://restapi.amap.com/v3/ip?ip=${ip}&key=1b2a4ed3fd051edd00211fd27c7a6884"
      val resp_json = get(url)
      //      println("~~~~~~~~~~~~~~~~~~~~~~>>>>>", resp_json)

      val (p, c) = Try(parseJson(resp_json)).getOrElse("", "")
      (p, c, requestmode, processnode)
    }).filter(x => x._1 != "[]" && x._2 != "[]").groupBy {
      // 按照 省份、城市 进行分组
      case (p, c, requestmode, processnode) => (p, c)
    }.map(x => {
      val p = x._1._1
      val c = x._1._2
      val cnt = x._2.filter(k => k._3 == 1 && k._4 >= 1).size

      (p, c, cnt)
    }).foreach(println(_))
  }


  def parseJson(str: String) = {
    import com.alibaba.fastjson.JSON
    val json = JSON.parseObject(str)
    val p = json.getString("province")
    val c = json.getString("city")
    (p, c)
  }

}

查询结果:

访问量统计小练习_第4张图片
图片.png

你可能感兴趣的:(访问量统计小练习)