Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bypass first line of each file in Spark (Scala)

I am processing an S3 folder containing csv.gz files in Spark. Each csv.gz file has a header that contains column names.

The way I load the contained data to Spark is to reference the path / folder, like this:

val rdd = sc.textFile("s3://.../my-s3-path")

How can I skip the header in each file, so that I can process the values only?

Thanks

like image 851
oikonomiyaki Avatar asked Dec 30 '25 01:12

oikonomiyaki


1 Answers

You could do something like:

val rdd = sc.textFile("s3://.../my-s3-path").mapPartitions(_.drop(1))

Because each input file is gzipped, it will be loaded under a separate partition. If we map across all partitions and drop the first line, we will consequently be removing the first line from each file.

like image 174
Rohan Aletty Avatar answered Dec 31 '25 16:12

Rohan Aletty



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!