-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
...g8/$if(multiModule.truthy)$m1$endif$/src/main/scala/$package__packaged$/CountingApp.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package $organization$.$name$ | ||
|
||
import org.apache.spark.rdd.RDD | ||
import org.apache.spark.{SparkConf, SparkContext} | ||
|
||
/** | ||
* Use this to test the app locally, from sbt: | ||
* sbt "run inputFile.txt outputFile.txt" | ||
* (+ select CountingLocalApp when prompted) | ||
*/ | ||
object CountingLocalApp extends App { | ||
val (inputFile, outputFile): (String, String) = (args(0), args(1)) | ||
val conf: SparkConf = new SparkConf() | ||
.setMaster("local") | ||
.setAppName("my awesome app") | ||
|
||
Runner.run(conf, inputFile, outputFile) | ||
} | ||
|
||
/** | ||
* Use this when submitting the app to a cluster with spark-submit | ||
*/ | ||
object CountingApp extends App { | ||
val (inputFile, outputFile): (String, String) = (args(0), args(1)) | ||
|
||
// spark-submit command should supply all necessary config elements | ||
Runner.run(new SparkConf(), inputFile, outputFile) | ||
} | ||
|
||
object Runner { | ||
def run(conf: SparkConf, inputFile: String, outputFile: String): Unit = { | ||
val sc: SparkContext = new SparkContext(conf) | ||
val rdd: RDD[String] = sc.textFile(inputFile) | ||
val counts: RDD[(String, Int)] = WordCount.withStopWordsFiltered(rdd) | ||
counts.saveAsTextFile(outputFile) | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...n/g8/$if(multiModule.truthy)$m1$endif$/src/main/scala/$package__packaged$/WordCount.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package $organization$.$name$ | ||
|
||
/** | ||
* Everyone's favourite wordcount example. | ||
*/ | ||
|
||
import org.apache.spark.rdd._ | ||
|
||
object WordCount { | ||
|
||
/** | ||
* A slightly more complex than normal wordcount example with optional | ||
* separators and stopWords. Splits on the provided separators, removes | ||
* the stopwords, and converts everything to lower case. | ||
*/ | ||
def withStopWordsFiltered( | ||
rdd: RDD[String], | ||
separators: Array[Char] = " ".toCharArray, | ||
stopWords: Set[String] = Set("the") | ||
): RDD[(String, Int)] = { | ||
|
||
val tokens: RDD[String] = rdd.flatMap(_.split(separators).map(_.trim.toLowerCase)) | ||
val lcStopWords: Set[String] = stopWords.map(_.trim.toLowerCase) | ||
val words: RDD[String] = tokens.filter(token => !lcStopWords.contains(token) && token.nonEmpty) | ||
val wordPairs: RDD[(String, Int)] = words.map((_, 1)) | ||
val wordCounts: RDD[(String, Int)] = wordPairs.reduceByKey(_ + _) | ||
wordCounts | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
.../$if(multiModule.truthy)$m1$endif$/src/test/scala/$package__packaged$/WordCountTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package $organization$.$name$ | ||
|
||
/** | ||
* A simple test for everyone's favourite wordcount example. | ||
*/ | ||
|
||
import com.holdenkarau.spark.testing.SharedSparkContext | ||
import org.apache.spark.rdd.RDD | ||
import org.scalatest.flatspec.AnyFlatSpecLike | ||
import org.scalatest.matchers.should.Matchers | ||
|
||
class WordCountTest extends AnyFlatSpecLike with Matchers with SharedSparkContext { | ||
|
||
"WordCount" should "count words with Stop Words Removed" in { | ||
val linesRDD: RDD[String] = sc.parallelize( | ||
Seq( | ||
"How happy was the panda? You ask.", | ||
"Panda is the most happy panda in all the#!?ing land!" | ||
) | ||
) | ||
|
||
val stopWords: Set[String] = Set("a", "the", "in", "was", "there", "she", "he") | ||
val splitTokens: Array[Char] = "#%?!. ".toCharArray | ||
|
||
val wordCounts: RDD[(String, Int)] = | ||
WordCount.withStopWordsFiltered(linesRDD, splitTokens, stopWords) | ||
val wordCountsAsMap: collection.Map[String, Int] = wordCounts.collectAsMap() | ||
|
||
assert(!wordCountsAsMap.contains("the")) | ||
assert(!wordCountsAsMap.contains("?")) | ||
assert(!wordCountsAsMap.contains("#!?ing")) | ||
assert(wordCountsAsMap.contains("ing")) | ||
wordCountsAsMap("panda") should be(3) | ||
} | ||
} |