SparkStreaming安全消费Kafka数据
前言在这之前做SparkStreaming连接Kafka,我会这么写:val sparkConf = new SparkConf().setAppName("Spark2Kafka")val ssc = new StreamingContext(sparkConf, Seconds(5))ssc.checkpoint("/xxx/xxx")val kafkaParameters = Ma...
·
前言
在这之前做SparkStreaming连接Kafka,我会这么写:
val sparkConf = new SparkConf().setAppName("Spark2Kafka")
val ssc = new StreamingContext(sparkConf, Seconds(5))
ssc.checkpoint("/xxx/xxx")
val kafkaParameters = Map[String, String]("metadata.broker.list" -> "Master:9092,Worker1:9092,Worker2:9092")
val topics = Set[String]("TOPIC_NAME")
val streaming = KafkaUtils.createDirectStream[String, String, StringDecoder, StringDecoder](ssc, kafkaParameters, topics)
不过最近我们需要读取数据中心的Kafka数据,人家直接给了我三个值:username、password、group_id,我刚开始还一头雾水:这是干啥啊?于是还按照之前的写法,发现:
INFO consumer.SimpleConsumer: Reconnect due to error:
java.nio.channels.ClosedChannelException
原因分析
数据中心给的这三个字段看样子是用来做登录验证的,于是参考了官网链接
原来登录别人的Kafka需要SSL协议,这个时候就需要用户名和密码了,并且:0.8版本是不支持的,因此要依赖1.0版本的
详细流程
- 依赖
groupId = org.apache.spark
artifactId = spark-streaming-kafka-0-10_2.12
version = 2.4.3
- 写spark代码参考官网
package cn.unisk
import org.apache.kafka.common.serialization.StringDeserializer
import org.apache.spark.SparkConf
import org.apache.spark.streaming.{Seconds, StreamingContext}
import org.apache.spark.streaming.kafka010._
import org.apache.spark.streaming.kafka010.LocationStrategies.PreferConsistent
import org.apache.spark.streaming.kafka010.ConsumerStrategies.Subscribe
object Spark2Kafka {
def main(args: Array[String]) {
val kafkaParams = Map[String, Object](
"bootstrap.servers" -> "10.161.xx.xxx:9092,10.161.xx.xxx:9092,10.161.xx.xxx:9092",
"key.deserializer" -> classOf[StringDeserializer],
"security.protocol" -> "SASL_PLAINTEXT",
"sasl.mechanism" -> "PLAIN",
"value.deserializer" -> classOf[StringDeserializer],
"auto.offset.reset" -> "latest",
"group.id" -> "EC_group",
"enable.auto.commit" -> (false: java.lang.Boolean)
)
val sparkConf = new SparkConf().setAppName("Spark2_KafkaSASL")
val streamingContext = new StreamingContext(sparkConf, Seconds(5))
val topics = Array("CB_JF_ACT1")
val stream = KafkaUtils.createDirectStream[String, String](
streamingContext,
PreferConsistent,
Subscribe[String, String](topics, kafkaParams)
)
// Get the lines, split them into words, count the words and print
stream.map(record => (record.key, record.value))
stream.print()
// Start the computation
streamingContext.start()
streamingContext.awaitTermination()
}
}
- 在提交spark代码的服务器上创建jass.conf文件,内容如下
KafkaClient {
org.apache.kafka.common.security.plain.PlainLoginModule required
username="xxxx"
password="xxxx";
};
- 提交spark代码(和平时有点区别哦)
注意–先要做这样一个操作:
export SPARK_KAFKA_VERSION=0.10
如果不做,直接报错:
Caused by: java.lang.ClassNotFoundException: org.apache.spark.streaming.kafka010.KafkaUtils$
spark2-submit \
--master yarn \
--deploy-mode client \
--files /home/analysis/jass.conf \
--driver-java-options "-Djava.security.auth.login.config=./jass.conf" \
--conf "spark.executor.extraJavaOptions=-Djava.security.auth.login.config=./jass.conf" \
--class cn.unisk.Spark2Kafka \
/home/analysis/ss.jar
这里比平时多加了三个配置
--files /home/analysis/jass.conf \
--driver-java-options "-Djava.security.auth.login.config=./jass.conf" \
--conf "spark.executor.extraJavaOptions=-Djava.security.auth.login.config=./jass.conf" \
主要日志信息
19/05/21 15:59:53 INFO kafka010.DirectKafkaInputDStream: Initialized and validated org.apache.spark.streaming.kafka010.DirectKafkaInputDStream@7cee5299
19/05/21 15:59:53 INFO dstream.ForEachDStream: Slide time = 5000 ms
19/05/21 15:59:53 INFO dstream.ForEachDStream: Storage level = Serialized 1x Replicated
19/05/21 15:59:53 INFO dstream.ForEachDStream: Checkpoint interval = null
19/05/21 15:59:53 INFO dstream.ForEachDStream: Remember interval = 5000 ms
19/05/21 15:59:53 INFO dstream.ForEachDStream: Initialized and validated org.apache.spark.streaming.dstream.ForEachDStream@16a44d14
19/05/21 15:59:53 INFO consumer.ConsumerConfig: ConsumerConfig values:
metric.reporters = []
metadata.max.age.ms = 300000
partition.assignment.strategy = [org.apache.kafka.clients.consumer.RangeAssignor]
reconnect.backoff.ms = 50
sasl.kerberos.ticket.renew.window.factor = 0.8
max.partition.fetch.bytes = 1048576
bootstrap.servers = [10.161.25.240:9092, 10.161.25.241:9092, 10.161.25.242:9092]
ssl.keystore.type = JKS
enable.auto.commit = false
sasl.mechanism = PLAIN
interceptor.classes = null
exclude.internal.topics = true
ssl.truststore.password = null
client.id =
ssl.endpoint.identification.algorithm = null
max.poll.records = 2147483647
check.crcs = true
request.timeout.ms = 40000
heartbeat.interval.ms = 3000
auto.commit.interval.ms = 5000
receive.buffer.bytes = 65536
ssl.truststore.type = JKS
ssl.truststore.location = null
ssl.keystore.password = null
fetch.min.bytes = 1
send.buffer.bytes = 131072
value.deserializer = class org.apache.kafka.common.serialization.StringDeserializer
group.id = EC_group
retry.backoff.ms = 100
ssl.secure.random.implementation = null
sasl.kerberos.kinit.cmd = /usr/bin/kinit
sasl.kerberos.service.name = null
sasl.kerberos.ticket.renew.jitter = 0.05
ssl.trustmanager.algorithm = PKIX
ssl.key.password = null
fetch.max.wait.ms = 500
sasl.kerberos.min.time.before.relogin = 60000
connections.max.idle.ms = 540000
session.timeout.ms = 30000
metrics.num.samples = 2
key.deserializer = class org.apache.kafka.common.serialization.StringDeserializer
ssl.protocol = TLS
ssl.provider = null
ssl.enabled.protocols = [TLSv1.2, TLSv1.1, TLSv1]
ssl.keystore.location = null
ssl.cipher.suites = null
security.protocol = SASL_PLAINTEXT
ssl.keymanager.algorithm = SunX509
metrics.sample.window.ms = 30000
auto.offset.reset = latest
19/05/21 15:59:54 INFO authenticator.AbstractLogin: Successfully logged in.
19/05/21 15:59:54 INFO consumer.ConsumerConfig: ConsumerConfig values:
metric.reporters = []
metadata.max.age.ms = 300000
partition.assignment.strategy = [org.apache.kafka.clients.consumer.RangeAssignor]
reconnect.backoff.ms = 50
sasl.kerberos.ticket.renew.window.factor = 0.8
max.partition.fetch.bytes = 1048576
bootstrap.servers = [10.161.25.xxx:9092, 10.161.25.xxx:9092, 10.161.25.xxx:9092]
ssl.keystore.type = JKS
enable.auto.commit = false
sasl.mechanism = PLAIN
interceptor.classes = null
exclude.internal.topics = true
ssl.truststore.password = null
client.id = consumer-1
ssl.endpoint.identification.algorithm = null
max.poll.records = 2147483647
check.crcs = true
request.timeout.ms = 40000
heartbeat.interval.ms = 3000
auto.commit.interval.ms = 5000
receive.buffer.bytes = 65536
ssl.truststore.type = JKS
ssl.truststore.location = null
ssl.keystore.password = null
fetch.min.bytes = 1
send.buffer.bytes = 131072
value.deserializer = class org.apache.kafka.common.serialization.StringDeserializer
group.id = EC_group
retry.backoff.ms = 100
ssl.secure.random.implementation = null
sasl.kerberos.kinit.cmd = /usr/bin/kinit
sasl.kerberos.service.name = null
sasl.kerberos.ticket.renew.jitter = 0.05
ssl.trustmanager.algorithm = PKIX
ssl.key.password = null
fetch.max.wait.ms = 500
sasl.kerberos.min.time.before.relogin = 60000
connections.max.idle.ms = 540000
session.timeout.ms = 30000
metrics.num.samples = 2
key.deserializer = class org.apache.kafka.common.serialization.StringDeserializer
ssl.protocol = TLS
ssl.provider = null
ssl.enabled.protocols = [TLSv1.2, TLSv1.1, TLSv1]
ssl.keystore.location = null
ssl.cipher.suites = null
security.protocol = SASL_PLAINTEXT
ssl.keymanager.algorithm = SunX509
metrics.sample.window.ms = 30000
auto.offset.reset = latest
19/05/21 15:59:54 INFO utils.AppInfoParser: Kafka version : 0.10.0-kafka-2.1.0
19/05/21 15:59:54 INFO utils.AppInfoParser: Kafka commitId : unknown
......
-------------------------------------------
Time: 1558425595000 ms
-------------------------------------------
ConsumerRecord(topic = CB_JF_ACT1, partition = 8, offset = 1764926986, CreateTime = 1558425597715, checksum = 1171090595, serialized key size = 22, serialized value size = 247, key = JF_ACT1,8,2142980219,0, value = ACTJF115303700642285000001299800046163040,03000001299800046163040UCR_ACT1.TF_O_LEAVEREALFEEU2019-05-18 01:29:18.00341905532553218619012346215532861901234621553225845585531930192041152019-05-18:09:29:182019-05-18:07:27:08)
ConsumerRecord(topic = CB_JF_ACT1, partition = 8, offset = 1764926987, CreateTime = 1558425597716, checksum = 581909573, serialized key size = 22, serialized value size = 249, key = JF_ACT1,8,2142980220,0, value = ACTJF115303700642294000001299800046167850,00000001299800046167850UCR_ACT1.TF_O_LEAVEREALFEEU2019-05-18 01:29:18.0034190478047801111801256415478011180125641547802101031014333393335341152019-05-18:09:29:182019-05-18:09:24:46)
更多推荐
已为社区贡献3条内容
所有评论(0)