C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Spark Word Count ExampleIn Spark word count example, we find out the frequency of each word exists in a particular file. Here, we use Scala language to perform Spark operations. Steps to execute Spark word count exampleIn this example, we find and display the number of occurrences of each word.
$ nano sparkdata.txt
$ cat sparkdata.txt
$ hdfs dfs -mkdir /spark
$ hdfs dfs -put /home/codegyani/sparkdata.txt /spark
$ spark-shell
scala> val data=sc.textFile("sparkdata.txt") Here, pass any file name that contains the data.
scala> data.collect;
scala> val splitdata = data.flatMap(line => line.split(" "));
scala> splitdata.collect;
scala> val mapdata = splitdata.map(word => (word,1)); Here, we are assigning a value 1 to each word.
scala> mapdata.collect;
scala> val reducedata = mapdata.reduceByKey(_+_); Here, we are summarizing the generated data.
scala> reducedata.collect; Here, we got the desired output.
Next TopicSpark Char Count Example
|