Click here to Skip to main content
15,077,641 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I wrote a spark program to count the words but now I want to count letters Instead of word. Can Anyone please tell me what do I have to change in the code word counting code to count letter instead.

What I have tried:

Here is the code to count words:

Java
public class WordCount {


static class SplitFunction implements FlatMapFunction<String, String>
 {
 public Iterable<String> call(String s) {
    return Arrays.asList(s.split(" "));
  }
 }

public static void main(String[] args)
{

SparkConf sparkConf = new SparkConf();
sparkConf.setAppName("Spark WordCount example using Java");

sparkConf.setMaster("local");
JavaSparkContext sparkContext = new JavaSparkContext(sparkConf);

JavaRDD<String> textFile = sparkContext.textFile("input.txt");

JavaRDD<String> words = textFile.flatMap(new SplitFunction());

JavaPairRDD<String, Integer> pairs = words.mapToPair(new PairFunction<String, String, Integer>() {
      public Tuple2<String, Integer> call(String s) {
        return new Tuple2<String, Integer>(s, 1);
      }
     });
 JavaPairRDD<String, Integer> counts = pairs.reduceByKey(
    new Function2<Integer, Integer, Integer>() {
      public Integer call(Integer a, Integer b) {
        return a + b;
      }
    });


counts.saveAsTextFile("output");
sparkContext.stop();
sparkContext.close();
   }
 }
Posted
Updated 12-May-21 9:04am
v2

1 solution

If you wrote that code, you should be able to very simply modify it - it's a less complicated task.

And since that code is copy'n'pasted from here: Spark WordCount example - Java Developer Zone[^] I don't believe a word of what you are saying.

And neither will your teacher when you hand it in ...

Homework is set for a reason: to let you develop a skill using what has been covered in lessons. Cheating doesn't do that, and it means that subsequent tasks become harder and harder because you haven't learn how to do the basics, or developed any skill at all.
Just like riding a bicycle, you can watch people riding them all you like, the first dozen times you try it for real you are going to fall off because you don't have the reflexes built up to balance it properly - you haven't got the skills you need to cycle yet.

And I for one, really, really do not like being lied to.
   
v2
Comments
Richard MacCutchan 12-May-21 16:36pm
   
Ooh, someone did not like your answer. Countered.
OriginalGriff 12-May-21 17:08pm
   
I wonder why not ... :InnocentWhistleSmiley:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900