import java.io.PrintWriter
import java.text.SimpleDateFormat
import java.util.{Date, Properties}
import org.apache.kafka.clients.producer.{KafkaProducer, ProducerRecord}
import org.apache.kafka.common.serialization.StringSerializer
import scala.util.Random
object MockData {
def main(args: Array[String]): Unit = {
mock()
}
def mock(): Unit = {
val random = new Random()
//初始化输出流和kafka producer
val pw = initFile("d://result.txt")
val producer = initKafkaProducer()
//1.指定日期
val day = new SimpleDateFormat("yyyy-MM-dd").format(new Date())
//2.车牌
val locations = Array("粤","深","沪","京","湘","京","京")
for(i <- 0 until 3000) {
//locations随机获取
//A-Z : 65-90之间的随机数
//5位随机数
val car = locations(random.nextInt(locations.length)) + (65 + random.nextInt(26)).asInstanceOf[Char] + randomNum(5, random)
for (j <- 0 until random.nextInt(300)) {
//3.抓拍时间
val actionTime = day + " " + fillZero(2, 24, random) + ":" + fillZero(2, 60, random) + ":" + fillZero(2, 60, random)
//4.车速 0-200
val speed = random.nextInt(200) + 1
//5.道路 0-20,填充0
val roadId = fillZero(2, 20, random)
//6.卡口4位,前两位都填充0,后两位0-20
val monitorId = fillZero1(4, 20, random)
//7.摄像头5位,最高位是0
val cameraId = "0" + randomNum(4, random)
//8.区域0-20,填充0
val areaId = fillZero(2, 20, random)
val content = day + "\t" + monitorId + "\t" + cameraId + "\t" + car + "\t" + actionTime + "\t" + + speed + "\t" + roadId + "\t" + areaId
sendKafkaData(producer,content)
// sendFileData(pw, content)
Thread.sleep(50)
}
}
//关闭kafka producer和输出流
producer.close()
pw.close()
}
/**
* 初始化文件
* @param path
* @return
*/
def initFile(path: String): PrintWriter = {
new PrintWriter(path)
}
/**
* 发送数据到本地文件中
* @param pw
* @param content
*/
def sendFileData(pw: PrintWriter, content: String): Unit = {
pw.write(content + "\n")
}
/**
* 关闭输出流
* @param pw
*/
def closeFile (pw: PrintWriter): Unit = {
pw.close()
}
/**
* 初始化kafkaProducer
* @return
*/
def initKafkaProducer(): KafkaProducer[String, String] = {
val props = new Properties()
props.put("bootstrap.servers", "hadoop-senior.test.com:9092")
new KafkaProducer[String, String](props, new StringSerializer(), new StringSerializer())
}
/**
* 关闭kafka Producer
* @param producer
*/
def closeKafka (producer: KafkaProducer[String, String]): Unit = {
producer.close()
}
/**
* 发送消息到kafka topic_car张
* @param message
*/
def sendKafkaData(producer: KafkaProducer[String, String], message: String):Unit = {
producer.send(new ProducerRecord[String, String]("topic_car", message))
}
/**
* 填充位数(卡口)
* @param index 填充几位
* @param num 随机数范围
* @param random
* @return
*/
def fillZero1(index: Int, num: Int, random: Random): String = {
val randomNum = random.nextInt(num)
var str = randomNum.toString
if (randomNum < 20) {
str = ("%0" + index + "d").format(randomNum)
}
str
}
/**
* 填充位数
* @param index 填充几位
* @param num 随机数范围
* @param random
* @return
*/
def fillZero(index: Int, num: Int, random: Random): String = {
val randomNum = random.nextInt(num)
var str = randomNum.toString
if (randomNum < 10) {
str = ("%0" + index + "d").format(randomNum)
}
str
}
/**
* 生成几位的随机数
* @param index 位数
* @param random
* @return
*/
def randomNum(index: Int, random: Random): String = {
var str = ""
for (i <- 0 until index) {
str += random.nextInt(10)
}
str
}
}
scala生成数据使用kafka发送并保存在本地
最新推荐文章于 2024-04-07 09:59:59 发布