从csv文件创建dataFrame

读取CSV文件创建dataFrame的写法:

val df1 = spark.read
      .format("csv")
      .option("header","true")
      .option("inferSchema","true")
      .load("src/test/resource/1.csv")

完整写法:

import org.apache.spark.{SparkConf,SparkContext}
import org.apache.spark.sql.SparkSession

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

    val conf = new SparkConf().setMaster("local").setAppName("readDataFrame")
    val sc = new SparkContext(conf)

    val spark = SparkSession.builder().appName("readDF").getOrCreate()

    val df = spark.read.format("csv").option("header",true).load("src/test/resource/1.csv")
    df.printSchema()
    df.show()

  }
}

 

你可能感兴趣的:(大数据学习)