scala生成数据使用kafka发送并保存在本地

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
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值