Adv Spark Programming

33 / 52

Adv Spark Programming - Broadcast Variables Example

Code is available here: Broadcast Example

var commonWords = Array("a", "an", "the", "of", "at", "is", "am","are","this","that","at", "in", "or", "and", "or", "not", "be", "for", "to", "it")
val commonWordsMap = collection.mutable.Map[String, Int]()
for(word <- commonWords){
commonWordsMap(word) = 1
}
var commonWordsBC = sc.broadcast(commonWordsMap)
var file = sc.textFile("/data/mr/wordcount/input/big.txt")
def toWords(line:String):Array[String] = {
var words = line.split(" ")
var output = Array[String]();
for(word <- words){
if(! (commonWordsBC.value contains word.toLowerCase.trim.replaceAll("[^a-z]",""))) output = output :+ word;
}
return output;
}
var uncommonWords = file.flatMap(toWords)
uncommonWords.take(100)

Slides - Adv Spark Programming (2)


No hints are availble for this assesment

Answer is not availble for this assesment

Please login to comment

24 Comments

This comment has been removed.

This comment has been removed.

Hi,

Please go through previous slides and chapters and let us know which topic you didn't understand.

Thanks.

  Upvote    Share

Also, we have provided the live session recordings, so feel free to go through them for better understanding.

  Upvote    Share

It is throwing an error. I am just copying and pasting the codes. Actually didn't understood even a single line. 

  Upvote    Share

This comment has been removed.

Hi,

Please go through previous slides and chapters and let us know which topic you didn't understand.

Thanks.

  Upvote    Share

Please go thru the previous sessions and also the recordings of the live session.

  Upvote    Share

Hey! Cloudxlab Team,

It looks like the code needs some update. Upon running, it returned a NullPointerException error.

var commonWords = Array("a", "an", "the", "of", "at", "is", "am","are","this","that","at","in", "or", "and", "or", "not", "be", "for", "to", "it")

val commonWordsMap = collection.mutable.Map[String, Int]()

for (word <- commonWords) {
    commonWordsMap(word) = 1
}

var commonWordsBC = sc.broadcast(commonWordsMap)

var file = sc.textFile("/data/mr/wordcount/input/big.txt")

def toWords(line:String):Array[String] = {
    var words = line.split(" ")
    var output = Array[String]()
    
    for(word <- words) {
        if(!(commonWordsBC.value contains word.toLowerCase.trim.replaceAll("[^a-z]",""))) output = output :+ word
    }
    return output
}

var uncommonWords = file.flatMap(toWords)
uncommonWords.take(10)
  Upvote    Share

This comment has been removed.

Code is available here:  there is no link

  Upvote    Share

Hi

There is no link or code shared as shown on the attached screen shot. Please share the link.

  Upvote    Share

Hi Dilip,

Code is there, can you please try reloading?.

It may take longer as it is rendering from github.

If it is still not visible then please go to this link: https://gist.github.com/girisandeep/f12ab4bf2536dc5f0a8ca673efbac1db#file-broadcast-example-scala

  Upvote    Share

No code is visible as shared in snippet

  Upvote    Share

Hi Anant,

Code is there, can you please try reloading?.

It may take longer as it is rendering from github.

If it is still not visible then please go to this link: https://gist.github.com/girisandeep/f12ab4bf2536dc5f0a8ca673efbac1db#file-broadcast-example-scala

  Upvote    Share

Sir ,Why all these type of new heavy codes are not explained everytime ??

  Upvote    Share

please also explain all the terms and function you are using as we are doing scala for the very first time in every vedio you guys are coming up with a new operation and only keeps on dictating it instead of making us understand.

REALLY REALLY DISAPPOINTED!!!

 1  Upvote    Share

not able to find link

  Upvote    Share

When I run the above code I get the following error---

Name: org.apache.spark.SparkException
Message: Job aborted due to stage failure: Task 0 in stage 0.0 failed 1 times, most recent failure: Lost task 0.0 in stage 0.0 (TID 0, localhost, executor driver): java.lang.NullPointerException
at $line13.$read$$iw$$iw$$iw$$iw$$iw$$iw$$anonfun$toWords$1.apply(<console>:39)
at $line13.$re

 1  Upvote    Share

Hi Sandeep,
I couldn't see any link below of "Code is available here:"

  Upvote    Share

Hi, Dhirendra.

Can you please check again!.
The code is just below the video!. It is completely visible!
Please let me know if still you could not find it.

All the best!

  Upvote    Share

Code is available here:

But there is no link or code.

  Upvote    Share

Hi Thiago,
The code is there in the section just below the text.

  Upvote    Share