-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIngest.scala
More file actions
60 lines (47 loc) · 1.95 KB
/
Copy pathIngest.scala
File metadata and controls
60 lines (47 loc) · 1.95 KB
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
package com.svend.demo.ingestion
import java.nio.charset.StandardCharsets
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.{HttpRequest, HttpResponse, Uri}
import akka.kafka.ProducerSettings
import akka.kafka.scaladsl.Producer
import akka.stream.ThrottleMode
import akka.stream.alpakka.sse.scaladsl.EventSource
import org.apache.kafka.clients.producer.ProducerRecord
import org.apache.kafka.common.header.Header
import org.apache.kafka.common.header.internals.RecordHeader
import org.apache.kafka.common.serialization.StringSerializer
import scala.concurrent.Future
import scala.concurrent.duration._
import scala.jdk.CollectionConverters._
import scala.language.postfixOps
object Ingest extends App {
val topic = "test-topic"
implicit val system = ActorSystem()
val send: HttpRequest => Future[HttpResponse] = Http().singleRequest(_)
def parse(row: String): (String, String) = (row.drop(15), row)
// input rowId is written as part of the kafka headers => let's find out the latest one committed
val latestCommittedRowId = KafkaRowIdReader.latestCommittedRowId("localhost:9092", topic)
println(s"latestCommittedRowId: $latestCommittedRowId")
// everything default :)
val producerSettings =
ProducerSettings(system, new StringSerializer, new StringSerializer)
.withBootstrapServers("localhost:9092")
val httpSource = EventSource(
uri = Uri(s"http://localhost:8080/events?fromRowId=$latestCommittedRowId"),
send,
initialLastEventId = None,
retryDelay = 1.second)
httpSource
.throttle(elements = 10, per = 1.second, maximumBurst = 1, ThrottleMode.Shaping)
.map(event => parse(event.data))
.map { case (rowId, row) =>
new ProducerRecord[String, String](
topic,
null,
rowId, row,
List(new RecordHeader("db.row.id", rowId.getBytes(StandardCharsets.UTF_8)).asInstanceOf[Header]).asJava
)
}
.runWith(Producer.plainSink(producerSettings))
}