-
Notifications
You must be signed in to change notification settings - Fork 90
/
KafkaPlugin.scala
95 lines (80 loc) · 3.87 KB
/
KafkaPlugin.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
* Copyright 2020 ABSA Group Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package za.co.absa.spline.harvester.plugin.embedded
import org.apache.kafka.clients.consumer.KafkaConsumer
import org.apache.kafka.common.TopicPartition
import org.apache.spark.sql.execution.datasources.{LogicalRelation, SaveIntoDataSourceCommand}
import org.apache.spark.sql.sources.BaseRelation
import za.co.absa.spline.commons.reflect.ReflectionUtils.extractValue
import za.co.absa.spline.commons.reflect.extractors.SafeTypeMatchingExtractor
import za.co.absa.spline.harvester.builder.SourceIdentifier
import za.co.absa.spline.harvester.plugin.Plugin.{Precedence, ReadNodeInfo, WriteNodeInfo}
import za.co.absa.spline.harvester.plugin.embedded.KafkaPlugin._
import za.co.absa.spline.harvester.plugin.{BaseRelationProcessing, Plugin, RelationProviderProcessing}
import java.util.Properties
import javax.annotation.Priority
import scala.collection.JavaConverters._
import scala.util.Try
@Priority(Precedence.Normal)
class KafkaPlugin
extends Plugin
with BaseRelationProcessing
with RelationProviderProcessing {
override def baseRelationProcessor: PartialFunction[(BaseRelation, LogicalRelation), ReadNodeInfo] = {
case (`_: KafkaRelation`(kr), _) =>
val options = extractValue[Map[String, String]](kr, "sourceOptions")
// org.apache.spark.sql.kafka010.ConsumerStrategy - private classes cannot be used directly
val consumerStrategy = extractValue[AnyRef](kr, "strategy")
def tryAssignStrategy: Try[Seq[String]] =
Try(extractValue[Array[TopicPartition]](consumerStrategy, "partitions"))
.map(partitions => partitions.map(_.topic()))
def trySubscribeStrategy: Try[Seq[String]] =
Try(extractValue[Seq[String]](consumerStrategy, "topics"))
def trySubscribePatternStrategy: Try[Seq[String]] =
Try(extractValue[String](consumerStrategy, "topicPattern"))
.map(pattern => kafkaTopics(options("kafka.bootstrap.servers")).filter(_.matches(pattern)))
val topics: Seq[String] =
tryAssignStrategy
.orElse(trySubscribeStrategy)
.orElse(trySubscribePatternStrategy)
.get
val sourceId = SourceIdentifier(Some("kafka"), topics.map(asURI): _*)
ReadNodeInfo(
sourceId,
options ++ Map(
"startingOffsets" -> extractValue[AnyRef](kr, "startingOffsets"),
"endingOffsets" -> extractValue[AnyRef](kr, "endingOffsets")
))
}
override def relationProviderProcessor: PartialFunction[(AnyRef, SaveIntoDataSourceCommand), WriteNodeInfo] = {
case (rp, cmd) if cmd.options.contains("kafka.bootstrap.servers") =>
val uri = asURI(cmd.options("topic"))
WriteNodeInfo(SourceIdentifier(Option(rp), uri), cmd.mode, cmd.query, cmd.options)
}
}
object KafkaPlugin {
object `_: KafkaRelation` extends SafeTypeMatchingExtractor[AnyRef]("org.apache.spark.sql.kafka010.KafkaRelation")
private def kafkaTopics(bootstrapServers: String): Seq[String] = {
val kc = new KafkaConsumer(new Properties {
put("bootstrap.servers", bootstrapServers)
put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer")
put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer")
})
try kc.listTopics.keySet.asScala.toSeq
finally kc.close()
}
private def asURI(topic: String) = s"kafka:$topic"
}