Package Spark;import Org.apache.spark.sparkconf;import Org.apache.spark.api.java.javapairrdd;import Org.apache.spark.api.java.javardd;import Org.apache.spark.api.java.javasparkcontext;import Org.apache.spark.sql.sparksession;import Scala. Tuple2;import java.util.arrays;import java.util.list;/** * Created by KKXWZ on 2018/5/24 */public class WordCountApp { public static void Main (string[] args) {////spark 2.0 version before//sparkconf sparkconf = new sparkconf (). Setappnam E ("Wordcountapp"). Setmaster ("local[2]");//Javasparkcontext spark = new Javasparkcontext (sparkconf);//Javar dd<string> lines= spark.textfile ("/users/zl/data/sparksqldata/hello.txt");//After Spark 2.0 release (recommended) Sparkse Ssion spark = Sparksession.builder (). Master ("local[2]"). AppName ("Wordcountapp") . Getorcreate (); Javardd<string> lines= Spark.read (). Textfile ("/users/zl/data/sparksqldata/hello.txt"). JavaRDD (); Javardd<string> words = Lines.flatmap (line-arrays.aslist ("T")). Line.split ()); Javapairrdd<string, integer> counts = words. Maptopair (Word, new tuple2<string, Integer> (w Ord, 1)). Reducebykey ((x, y)-x+y); First output mode: Counts.foreach (Count---System.out.println (count._1 () + ":" + count._2 ())); The second output mode://list<tuple2<string, integer>> output = Counts.collect ();////for (Tuple2<string, Integer> tuple:output) {//System.out.println (Tuple._1 () + ":" + tuple._2 ()),//} spark.stop ( ); }}//ps://1, JDK version of at least 1.8//2, the best associated source code, see Return type Learning!!!
Developing Spark's WordCount program using Java