idea生产数据模块

import java.io.{FileOutputStream, OutputStreamWriter}
import java.text.{DecimalFormat, SimpleDateFormat}
import java.util.Date
import scala.collection.mutable
import scala.collection.mutable.ListBuffer
import scala.util.control.Breaks

object ProductLog {
private val startTime = “2020-01-01”
private val endTime = “2020-12-31”

private val phoneList = ListBufferString
private val phoneNameMap: mutable.Map[String, String] = mutable.MapString, String

def main(args: Array[String]): Unit = {
val logPath = “D:\calllog.csv”;
//初始化数据
ProductLog.initPhone()
//数据写入到指定的文件
ProductLog.writeLog(logPath)
}

//初始化电话数据
def initPhone(): Unit = {
phoneList.append(“17078388295”)
phoneList.append(“13980337439”)
phoneList.append(“14575535933”)
phoneList.append(“19902496992”)
phoneList.append(“18549641558”)

phoneNameMap.put("17078388295", "李名")
phoneNameMap.put("13980337439", "卫艺")
phoneNameMap.put("14575535933", "仰莉")
phoneNameMap.put("19902496992", "陶欣悦")
phoneNameMap.put("18549641558", "施梅梅")

}

def randomBuildTime(startTime: String, endTime: String): String = {
val sdf1 = new SimpleDateFormat(“yyyy-MM-dd”)
val startDate: Date = sdf1.parse(startTime)
val endDate: Date = sdf1.parse(endTime)
if (endDate.getTime <= startDate.getTime) return null

val randomTS: Long = startDate.getTime + ((endDate.getTime - startDate.getTime) * Math.random).toLong
val resultDate = new Date(randomTS)
val sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
val resultTimeString: String = sdf2.format(resultDate)
resultTimeString

}

def product(): String = {
//主叫号码
var caller: String = null
//主叫号码对应的名字
var callerName: String = null
//被叫号码
var callee: String = null
//被叫号码对应的名字
var calleeName: String = null
//取得主叫电话号码
val callerIndex: Int = (Math.random * phoneList.size).toInt
//随即取出一个号码
caller = phoneList(callerIndex)
//取出根据号码对应的名字
callerName = phoneNameMap.get(caller).get
val breaks = new Breaks
breaks.breakable {
while (true) {
//取得被叫电话号码
val calleeIndex: Int = (Math.random * phoneList.size).toInt
//从list中选出其中一个号码,作为被叫号码
callee = phoneList(calleeIndex)
//被叫对应的名称
calleeName = phoneNameMap.get(callee).get
//如果两个电话号码是一样的,就结束本次循环
if (!(caller == callee)) {
breaks.break()
}

  }
}
//聊天建立的时间
val buildTime: String = randomBuildTime(startTime, endTime)
val df = new DecimalFormat("0000")
val duration: String = df.format((30 * 60 * Math.random).toInt)
val sb = new StringBuilder

sb.append(caller + “,”).append(callerName + “,”).append(callee + “,”).append(calleeName + “,”).append(buildTime + “,”).append(duration + “,”).append(“1”);
sb.toString
}

//把日志写入到指定文件
def writeLog(filePath: String): Unit = {
//创建输出流
val osw = new OutputStreamWriter(new FileOutputStream(filePath), “UTF-8”)
while (true) {
//休眠时间
Thread.sleep(500)
//生成数据
val log: String = product()
//打印数据
println("日志数据: " + log)
//把数据写入到指定的路径
osw.write(log + “\n”)
osw.flush()

}

}
}

你可能感兴趣的:(随机,flink)