我有一个流式消费程序,正在阅读一个主题,并希望保存在本地系统中。 我正在将spark master提交为local [*],但是问题是输出没有保存。程序运行时,我可以在_temporary文件夹中看到输出,但是当我停止程序时,文件夹消失了。可以请任何人让我知道为什么会这样以及如何解决吗?
谢谢
这是我的代码:
package com.org.spark
import org.apache.spark.SparkConf
import org.apache.spark.streaming.StreamingContext
import org.apache.spark.streaming.Seconds
import org.apache.spark.streaming.kafka010.KafkaUtils
import org.apache.spark.sql.SparkSession
import org.apache.spark.streaming.kafka010.LocationStrategies.PreferConsistent
import org.apache.spark.streaming.kafka010.ConsumerStrategies.Subscribe
import org.apache.kafka.common.serialization.StringDeserializer
import org.apache.spark.streaming.kafka010.HasOffsetRanges
import org.apache.spark.streaming.kafka010.OffsetRange
import org.apache.kafka.clients.consumer.ConsumerConfig
import org.apache.spark.sql.SaveMode
import org.apache.spark.SparkContext
import java.util.Date
import java.text.SimpleDateFormat
//import org.apache.spark.TaskContext
object SparkKafkaDemo {
def main(args: Array[String]) = {
val kafkaParams = Map[String, Object](
"bootstrap.servers" -> “kafka-broker1:9094,kafka-broker2:9094",
"key.deserializer" -> classOf[StringDeserializer],
"value.deserializer" -> classOf[StringDeserializer],
"security.protocol" -> "SASL_SSL",
"sasl.mechanism" -> "PLAIN",
"group.id" -> "test-consumer-group",
"auto.offset.reset" -> "earliest",
"enable.auto.commit" -> (false: java.lang.Boolean),
"ssl.truststore.location" -> “/path/to/truststore/file.truststore",
"ssl.truststore.password" -> “xxxxxxxx”,
"ssl.keystore.location" -> “/path/to/keystore/file.keystore",
"ssl.keystore.password" -> “xxxxxxxx”,
"ssl.key.password" -> “xxxxxxxxx”
)
val topics = Array(“topic-name”)
val sparkConf = new SparkConf().setAppName("KafkaTest")
val sc=new SparkContext(sparkConf)
sc.setLogLevel("ERROR")
val streamingContext = new StreamingContext(sc, Seconds(30))
val kafkaStream = KafkaUtils.createDirectStream[String, String](
streamingContext,
PreferConsistent,
Subscribe[String, String](topics, kafkaParams)
)
val sqlContext = new org.apache.spark.sql.SQLContext(sc)
import sqlContext.implicits._
val result = kafkaStream.map(record => (record.topic(), record.value(), record.offset(), record.key()))
result.print()
result.foreachRDD(
rdd => {
val rdd1 = rdd.map(line => (line._3,line._2))
val today = new Date()
val date = new SimpleDateFormat("dd-MM-yyyy").format(today)
rdd1.saveAsTextFile("file:///path/to/output/"+"/"+date+"/")
}
)
streamingContext.start()
streamingContext.awaitTermination()
}
}