scala文件流操作,生成sparkpv,uv作业文件

package com.bjsxt.scalaspark.core.examples.pvAnduv

import java.io.File
import java.text.SimpleDateFormat
import java.util.{Date, Random}
import java.io.FileOutputStream
import java.io.OutputStreamWriter
import java.io.PrintWriter
/**
* 向文件中生产数据
*/
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 = "./data/pvuvdata"
//创建文件
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
}

}

转载于:https://www.cnblogs.com/pursue339/p/10612900.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值