0 votes
1 view
in Big Data Hadoop & Spark by (11.5k points)

I'm using python on Spark and would like to get a csv into a dataframe.

The documentation for Spark SQL strangely does not provide explanations for CSV as a source.

I have found Spark-CSV, however I have issues with two parts of the documentation:

  • "This package can be added to Spark using the --jars command line option. For example, to include it when starting the spark shell: $ bin/spark-shell --packages com.databricks:spark-csv_2.10:1.0.3" Do I really need to add this argument everytime I launch pyspark or spark-submit? It seems very inelegant. Isn't there a way to import it in python rather than redownloading it each time?
  • df = sqlContext.load(source="com.databricks.spark.csv", header="true", path = "cars.csv") Even if I do the above, this won't work. What does the "source" argument stand for in this line of code? How do I simply load a local file on linux, say "/Spark_Hadoop/spark-1.3.1-bin-cdh4/cars.csv"?

1 Answer

0 votes
by (31.4k points)

In the recent versions of Spark, the way to get a CSV into Spark dataframe has become a lot easier. sqlContext.read is an expression that gives you a DataFrameReader instance, with a .csv() method:

df = sqlContext.read.csv("/path/to/your.csv")

Note: You can also indicate that the csv file has a header by adding the keyword argument header=True to the .csv() call.

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...