溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何生成Java數(shù)據(jù)腳本

發(fā)布時間:2021-11-16 11:23:00 來源:億速云 閱讀:248 作者:iii 欄目:大數(shù)據(jù)

這篇文章主要介紹“如何生成Java數(shù)據(jù)腳本”,在日常操作中,相信很多人在如何生成Java數(shù)據(jù)腳本問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”如何生成Java數(shù)據(jù)腳本”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

/**
  * 向文件中生產(chǎn)數(shù)據(jù)
  */
object ProducePvAndUvData {

  //ip
  val IP = 223
  //地址
  val ADDRESS = Array("北京", "天津", "上海", "重慶", "河北", "遼寧","山西",
                      "吉林", "江蘇", "浙江", "黑龍江", "安徽", "福建", "江西",
                      "山東", "河南", "湖北", "湖南", "廣東", "海南", "四川",
                      "貴州", "云南", "山西", "甘肅", "青海", "臺灣", "內(nèi)蒙",
                      "廣西", "西藏", "寧夏", "新疆", "香港", "澳門")
  //日期
  val DATE = new SimpleDateFormat("yyyy-MM-dd").format(new Date())
  //timestamp
  val TIMESTAMP = 0L
  //userid
  val USERID = 0L
  //網(wǎng)站
  val WEBSITE = Array("www.baidu.com", "www.taobao.com", "www.dangdang.com", "www.jd.com", "www.suning.com", "www.mi.com", "www.gome.com.cn")
  //行為
  val ACTION = Array("Regist", "Comment", "View", "Login", "Buy", "Click", "Logout")

  def main(args: Array[String]): Unit = {
    val pathFileName = "G://idea//scala//spark02/data"
    //創(chuàng)建文件
    val createFile = CreateFile(pathFileName)

    //向文件中寫入數(shù)據(jù) 需要的對象
    val file = new File(pathFileName)
    val fos = new FileOutputStream(file, true)
    val osw = new OutputStreamWriter(fos, "UTF-8")
    val pw = new PrintWriter(osw)

    if (createFile) {
      var i = 0
      //產(chǎn)生5萬+數(shù)據(jù)
      while (i < 50000){ //模擬一個ip
        val random = new Random()
        val ip = random.nextInt(IP) + "." + random.nextInt(IP) + "." + random.nextInt(IP) + "." + random.nextInt(IP)
        //模擬地址
        val address = ADDRESS(random.nextInt(34))
        //模擬日期
        val date = DATE
        //模擬userid
        val userid = Math.abs(random.nextLong)
        /**
          * 這里的while模擬是同一個用戶不同時間點(diǎn)對不同網(wǎng)站的操作
          */
        var j = 0
        var timestamp = 0L
        var webSite = "未知網(wǎng)站"
        var action = "未知行為"
        val flag = random.nextInt(5) | 1
        while (j < flag) { //					Threads.sleep(5);
          //模擬timestamp
          timestamp = new Date().getTime()
          //模擬網(wǎng)站
          webSite = WEBSITE(random.nextInt(7))
          //模擬行為
          action = ACTION(random.nextInt(6))
          j += 1
          /**
            * 拼裝
            */
          val content = ip + "\t" + address + "\t" + date + "\t" + timestamp + "\t" + userid + "\t" + webSite + "\t" + action
          System.out.println(content)
          //向文件中寫入數(shù)據(jù)
          pw.write(content + "\n")
        }
        i += 1
      }

      //注意關(guān)閉的先后順序,先打開的后關(guān)閉,后打開的先關(guān)閉
      pw.close()
      osw.close()
      fos.close()

    }
  }

  /**
    * 創(chuàng)建文件
    */
  def CreateFile(pathFileName: String): Boolean = {
    val file = new File(pathFileName)
    if (file.exists) file.delete
    val createNewFile = file.createNewFile()
    System.out.println("create file " + pathFileName + " success!")
    createNewFile
  }
}

統(tǒng)計(jì)每個網(wǎng)站的PU、VU、每個網(wǎng)站的每個地區(qū)訪問量,由大到小排序

 def main(args: Array[String]): Unit = {
    val conf = new SparkConf()
    conf.setMaster("local")
    conf.setAppName("SparkPvAndUv")
    val sc = new SparkContext(conf)
    val rdd: RDD[String] = sc.textFile("G:/idea/scala/spark02/data")
    println("*************PU******************")
    rdd.map(line=>{(line.split("\t")(5),1)})
      .reduceByKey(_+_)
      .sortBy(_._2,false)//是否降序,false:是降序
      .foreach(println)

    println("*************UV******************")
    rdd.map(line=>line.split("\t")(5)+"_"+line.split("\t")(1))//網(wǎng)站_ip
      .distinct()//去重
      .map(line=>{(line.split("_")(0),1)})
      .reduceByKey(_+_)
      .sortBy(_._2,false)
      .foreach(println)

    //每個網(wǎng)址的每個地區(qū)訪問量,由大到小排序
    val site_local: RDD[(String, String)] = rdd.map(line=>{(line.split("\t")(5),line.split("\t")(1))})
    val site_localIterable: RDD[(String, Iterable[String])] = site_local.groupByKey()
    val result: RDD[(String, AbstractSeq[(String, Int)])] = site_localIterable.map(one => {
      val localMap = mutable.Map[String, Int]()
      //可變map
      val site = one._1
      val localIterator = one._2.iterator
      while (localIterator.hasNext) {
        //地區(qū)
        val local = localIterator.next()
        if (localMap.contains(local)) {
          //如果map中有該地區(qū),則獲取該地區(qū)的值再加1
          val value = localMap.get(local).get
          localMap.put(local, value + 1)
        } else {
          //如果map中沒有該地區(qū),則獲取該地區(qū)的值再加1
          localMap.put(local, 1);
        }
      }
      //默認(rèn)是升序,降序:localMap.toList.sortBy(-_._2),既多一個"-"
      val tuples: List[(String, Int)] = localMap.toList.sortBy(-_._2)

      if (tuples.length > 3) {
        val list = new ListBuffer[(String, Int)]()
        for (i <- 0 to 2) {
          list.append(tuples(i))
        }
        (site, list)
      } else {
        (site, tuples)
      }
    })
    result.foreach(println)
  }

到此,關(guān)于“如何生成Java數(shù)據(jù)腳本”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI