您好,登錄后才能下訂單哦!
這篇文章主要介紹“如何生成Java數據腳本”,在日常操作中,相信很多人在如何生成Java數據腳本問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”如何生成Java數據腳本”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
/** * 向文件中生產數據 */ object ProducePvAndUvData { //ip val IP = 223 //地址 val ADDRESS = Array("北京", "天津", "上海", "重慶", "河北", "遼寧","山西", "吉林", "江蘇", "浙江", "黑龍江", "安徽", "福建", "江西", "山東", "河南", "湖北", "湖南", "廣東", "海南", "四川", "貴州", "云南", "山西", "甘肅", "青海", "臺灣", "內蒙", "廣西", "西藏", "寧夏", "新疆", "香港", "澳門") //日期 val DATE = new SimpleDateFormat("yyyy-MM-dd").format(new Date()) //timestamp val TIMESTAMP = 0L //userid val USERID = 0L //網站 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" //創建文件 val createFile = CreateFile(pathFileName) //向文件中寫入數據 需要的對象 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 //產生5萬+數據 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模擬是同一個用戶不同時間點對不同網站的操作 */ var j = 0 var timestamp = 0L var webSite = "未知網站" var action = "未知行為" val flag = random.nextInt(5) | 1 while (j < flag) { // Threads.sleep(5); //模擬timestamp timestamp = new Date().getTime() //模擬網站 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) //向文件中寫入數據 pw.write(content + "\n") } i += 1 } //注意關閉的先后順序,先打開的后關閉,后打開的先關閉 pw.close() osw.close() fos.close() } } /** * 創建文件 */ 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 } }
統計每個網站的PU、VU、每個網站的每個地區訪問量,由大到小排序
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))//網站_ip .distinct()//去重 .map(line=>{(line.split("_")(0),1)}) .reduceByKey(_+_) .sortBy(_._2,false) .foreach(println) //每個網址的每個地區訪問量,由大到小排序 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) { //地區 val local = localIterator.next() if (localMap.contains(local)) { //如果map中有該地區,則獲取該地區的值再加1 val value = localMap.get(local).get localMap.put(local, value + 1) } else { //如果map中沒有該地區,則獲取該地區的值再加1 localMap.put(local, 1); } } //默認是升序,降序: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) }
到此,關于“如何生成Java數據腳本”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。