這篇文章主要介紹“如何生成Java數(shù)據(jù)腳本”,在日常操作中,相信很多人在如何生成Java數(shù)據(jù)腳本問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”如何生成Java數(shù)據(jù)腳本”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!
成都創(chuàng)新互聯(lián)公司專注于企業(yè)網(wǎng)絡(luò)營(yíng)銷推廣、網(wǎng)站重做改版、郎溪網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、HTML5建站、商城網(wǎng)站制作、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)營(yíng)銷網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為郎溪等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
/** * 向文件中生產(chǎn)數(shù)據(jù) */ object ProducePvAndUvData { //ip val IP = 223 //地址 val ADDRESS = Array("北京", "天津", "上海", "重慶", "河北", "遼寧","山西", "吉林", "江蘇", "浙江", "黑龍江", "安徽", "福建", "江西", "山東", "河南", "湖北", "湖南", "廣東", "海南", "四川", "貴州", "云南", "山西", "甘肅", "青海", "臺(tái)灣", "內(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ù) 需要的對(duì)象 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){ //模擬一個(gè)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模擬是同一個(gè)用戶不同時(shí)間點(diǎn)對(duì)不同網(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ì)每個(gè)網(wǎng)站的PU、VU、每個(gè)網(wǎng)站的每個(gè)地區(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) //每個(gè)網(wǎng)址的每個(gè)地區(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),既多一個(gè)"-" 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)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
網(wǎng)站欄目:如何生成Java數(shù)據(jù)腳本
文章位置:http://www.rwnh.cn/article16/ippcgg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、網(wǎng)站內(nèi)鏈、服務(wù)器托管、ChatGPT、面包屑導(dǎo)航、App設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)