火花如何将[JSONOBJECT]转到数据集



我正在从类型com.google.google.gson.jsonobject的RDD读取数据。试图将其转换为数据集,但没有线索如何做到这一点。

import com.google.gson.{JsonParser}
import org.apache.hadoop.io.LongWritable
import org.apache.spark.sql.{SparkSession}
object tmp {
  class people(name: String, age: Long, phone: String)
  def main(args: Array[String]): Unit = {
    val spark = SparkSession.builder().master("local[*]").getOrCreate()
    val sc = spark.sparkContext
    val parser = new JsonParser();
    val jsonObject1 = parser.parse("""{"name":"abc","age":23,"phone":"0208"}""").getAsJsonObject()
    val jsonObject2 = parser.parse("""{"name":"xyz","age":33}""").getAsJsonObject()
    val PairRDD = sc.parallelize(List(
      (new LongWritable(1l), jsonObject1),
      (new LongWritable(2l), jsonObject2)
    ))
    val rdd1 =PairRDD.map(element => element._2)
    import spark.implicits._
    //How to create Dataset as schema People from rdd1?
  }
}

甚至尝试打印RDD1元素抛出

object not serializable (class: org.apache.hadoop.io.LongWritable, value: 1)
- field (class: scala.Tuple2, name: _1, type: class java.lang.Object)
- object (class scala.Tuple2, (1,{"name":"abc","age":23,"phone":"0208"}))

基本上,我从BigQuery表中获得了此RDD [Longrlitable,JSONPARSER],我想将其转换为数据集,因此我可以将SQL应用于转换。

我故意将手机留在第二个记录中,bigquery返回该元素的无效值。

感谢您的澄清。您需要在Kryo中以序列化注册课程。以下节目作品。我在Spark-Shell中运行,因此必须破坏旧上下文,并使用包含注册Kryo类的配置创建新的Spark上下文

import com.google.gson.{JsonParser}
import org.apache.hadoop.io.LongWritable
import org.apache.spark.SparkContext
sc.stop()
val conf = sc.getConf
conf.registerKryoClasses( Array(classOf[LongWritable], classOf[JsonParser] ))
conf.get("spark.kryo.classesToRegister")
val sc = new SparkContext(conf)
val parser = new JsonParser();
val jsonObject1 = parser.parse("""{"name":"abc","age":23,"phone":"0208"}""").getAsJsonObject()
val jsonObject2 = parser.parse("""{"name":"xyz","age":33}""").getAsJsonObject()
val pairRDD = sc.parallelize(List(
  (new LongWritable(1l), jsonObject1),
  (new LongWritable(2l), jsonObject2)
))

val rdd = pairRDD.map(element => element._2)
rdd.collect()
// res9: Array[com.google.gson.JsonObject] = Array({"name":"abc","age":23,"phone":"0208"}, {"name":"xyz","age":33})
val jsonstrs = rdd.map(e=>e.toString).collect()
val df = spark.read.json( sc.parallelize(jsonstrs) )    
df.printSchema
// root
//  |-- age: long (nullable = true)
//  |-- name: string (nullable = true)
//  |-- phone: string (nullable = true)

相关内容

  • 没有找到相关文章

最新更新