Above answer works but might be error prone some times SparkFiles.get will return null
#1 is more prominent way of getting a file from any url or public s3 location
Option 1 :
IOUtils.toString will do the trick see the docs of apache commons io jar will be already present in any spark cluster whether its databricks or any other spark installation.
Below is the scala way of doing this... I have taken a raw git hub csv file for this example ... can change based on the requirements.
import org.apache.commons.io.IOUtils // jar will be already there in spark cluster no need to worry
import java.net.URL
val urlfile=new URL("https://raw.githubusercontent.com/lrjoshi/webpage/master/public/post/c159s.csv")
val testcsvgit = IOUtils.toString(urlfile,"UTF-8").lines.toList.toDS()
val testcsv = spark
.read.option("header", true)
.option("inferSchema", true)
.csv(testcsvgit)
testcsv.show
Result :
+-----------+------+----+----+---+-----+
|Experiment |Virus |Cell| MOI|hpi|Titer|
+-----------+------+----+----+---+-----+
| EXP I| C159S|OFTu| 0.1| 0| 4.75|
| EXP I| C159S|OFTu| 0.1| 6| 2.75|
| EXP I| C159S|OFTu| 0.1| 12| 2.75|
| EXP I| C159S|OFTu| 0.1| 24| 5.0|
| EXP I| C159S|OFTu| 0.1| 48| 5.5|
| EXP I| C159S|OFTu| 0.1| 72| 7.0|
| EXP I| C159S| STU| 0.1| 0| 4.75|
| EXP I| C159S| STU| 0.1| 6| 3.75|
| EXP I| C159S| STU| 0.1| 12| 4.0|
| EXP I| C159S| STU| 0.1| 24| 3.75|
| EXP I| C159S| STU| 0.1| 48| 3.25|
| EXP I| C159S| STU| 0.1| 72| 3.25|
| EXP I| C159S|OFTu|10.0| 0| 6.5|
| EXP I| C159S|OFTu|10.0| 6| 4.75|
| EXP I| C159S|OFTu|10.0| 12| 4.75|
| EXP I| C159S|OFTu|10.0| 24| 6.25|
| EXP I| C159S|OFTu|10.0| 48| 6.5|
| EXP I| C159S|OFTu|10.0| 72| 7.0|
| EXP I| C159S| STU|10.0| 0| 7.0|
| EXP I| C159S| STU|10.0| 6| 4.75|
+-----------+------+----+----+---+-----+
only showing top 20 rows
Option 2 : in Scala
import java.net.URL
import org.apache.spark.SparkFiles
val urlfile="https://raw.githubusercontent.com/lrjoshi/webpage/master/public/post/c159s.csv"
spark.sparkContext.addFile(urlfile)
val df = spark.read
.option("inferSchema", true)
.option("header", true)
.csv("file://"+SparkFiles.get("c159s.csv"))
df.show
Result : Will be same as Option #1 like below
+-----------+------+----+----+---+-----+
|Experiment |Virus |Cell| MOI|hpi|Titer|
+-----------+------+----+----+---+-----+
| EXP I| C159S|OFTu| 0.1| 0| 4.75|
| EXP I| C159S|OFTu| 0.1| 6| 2.75|
| EXP I| C159S|OFTu| 0.1| 12| 2.75|
| EXP I| C159S|OFTu| 0.1| 24| 5.0|
| EXP I| C159S|OFTu| 0.1| 48| 5.5|
| EXP I| C159S|OFTu| 0.1| 72| 7.0|
| EXP I| C159S| STU| 0.1| 0| 4.75|
| EXP I| C159S| STU| 0.1| 6| 3.75|
| EXP I| C159S| STU| 0.1| 12| 4.0|
| EXP I| C159S| STU| 0.1| 24| 3.75|
| EXP I| C159S| STU| 0.1| 48| 3.25|
| EXP I| C159S| STU| 0.1| 72| 3.25|
| EXP I| C159S|OFTu|10.0| 0| 6.5|
| EXP I| C159S|OFTu|10.0| 6| 4.75|
| EXP I| C159S|OFTu|10.0| 12| 4.75|
| EXP I| C159S|OFTu|10.0| 24| 6.25|
| EXP I| C159S|OFTu|10.0| 48| 6.5|
| EXP I| C159S|OFTu|10.0| 72| 7.0|
| EXP I| C159S| STU|10.0| 0| 7.0|
| EXP I| C159S| STU|10.0| 6| 4.75|
+-----------+------+----+----+---+-----+
only showing top 20 rows
If you are using windows .csv("file:///
should be used instead of //
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.HttpClients
import org.apache.http.util.EntityUtils
import org.apache.spark.sql.{DataFrame, SparkSession}
object ReadFromUrl {
def main(args: Array[String]): Unit = {
val spark = SparkSession.builder()
.appName("ReadFromUrl")
.master("local[*]")
.getOrCreate()
val url = "https://people.sc.fsu.edu/~jburkardt/data/csv/airtravel.csv"
val httpClient = HttpClients.createDefault()
val httpGet = new HttpGet(url)
val response = httpClient.execute(httpGet)
val entity = response.getEntity
val data = EntityUtils.toString(entity)
val tempFile = java.io.File.createTempFile("airtravel", ".csv")
tempFile.deleteOnExit()
val writer = new java.io.PrintWriter(tempFile)
writer.write(data)
writer.close()
val df: DataFrame = spark.read
.format("csv")
.option("header", "true")
.option("inferSchema", "true")
.load(tempFile.getAbsolutePath)
df.show()
spark.stop()
}
}
Result :
+-----+-------+-------+-------+
|Month| "1958"| "1959"| "1960"|
+-----+-------+-------+-------+
| JAN| 340.0| 360.0| 417.0|
| FEB| 318.0| 342.0| 391.0|
| MAR| 362.0| 406.0| 419.0|
| APR| 348.0| 396.0| 461.0|
| MAY| 363.0| 420.0| 472.0|
| JUN| 435.0| 472.0| 535.0|
| JUL| 491.0| 548.0| 622.0|
| AUG| 505.0| 559.0| 606.0|
| SEP| 404.0| 463.0| 508.0|
| OCT| 359.0| 407.0| 461.0|
| NOV| 310.0| 362.0| 390.0|
| DEC| 337.0| 405.0| 432.0|
+-----+-------+-------+-------+