C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Spark Char Count ExampleIn Spark char count example, we find out the frequency of each character exists in a particular file. Here, we use Scala language to perform Spark operations. Steps to execute Spark char count exampleIn this example, we find and display the number of occurrences of each character.
$ 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 Topic#
|