Halloween Sale: Flat 70% + Addl. 25% Off + 30 Days Extra Lab on all Courses | Use Coupon HS25 in Checkout | Offer Expires In

  Enroll Now

Project - Handling Binary Files

You can load binary files from a directory as RDD using sc.binaryFiles.


We are going to load the gzipped file from HDFS and then using spark we are going to process those.

The files are located in HDFS in the directory /data/SentimentFiles/SentimentFiles/upload/data/

Taking a look

First, download a copy from hdfs using the following command:

hadoop fs -get /data/SentimentFiles/SentimentFiles/upload/data/tweets_raw/FlumeData.1367523670393.gz

And then using unix file command, understand the datatype:

file FlumeData.1367523670393.gz

This should display something like:

FlumeData.1367523670393.gz: gzip compressed data, was "FlumeData.1367523670393", from Unix, last modified: Thu Jul 18 03:11:25 2013

Start spark shell

First export the variables required to access Hadoop from Spark:

export HADOOP_CONF_DIR=/etc/hadoop/conf/
export YARN_CONF_DIR=/etc/hadoop/conf/

Start Spark Shell

Let us launch a new version of spark using the following command:


Copy Paste the following code

// Imports
import java.io.{ByteArrayOutputStream, ByteArrayInputStream}
import java.util.zip.{GZIPOutputStream, GZIPInputStream}
import scala.collection.mutable.ArrayBuffer

//This creates the rdd from directory such that each record ia (filename, file_content)
var tweets_raw = sc.binaryFiles("/data/SentimentFiles/SentimentFiles/upload/data/tweets_raw/")

//This function basically converts compressed bytes to uncompressed bytes
import java.io.DataInputStream
import org.apache.spark.input.PortableDataStream

def count_length(compressed: PortableDataStream): Int = {
    var compressedis:DataInputStream = null
        var compressedis = compressed.open()

        val gzipInputStream = new GZIPInputStream(compressedis)
        var output = 0
        var totalByteCount = 0
        val bytes = new Array[Byte](1024)
        while (gzipInputStream.available() == 1) {
          val byteCount = gzipInputStream.read(bytes)
          if (byteCount > 0) {
            var lb = bytes.take(byteCount)
            output = output + lb.length
            totalByteCount += byteCount
        return output;
        if(compressedis != null)

var decompressed_tweets_lengths = tweets_raw.mapValues(t => count_length(t))

//Save the lengths in HDFS in your home directory in a directory named jsonTweets

Given the compressed file in the format of gzip in the folder /data/SentimentFiles/SentimentFiles/upload/data/tweets_raw/ in HDFS, find the lengths of files after decompressing each.

The result should be saved in files in the folder onlytweets_length_after_decompressing such that lines in the file has the data in the format of tuple such as:


No hints are availble for this assesment

Answer is not availble for this assesment

Note - Having trouble with the assessment engine? Follow the steps listed here

Loading comments...