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

后端 未结 2 601
抹茶落季
抹茶落季 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()
    
    0 讨论(0)
  • 2021-01-06 01:38
    val content = scala.io.Source.fromURL("http://ichart.finance.yahoo.com/table.csv?s=FB").mkString
    
    val list = content.split("\n").filter(_ != "")
    
    val rdd = sc.parallelize(list)
    
    val df = rdd.toDF
    
    0 讨论(0)
提交回复
热议问题