Spark Streaming输出操作【Output Operations on DStreams】

Spark Streaming输出操作 可以将DStream中的数据写入到外部的存储系统,如:数据库或者文件系统

Output OperationMeaning
print()Prints the first ten elements of every batch of data in a DStream on the driver node running the streaming application. This is useful for development and debugging. Python API This is called pprint() in the Python API.
saveAsTextFiles(prefix, [suffix])Save this DStream’s contents as text files. The file name at each batch interval is generated based on prefix and suffix: “prefix-TIME_IN_MS[.suffix]”.
saveAsObjectFiles(prefix, [suffix])Save this DStream’s contents as SequenceFiles of serialized Java objects. The file name at each batch interval is generated based on prefix and suffix: “prefix-TIME_IN_MS[.suffix]”. Python API This is not available in the Python API.
saveAsHadoopFiles(prefix, [suffix])Save this DStream’s contents as Hadoop files. The file name at each batch interval is generated based on prefix and suffix: “prefix-TIME_IN_MS[.suffix]”. Python API This is not available in the Python API.
foreachRDD(func)The most generic output operator that applies a function, func, to each RDD generated from the stream. This function should push the data in each RDD to an external system, such as saving the RDD to files, or writing it over the network to a database. Note that the function func is executed in the driver process running the streaming application, and will usually have RDD actions in it that will force the computation of the streaming RDDs.
print()

打印DStream每个批次中前10个元素

package example1
import org.apache.spark.SparkConf
import org.apache.spark.streaming.{Seconds, StreamingContext}

object QuickExample {
  def main(args: Array[String]): Unit = {
    // StreamingContext是所有Spark Streaming应用的入口
    val conf = new SparkConf().setMaster("local[2]").setAppName("quick example")
    val ssc = new StreamingContext(conf, Seconds(5))
    // 通过TCP Source创建DStream对象
    val lines = ssc.socketTextStream("spark", 9999)
    // 将此DStream中生成的每个RDD的前十个元素打印到控制台
    lines.flatMap(_.split(" ")).map((_, 1)).reduceByKey(_ + _).print()
    // 启动计算
    ssc.start()
    // 等待计算终止
    ssc.awaitTermination()
  }
}
saveAsTextFiles(prefix, [suffix])

将DStream的内容保存到文本文件中

// 保存到文本文件中
lines.flatMap(_.split(" ")).map((_, 1)).reduceByKey(_ + _).saveAsTextFiles("result", "txt")

在这里插入图片描述

saveAsHadoopFiles(prefix, [suffix])

将DStream的内容保存到Hadoop文件系统中

  // --------------------------------------------------------------------------
   // 保存到hadoop文件中
   val configuration = new Configuration()
   configuration.set("fs.defaultFS", "hdfs://gaozhy:9000")
   val jobConf = new JobConf(configuration)
   lines.flatMap(_.split(" ")).map((_, 1)).reduceByKey(_ + _)
      .saveAsNewAPIHadoopFiles("result", "log", classOf[Text], classOf[IntWritable], classOf[TextOutputFormat[Text, IntWritable]], jobConf)

在这里插入图片描述

foreachRDD(func)

遍历处理DStream中的批次对应的RDD,可以将每个RDD的数据写入到外部的存储系统,如数据库、Redis等

如:将Stream的计算结果存储到MySQL中

// --------------------------------------------------------------------------
// 保存到数据库中
lines.flatMap(_.split(" ")).map((_, 1)).reduceByKey(_ + _).foreachRDD(rdd => {
  rdd.foreachPartition(partitionOfRecords => {
    classOf[Driver]
    val connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root")
    val selectSql: String = "select *  from t_word where word = ?"
    val updateSql = "update t_word set count = ? where word = ?"
    val insertSql = "insert t_word(word,count) values(?,1)"
    partitionOfRecords.foreach(record => {
      val pstm = connection.prepareStatement(selectSql)
      pstm.setString(1, record._1)
      val rs = pstm.executeQuery()
      // word存在
      if (rs.next()) {
        val count = rs.getInt("count")
        val updateStatement = connection.prepareStatement(updateSql)
        updateStatement.setInt(1, count + record._2)
        updateStatement.setString(2, record._1)
        updateStatement.executeUpdate()
      } else {
        val insertStatement = connection.prepareStatement(insertSql)
        insertStatement.setString(1, record._1)
        insertStatement.executeUpdate()
      }
    })
    connection.close()
  })
})

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值