spark water notes Row StructType?

1, StructType? You can specify sql table

package test
import org.apache.spark.sql.{Row, SparkSession}
import org.apache.spark.sql.types._
object StructTypeDemo {

  def main(args: Array[String]): Unit = {


    val spark = SparkSession
      .builder()
      .master("local")
      .appName("structType")
      //.enableHiveSupport()
      .getOrCreate()

    val schema = StructType(
        StructField("name",StringType,true)::
        StructField("age",IntegerType,true)::
        Nil)
    import spark.implicits._
    val s =spark.sparkContext.parallelize(Seq(("name",1),("name2",2)))
    val rowRDD =s.map(row=>Row(row._1,row._2));
    val rdd = spark.createDataFrame(rowRDD,schema)
    rdd.toDF().show()
  }
}

---- created using the Case class

 

Published 61 original articles · won praise 1 · views 661

Guess you like

Origin blog.csdn.net/u012842247/article/details/103516896