46

i have started the shell with databrick csv package

#../spark-1.6.1-bin-hadoop2.6/bin/pyspark --packages com.databricks:spark-csv_2.11:1.3.0

Then i read a csv file did some groupby op and dump that to a csv.

from pyspark.sql import SQLContext
sqlContext = SQLContext(sc)
df = sqlContext.read.format('com.databricks.spark.csv').options(header='true').load(path.csv')   ####it has columns and df.columns works fine
type(df)   #<class 'pyspark.sql.dataframe.DataFrame'>
#now trying to dump a csv
df.write.format('com.databricks.spark.csv').save('path+my.csv')
#it creates a directory my.csv with 2 partitions
### To create single file i followed below line of code
#df.rdd.map(lambda x: ",".join(map(str, x))).coalesce(1).saveAsTextFile("path+file_satya.csv") ## this creates one partition in directory of csv name
#but in both cases no columns information(How to add column names to that csv file???)
# again i am trying to read that csv by
df_new = sqlContext.read.format("com.databricks.spark.csv").option("header", "true").load("the file i just created.csv")
#i am not getting any columns in that..1st row becomes column names

Please don't answer like add a schema to dataframe after read_csv or while reading mention the column names.

Question1- while giving csv dump is there any way i can add column name with that???

Question2-is there a way to create single csv file(not directory again) which can be opened by ms office or notepad++???

note: I am currently not using cluster, As it is too complex for spark beginner like me. If any one can provide a link for how to deal with to_csv into single file in clustered environment , that would be a great help.

5 Answers 5

56

Try

df.coalesce(1).write.format('com.databricks.spark.csv').save('path+my.csv',header = 'true')

Note that this may not be an issue on your current setup, but on extremely large datasets, you can run into memory problems on the driver. This will also take longer (in a cluster scenario) as everything has to push back to a single location.

Sign up to request clarification or add additional context in comments.

2 Comments

@Mike-yes i am using a huge dataset, but the datset(after doing some function over that large dataset) ,i want to o/p as a csv might be having 1 million rows or less . I am having 28GB RAM(on Master and both of its slaves). I will definitely try it out to check if it is giving me Memory error or not. Just for curiosity, can you suggest what will be ideal configuration, if i want to o/p a csv about 5Million Rows?
@Satya - I've mostly done my re-combining of the files with other tools outside of Spark (ie, cat, gzip, etc) if I needed that format. Regarding best configuration, it depends on what you're trying to read the file with. Most of my usage is preprocessing and then re-import into a SQL database for live querying - running bulk imports hasn't required a single file.
37

Just in case, on spark 2.1 you can create a single csv file with the following lines

dataframe.coalesce(1) //So just a single part- file will be created
.write.mode(SaveMode.Overwrite)
.option("mapreduce.fileoutputcommitter.marksuccessfuljobs","false") //Avoid creating of crc files
.option("header","true") //Write the header
.csv("csvFullPath")

Comments

14

The following should do the trick:

df \
  .write \
  .mode('overwrite') \
  .option('header', 'true') \
  .csv('output.csv')

Alternatively, if you want the results to be in a single partition, you can use coalesce(1):

df \
  .coalesce(1) \
  .write \
  .mode('overwrite') \
  .option('header', 'true') \
  .csv('output.csv')

Note however that this is an expensive operation and might not be feasible with extremely large datasets.

Comments

13

with spark >= 2.o, we can do something like

df = spark.read.csv('path+filename.csv', sep = 'ifany',header='true')
df.write.csv('path_filename of csv',header=True) ###yes still in partitions
df.toPandas().to_csv('path_filename of csv',index=False)  ###single csv(Pandas Style)

1 Comment

It should be noted that you can force a single csv from by doing df.coalesce(1).write.csv(..., header = True). If you're partitioning your csv, this will create one file for each partition. The name of the output file will be gobbledygook.
1

got answer for 1st question, it was a matter of passing one extra parameter header = 'true' along with to csv statement

df.write.format('com.databricks.spark.csv').save('path+my.csv',header = 'true')

#Alternative for 2nd question

Using topandas.to_csv , But again i don't want to use pandas here, so please suggest if any other way around is there.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.