【问题标题】:how to convert directstream from kafka into data frames in spark 1.3.0如何将来自kafka的directstream转换为spark 1.3.0中的数据帧
【发布时间】:2015-11-07 06:37:29
【问题描述】:

创建如下直接流后:

val events = KafkaUtils.createDirectStream[String, String, StringDecoder, StringDecoder](
  ssc, kafkaParams, topicsSet)

我想将上面的流转换成数据帧,这样我就可以在它上面运行 hive 查询。谁能解释一下这是如何实现的?我使用的是火花版本 1.3.0

【问题讨论】:

    标签: apache-spark hive streaming apache-kafka


    【解决方案1】:

    Spark Streaming programming guide 中所述,试试这个:

    import org.apache.spark.sql.SQLContext
    object SQLContextSingleton {
      @transient private var instance: SQLContext = null
    
      // Instantiate SQLContext on demand
      def getInstance(sparkContext: SparkContext): SQLContext = synchronized {
        if (instance == null) {
          instance = new SQLContext(sparkContext)
        }
        instance
      }
    }
    case class Row(key: String, value: String)
    eventss.foreachRDD { rdd =>
      val sqlContext = SQLContextSingleton.getInstance(rdd.sparkContext)
      import sqlContext.implicits._
      val dataFrame = rdd.map {case (key, value) => Row(key, value)}.toDF()
      dataFrame.show()
    }
    

    【讨论】:

      猜你喜欢
      • 2015-11-05
      • 1970-01-01
      • 1970-01-01
      • 2018-09-01
      • 1970-01-01
      • 2016-01-03
      • 1970-01-01
      • 2023-03-31
      • 2019-09-18
      相关资源
      最近更新 更多