object Producerlog {
private val startTime ="2020-01-01"
? private val endTime ="2020-12-31"
? private val phoneList =new ListBuffer[String]()
private val phoneNameMap: mutable.Map[String,String] = mutable.Map[String,String]()
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 prodtuc():String = {
//主叫號(hào)碼
? ? var caller:String =null
? ? //主叫號(hào)碼對(duì)應(yīng)的名字
? ? var callerName :String=null
? ? //被叫號(hào)碼
? ? var callee:String =null
? ? //被叫號(hào)碼對(duì)應(yīng)的名字
? ? var calleeName :String=null
? ? //取得主叫號(hào)碼
? ? val callerIndex = (Math.random() *phoneList.size).toInt
//隨機(jī)取出一個(gè)號(hào)碼
? ? caller =phoneList(callerIndex)
//取出根據(jù)號(hào)碼對(duì)應(yīng)的名字
? ? ? callerName =phoneNameMap.get(caller).get
val breaks =new Breaks
breaks.breakable(
while(true){
//取得被叫號(hào)碼
? ? ? ? val callerIndex = (Math.random() *phoneList.size).toInt
//從list中取出被叫號(hào)碼
? ? ? ? callee =phoneList(callerIndex)
//取得被叫號(hào)碼名稱
? ? ? ? calleeName =phoneNameMap.get(callee).get
if(!(callee == caller)){
breaks.break()
}
} )
val df=new DecimalFormat("0000")
val duration:String = df.format(30 *60 * Math.random())
val sb =new StringBuilder
val value =randomBuildTime(startTime,endTime)
sb.append(caller +",").append(callerName +",").append(callee +",").append(calleeName +",").append(value+",").append(duration+",").append(1)
println("日志:"+sb.toString())
sb.toString()
}
def writeLog(logPath:String): Unit = {
val osw =new OutputStreamWriter(new FileOutputStream(logPath),"UTF-8")
while (true){
//休閑時(shí)間
? ? ? Thread.sleep(5000)
//生成數(shù)據(jù)
? ? ? val log=prodtuc()
//把數(shù)據(jù)寫入到指定的路徑
? ? ? osw.write(log +"\n")
//一定要手動(dòng)flush才可以確保每條數(shù)據(jù)都寫入到文件一次
? ? ? osw.flush()
}
}
/**
? * 初始化數(shù)據(jù)
? */
? def initPhone() ={
phoneList.append("17078388295")
phoneList.append("13980337439")
phoneList.append("14575535933")
phoneList.append("19902496992")
phoneList.append("18549641558")
phoneList.append("17005930322")
phoneList.append("18468618874")
phoneList.append("18576581848")
phoneList.append("15978226424")
phoneList.append("15542823911")
phoneList.append("17526304161")
phoneList.append("15422018558")
phoneList.append("17269452013")
phoneList.append("17764278604")
phoneList.append("15711910344")
phoneList.append("15714728273")
phoneList.append("16061028454")
phoneList.append("16264433631")
phoneList.append("17601615878")
phoneList.append("15897468949")
phoneNameMap.put("17078388295","李雁")
phoneNameMap.put("13980337439","衛(wèi)藝")
phoneNameMap.put("14575535933","仰莉")
phoneNameMap.put("19902496992","陶欣悅")
phoneNameMap.put("18549641558","施梅梅")
phoneNameMap.put("17005930322","金虹霖")
phoneNameMap.put("18468618874","魏明艷")
phoneNameMap.put("18576581848","華貞")
phoneNameMap.put("15978226424","華啟倩")
phoneNameMap.put("15542823911","仲采綠")
phoneNameMap.put("17526304161","衛(wèi)丹")
phoneNameMap.put("15422018558","戚麗紅")
phoneNameMap.put("17269452013","何翠柔")
phoneNameMap.put("17764278604","錢溶艷")
phoneNameMap.put("15711910344","錢琳")
phoneNameMap.put("15714728273","繆靜欣")
phoneNameMap.put("16061028454","焦秋菊")
phoneNameMap.put("16264433631","呂訪琴")
phoneNameMap.put("17601615878","沈丹")
phoneNameMap.put("15897468949","褚美麗")
}
def main(args: Array[String]): Unit = {
//傳入存放數(shù)據(jù)的路徑
? ? if(args==null || args.length<=0){
println("沒有輸入路徑")
return
? ? }
val logPath=args(0)
println(s"存放日志的路徑:$logPath")
//初始化數(shù)據(jù)
? ? Producerlog.initPhone()
//數(shù)據(jù)寫入到指定的文件
? ? Producerlog.writeLog(logPath)
}
}