Scala模拟日志生成

2010-1-10_Lenovo_2012-12-30-5:58:49_192.168.140.37_Safari_http://kan.sogou.com/player/180744624/_192.168.165.168

测试的日志由时间年月日,设备名称,时间精确到秒,用户ip,浏览器类型,浏览的网址,服务器ip这些元素组成,每一个元素之间用下划线(“_”)连接。

年月日:2010-1-10

设备名称:Lenovo

时间精确到秒:2021-12-30-5:58:49

用户ip:192.168.140.37

浏览器类型:Safari

浏览的网址:http://kan.sogou.com

服务器ip:192.168.165.168

import java.io.{File, PrintWriter}

object DataGet {
  def main(args: Array[String]): Unit = {
    Data(20000)
  }
  def Data(n:Int):Unit ={
    var sb:StringBuilder = new StringBuilder
    for (i <- 1 to n){
      sb.append(time)
        .append("_")
      if (i % 2 == 0) {
        sb.append(phone())
      } else {
        sb.append("null")
      }
        sb.append("_")
        .append(time + "-" + timeStamp())
      sb.append("_")
      if (i % 2 == 0){
        sb.append("192")
            .append(".")
            .append("168")
            .append(".")
          .append(ip())
      }else {
        sb.append("null")
      }
      sb.append("_")
      if (i <= n - 1){
        sb.append(web())
      }else {
        sb.append("_")
      }
      sb.append("_")
      if (i <= n - 1){
        sb.append(webpage())
      } else (
        sb.append("null")
      )
      sb.append("_")
      if (i % 2 == 0){
        sb.append("192")
          .append(".")
          .append("168")
          .append(".")
          .append(userId())
      }else {
        sb.append("null")
      }
      if (i <= n - 1) {
          sb.append("\n")
        }
    }
    writeToFile(sb.toString())
  }
  def userId(): String = {
    var IDID: Int = (Math.random() * (250 - 1 + 1)+1).toInt
    var IDIDID: Int = (Math.random() * (250 - 1 + 1)).toInt
    IDID + "." + IDIDID
  }
  def webpage(): String = {
    val page = Array(
      "https://blog.csdn.net",
      "https://ai.taobao.com",
      "http://toutiao.sogou.com",
      "https://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")
    val s = (Math.random() * (10) + 0).toInt
    page(s)
  }
  
  def phone(): String = {
    val array = Array( "Apple", "Xiaomi", "Lenovo", "Huawei", "Redmi", 
      "Msi", "Oppo", "vivo","honor","YYDS","oneplus","tototo")
    val r = (math.random()*(11)+0).toInt
    array(r)
  }
  def web(): String = {
    val web1 = Array("QQ","Internet explorer","Safari","Firefox","Chrome","Google","Edge")
    val c = (Math.random() * (7) + 0).toInt
    web1(c)
  }
  def ip(): String ={
    var ipip: Int = (Math.random() * (250 - 1 + 1)+1).toInt
    var ipipip: Int = (Math.random() * (250 - 1 + 1)).toInt
    ipip + "." + ipipip
  }
  def time(): String = {
    var year: Int = (Math.random() * (2021 - 2010 + 1) + 2010).toInt
    var month: Int = (Math.random() * (12 - 1 + 1) + 1).toInt
    var day: Int = (Math.random() * (31 - 1 + 1) + 1).toInt
    year + "-" + month + "-" + day
  }
  def timeStamp(): String = {
    var hour = (Math.random() * (24 - 1 + 1) + 1).toInt
    var m = (Math.random() * (60 - 1 + 1) + 1).toInt
    var second = (Math.random() * (60 - 1 + 1) + 1).toInt
    hour + ":" + m + ":" + second
  }
  def cityId(): Int = {
    (Math.random() * (10000 - 1) + 1).toInt
  }
  def writeToFile(str: String): Unit = {
    val printWriter = new PrintWriter(new File("F:Scala\\test\\bighomework\\testlog7.log"))
    printWriter.write(str)
    printWriter.flush()
    printWriter.close()
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值