How to use Spark-Scala to download a CSV file from the web?

后端 未结 2 609
抹茶落季
抹茶落季 2021-01-06 01:18

world,

How to use Spark-Scala to download a CSV file from the web and load the file into a spark-csv DataFrame?

Currently I depend on curl in a shell command

2条回答
  •  春和景丽
    2021-01-06 01:33

    Found better answer from Process CSV from REST API into Spark

    Here you go:

    import scala.io.Source._
    import org.apache.spark.sql.{Dataset, SparkSession}
    
    var res = fromURL(url).mkString.stripMargin.lines.toList
    val csvData: Dataset[String] = spark.sparkContext.parallelize(res).toDS()
    
    val frame = spark.read.option("header", true).option("inferSchema",true).csv(csvData)
    frame.printSchema()
    

提交回复
热议问题