cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
cancel
Showing results for 
Search instead for 
Did you mean: 

How to write *.csv file from DataBricks FileStore

Jeff1
Contributor II

Struggling with how to export a Spark dataframe as a *.csv file to a local computer. I'm successfully using the spark_write_csv funciton (sparklyr R library R) to write the csv file out to my databricks dbfs:FileStore location. Becase (I'm assuming) databricks is creating 4 *.csv partitions. So I gather from the databricks documentation I need to coalesce the partitions files . So I'm am using the following command....

df.coalesce(1).write.option("header","true").csv("dbfs:FileStore/temp/df.csv")

And then I receive a NameError: name 'df' is not defined.

So am I missing a step, syntax wrong? I'm working in a R Notebook

Jeff

1 ACCEPTED SOLUTION

Accepted Solutions

Hubert-Dudek
Esteemed Contributor III

sparklyr has a different syntax. There is function sdf_coalesce.

The code which you paste is for Scala/Python. Additionally, even in python you can only specify folder not file so CSV("dbfs:FileStore/temp/")

View solution in original post

7 REPLIES 7

Hubert-Dudek
Esteemed Contributor III

sparklyr has a different syntax. There is function sdf_coalesce.

The code which you paste is for Scala/Python. Additionally, even in python you can only specify folder not file so CSV("dbfs:FileStore/temp/")

Ok that helped. I was able to use the sdf_coalesce function and have 1 partition. Then how do I download it from DataBrick. I t provides a path and I would assume I have to combine it with some other portion of an html command

Hubert-Dudek
Esteemed Contributor III

@Jeff (Customer),

  • Mount your blob storage (or s3) to databricks and save it there. Then you can get it from a browser or using an app like storage explorer.
  • OR use display(your_dataframe) function - there is export option

Kaniz
Community Manager
Community Manager

Hi @Jeff (Customer), Were you able to follow @Hubert Dudek​ ? Did it help you?

Jeff1
Contributor II

Yes. Still don't understand why I couldn't use the python code after %python. But never the less Mr. Dudek response corrected my course. I also was able to download the file to my local drive after playing with the html path. The databricks documentation was not helpful, or maybe I just didn't find the correct help document.

Kaniz
Community Manager
Community Manager

Hi @Jeff Reichman​ , Thanks for the update. Would you like to mark @Hubert Dudek​ 's response as the best in case it helped you resolve your problem?

Hi @Jeff Reichman​,

Once you saved the file to FileStore https://docs.databricks.com/data/filestore.html#save-a-file-to-filestore then you can follow the instructions from here to be able to download it to your local machine

Welcome to Databricks Community: Lets learn, network and celebrate together

Join our fast-growing data practitioner and expert community of 80K+ members, ready to discover, help and collaborate together while making meaningful connections. 

Click here to register and join today! 

Engage in exciting technical discussions, join a group with your peers and meet our Featured Members.