I'm a newcomer to scala and I use the below code to get lines(rows) one by one from CSV(with 20 records) using for loop and I send those to Kafka
for (line <- FileStream.getLines) {
val today = Calendar.getInstance.getTime
val formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
val key = UUID.randomUUID().toString().split("-")(0)
val value = formatter.format(today) + "," + line
val data = new ProducerRecord[String, String](topic, key, value)
println(data.value())
producer.send(data)
}
What I need is after the loop reads 5 rows i.e, after it reaches 5 loops it should sleep for 5 secs
and then it should continue from where it left(from 6th row).
The output I expect is:
2012-08-13T00:00:00.000Z,92.29,92.59,91.74,92.4,2075391.0,MMM
2012-08-14T00:00:00.000Z,92.36,92.5,92.01,92.3,1843476.0,MMM
2012-08-15T00:00:00.000Z,92.0,92.74,91.94,92.54,1983395.0,MMM
2012-08-16T00:00:00.000Z,92.75,93.87,92.21,93.74,3395145.0,MMM
2012-08-17T00:00:00.000Z,93.93,94.3,93.59,94.24,3069513.0,MMM
------5 seconds sleep------
2012-08-20T00:00:00.000Z,94.0,94.17,93.55,93.89,1640008.0,MMM
2012-08-21T00:00:00.000Z,93.98,94.1,92.99,93.21,2302988.0,MMM
2012-08-22T00:00:00.000Z,92.56,93.36,92.43,92.68,2463908.0,MMM
2012-08-23T00:00:00.000Z,92.65,92.68,91.79,91.98,1823757.0,MMM
2012-08-24T00:00:00.000Z,92.03,92.97,91.94,92.83,1945796.0,MMM
So how can we achieve that?