【问题标题】:How to create a KinesisSink for Spark Structural Streaming如何为 Spark 结构化流创建 Kinesis Sink
【发布时间】:2018-03-13 11:22:25
【问题描述】:

我在 Databricks 上使用 Spark 2.2 并尝试实现 Kinesis 接收器以从 Spark 写入 Kinesis 流。

我正在使用以下从这里提供的示例https://docs.databricks.com/_static/notebooks/structured-streaming-kinesis-sink.html

/**
* A simple Sink that writes to the given Amazon Kinesis `stream` in the given    `region`. For authentication, users may provide
* `awsAccessKey` and `awsSecretKey`, or use IAM Roles when launching their cluster.
*
* This Sink takes a two column Dataset, with the columns being the `partitionKey`, and the `data` respectively.
* We will buffer data up to `maxBufferSize` before flushing to Kinesis in order to reduce cost.
*/
class KinesisSink(
    stream: String,
    region: String,
    awsAccessKey: Option[String] = None,
    awsSecretKey: Option[String] = None) extends ForeachWriter[(String, Array[Byte])] { 

 // Configurations
 private val maxBufferSize = 500 * 1024 // 500 KB

 private var client: AmazonKinesis = _
 private val buffer = new ArrayBuffer[PutRecordsRequestEntry]()
 private var bufferSize: Long = 0L

 override def open(partitionId: Long, version: Long): Boolean = {
   client = createClient
   true
 }

 override def process(value: (String, Array[Byte])): Unit = {
   val (partitionKey, data) = value
   // Maximum of 500 records can be sent with a single `putRecords` request
   if ((data.length + bufferSize > maxBufferSize && buffer.nonEmpty) ||    buffer.length == 500) {
  flush()
}
buffer += new PutRecordsRequestEntry().withPartitionKey(partitionKey).withData(ByteBuffer.wrap(data))
bufferSize += data.length
 }

 override def close(errorOrNull: Throwable): Unit = {
   if (buffer.nonEmpty) {
     flush()
   }
   client.shutdown()
 }

 /** Flush the buffer to Kinesis */
 private def flush(): Unit = {
   val recordRequest = new PutRecordsRequest()
     .withStreamName(stream)
     .withRecords(buffer: _*)

   client.putRecords(recordRequest)
   buffer.clear()
   bufferSize = 0
 }

 /** Create a Kinesis client. */
 private def createClient: AmazonKinesis = {
   val cli = if (awsAccessKey.isEmpty || awsSecretKey.isEmpty) {
     AmazonKinesisClientBuilder.standard()
       .withRegion(region)
       .build()
   } else {
     AmazonKinesisClientBuilder.standard()
       .withRegion(region)
       .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(awsAccessKey.get, awsSecretKey.get)))
       .build()
   }
   cli
 }
}

然后我使用

实现 KinesisSink 类
val kinesisSink = new KinesisSink("us-east-1", "MyStream", Option("xxx..."), Option("xxx..."))

最后我使用这个接收器创建了一个流。此 KinesisSink 采用两列数据集,列分别为 partitionKeydata

case class MyData(partitionKey: String, data: Array[Byte])

val newsDataDF = kinesisDF
   .selectExpr("apinewsseqid", "fullcontent").as[MyData]
   .writeStream
   .outputMode("append")
   .foreach(kinesisSink)
   .start

但我仍然收到以下错误

error: type mismatch;
found   : KinesisSink
required: org.apache.spark.sql.ForeachWriter[MyData]
   .foreach(kinesisSink)

【问题讨论】:

  • 我看到的第一件事是你对 KinesisSink 的实现与类的签名不匹配。您首先提供了区域,而班级首先需要流。应该是val ks = new KinesisSink("MyStream", "us-east-1",...)

标签: apache-spark spark-streaming amazon-kinesis


【解决方案1】:

您需要更改KinesisSink.process 方法的签名,它应该采用您的自定义MyData 对象,然后从那里提取partitionKeydata

【讨论】:

    【解决方案2】:

    我使用了与 databricks 提供的完全相同的 KinesisSink,并通过使用

    创建数据集使其工作
    val dataset = df.selectExpr("CAST(rand() AS STRING) as partitionKey","message_bytes").as[(String, Array[Byte])]
    

    并使用数据集写入运动流

    val query = dataset
    .writeStream
    .foreach(kinesisSink)
    .start()
    .awaitTermination()
    

    当我使用dataset.selectExpr("partitionKey","message_bytes") 时,我得到了相同的类型不匹配错误:

    错误:类型不匹配;
    找到:KinesisSink
    必需:org.apache.spark.sql.ForeachWriter[(String, Array[Byte])]
    .foreach(kinesisSink)

    在这种情况下不需要selectExpr,因为数据集驱动 ForeachWriter 的数据类型。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-22
      • 2020-04-28
      • 2019-09-05
      • 1970-01-01
      • 1970-01-01
      • 2022-10-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多