-
Notifications
You must be signed in to change notification settings - Fork 2
/
BeerProducer.scala
70 lines (58 loc) · 2.59 KB
/
BeerProducer.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.logicalgenetics.beer
import java.util.Properties
import com.logicalgenetics.Config
import io.confluent.kafka.serializers.KafkaAvroSerializer
import org.apache.avro.Schema
import org.apache.avro.generic.{GenericData, GenericRecord}
import org.apache.kafka.clients.producer.{KafkaProducer, ProducerRecord}
import org.apache.kafka.common.serialization.StringSerializer
import scala.io.Source
object BeerProducer {
val beerFile = "data/beers.csv"
val topic = "beers"
lazy val schema: Schema = new Schema.Parser().parse("""
{
"namespace": "logicalgenetics.beer",
"type": "record",
"name": "beer",
"fields": [
{"name": "row", "type": "int", "default": 0},
{"name": "abv", "type": ["null", "double"], "default": null},
{"name": "ibu", "type": ["null", "double"], "default": null},
{"name": "id", "type": "int", "default": 0},
{"name": "name", "type": "string", "default": "UNKNOWN"},
{"name": "style", "type": "string", "default": "UNKNOWN"},
{"name": "brewery_id", "type": "int", "default": 0},
{"name": "ounces", "type": "double", "default": 0.0}
]
}""")
lazy val producer : KafkaProducer[String, GenericRecord] = {
val properties = new Properties()
properties.put("bootstrap.servers", Config.servers)
properties.put("schema.registry.url", Config.schemaRegistry)
properties.put("key.serializer", classOf[StringSerializer])
properties.put("value.serializer", classOf[KafkaAvroSerializer])
new KafkaProducer[String, GenericRecord](properties)
}
def createBeerFrom(line : String) : GenericRecord = {
val Array(row,abv,ibu,id,name,style,brewery_id,ounces) = line.split(',')
val beer: GenericRecord = new GenericData.Record(schema)
beer.put("row", row.toInt)
beer.put("abv", abv match { case "" => null; case x => x.toDouble})
beer.put("ibu", ibu match { case "" => null; case x => x.toDouble})
beer.put("id", id.toInt)
beer.put("name", name)
beer.put("style", style)
beer.put("brewery_id", brewery_id.toInt)
beer.put("ounces", ounces.toDouble)
beer
}
def main(args: Array[String]): Unit = {
val bufferedSource = Source.fromFile(beerFile)
for (line <- bufferedSource.getLines.drop(1)) {
// The call to 'get' here forces us to be synchronous by waiting for the send to complete
producer.send(new ProducerRecord[String, GenericRecord](topic, createBeerFrom(line))).get()
}
bufferedSource.close()
}
}