Scala模拟生成日志文件,包括:IP,请求的网页,状态等
import java.io.{File, PrintWriter}
import java.text.SimpleDateFormat
import java.util.{Date, Random}
/**
* @className: .SparkSteamingDemo02
* @author: Star
* @create: 2022-07-27 19:10
*/
object SparkSteamingDemo02 {
def main(args: Array[String]): Unit = {
val writer = new PrintWriter(new File("datas/9.txt"))
for (i <- 1 to 10) {
writer.println(loggers)
}
writer.flush()
writer.close()
}
def loggers(): Unit = {
var i = 0
while (i <= 10) {
ip
web
mpb
logTime
requestType
addr
status
println(" ")
i = i + 1
//可以不要,为了便于观察而已
Thread.sleep(1000)
}
}
/**
* 生成固定位数的IP
*/
def ip(): Unit = {
val random1 = new Random()
val ran = random1.nextLong().toString
val num = ran.substring(0, 13)
val nums = num.substring(1, 4) + "." + num.substring(4, 7) + "." + num.substring(7, 10) + "." + num.substring(10, 13)
print(" " + nums)
}
/**
* 生成日志的时间;
*/
def logTime(): Unit = {
val date = new Date()
var dateTime: SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd/HH:mm:ss")
var logTimes = dateTime.format(date)
print(" " + logTimes)
}
/**
* 生成请求类型
*/
def requestType(): Unit = {
var num = scala.util.Random.nextInt(10)
if (num <= 7) {
print(" " + "POST")
} else {
print(" " + "GET")
}
if (num > 7) {
print("null")
}
}
/**
* 随机生成手机品牌
*/
def mpb(): Unit = {
val phone = Array("华为", "IQOO", "小米", "苹果", "诺基亚", "OPPle", "努比亚", "一加", "三星", "锤子")
var rand = scala.util.Random.nextInt(10)
print(" " + phone(rand))
}
/**
* 生成访问的页面
*/
def addr(): Unit = {
var str = scala.util.Random.nextInt(15)
val page = Array(" " +
"/www.wangxig.bi/69809567/jgfnblns/79/gooh.sc",
"/php/1427679483/course/format/weeks/format.js",
"/theme/yui_combo.php?rollup/3.17.2/yui-moodlesimple-min.css",
"/favicon.ico HTTP/1.1",
"/course/view.php?id=27",
"https://blog.csdn.net",
"www.https://ai.taobao.com",
"http://toutiao.sogou.com",
"https:www.hour//mini.yyrtv.com",
"https://www.bilibili.com",
"https://www.bilibili.com/video",
"https://www.douyin.com",
"http://picture.youth.cn",
"https://blog.csdn.net",
"https://daohang.qq.com"
)
print(" " + page(str))
}
/**
* 生成状态码
*/
def status(): Unit = {
var str = scala.util.Random.nextInt(7)
val sta = Array("200", "400", "404", "500", "900", " ", "401")
print(" " + sta(str))
}
/**
* 浏览器类型;
*/
def web(): Unit = {
val web1 = Array("QQ", "Internet explorer", "Safari", "Firefox", "Chrome", "Google", "Edge")
val c = (Math.random() * (7) + 0).toInt
print(" " + web1(c))
}
}