gpt4 book ai didi

apache-spark - 如何将基本身份验证传递给 Confluent Schema Registry?

转载 作者:行者123 更新时间:2023-12-03 21:17:23 24 4
gpt4 key购买 nike

我想从融合的云主题读取数据,然后写入另一个主题。

在本地主机上,我没有遇到任何重大问题。但是confluent cloud的schema registry需要传递一些我不知道如何输入的身份验证数据:

basic.auth.credentials.source=USER_INFO

schema.registry.basic.auth.user.info=:

schema.registry.url=https://xxxxxxxxxx.confluent.cloudBlockquote



以下是当前代码:
import com.databricks.spark.avro.SchemaConverters
import io.confluent.kafka.schemaregistry.client.{CachedSchemaRegistryClient, SchemaRegistryClient}
import io.confluent.kafka.serializers.AbstractKafkaAvroDeserializer
import org.apache.avro.Schema
import org.apache.avro.generic.GenericRecord
import org.apache.spark.sql.SparkSession

object AvroConsumer {
private val topic = "transactions"
private val kafkaUrl = "http://localhost:9092"
private val schemaRegistryUrl = "http://localhost:8081"

private val schemaRegistryClient = new CachedSchemaRegistryClient(schemaRegistryUrl, 128)
private val kafkaAvroDeserializer = new AvroDeserializer(schemaRegistryClient)

private val avroSchema = schemaRegistryClient.getLatestSchemaMetadata(topic + "-value").getSchema
private var sparkSchema = SchemaConverters.toSqlType(new Schema.Parser().parse(avroSchema))

def main(args: Array[String]): Unit = {
val spark = SparkSession
.builder
.appName("ConfluentConsumer")
.master("local[*]")
.getOrCreate()

spark.sparkContext.setLogLevel("ERROR")

spark.udf.register("deserialize", (bytes: Array[Byte]) =>
DeserializerWrapper.deserializer.deserialize(bytes)
)

val kafkaDataFrame = spark
.readStream
.format("kafka")
.option("kafka.bootstrap.servers", kafkaUrl)
.option("subscribe", topic)
.load()

val valueDataFrame = kafkaDataFrame.selectExpr("""deserialize(value) AS message""")

import org.apache.spark.sql.functions._

val formattedDataFrame = valueDataFrame.select(
from_json(col("message"), sparkSchema.dataType).alias("parsed_value"))
.select("parsed_value.*")

formattedDataFrame
.writeStream
.format("console")
.option("truncate", false)
.start()
.awaitTermination()
}

object DeserializerWrapper {
val deserializer = kafkaAvroDeserializer
}

class AvroDeserializer extends AbstractKafkaAvroDeserializer {
def this(client: SchemaRegistryClient) {
this()
this.schemaRegistry = client
}

override def deserialize(bytes: Array[Byte]): String = {
val genericRecord = super.deserialize(bytes).asInstanceOf[GenericRecord]
genericRecord.toString
}
}

}

我想我必须将此身份验证数据传递给 CachedSchemaRegistryClient,但我不确定是否这样做以及如何传递。

最佳答案

我终于能够通过属性。

我留下了给出解决方案的台词。

val restService = new RestService(schemaRegistryURL)

val props = Map(
"basic.auth.credentials.source" -> "USER_INFO",
"schema.registry.basic.auth.user.info" -> "secret:secret"
).asJava

var schemaRegistryClient = new CachedSchemaRegistryClient(restService, 100, props)

关于apache-spark - 如何将基本身份验证传递给 Confluent Schema Registry?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58835635/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com