This is basically the same as the Java Code section above, directly on the code
Package Com.spark.study.core;
Import Java.util.Arrays;
Import Java.util.Iterator;
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.api.java.function.FlatMapFunction;
Import Org.apache.spark.api.java.function.Function2;
Import Org.apache.spark.api.java.function.PairFunction;
Import Org.apache.spark.api.java.function.VoidFunction;
Import Scala. Tuple2;
/**
*Willjavawordcount Program deployment to spark running on a cluster
* @author administrator
*
*/
public class Wordcountcluster {
public static void Main (string[] args) {
// if you want toSparkrun on the cluster, need to be modified, only two places
// first, it willsparkconfof theSetmaster ()method to delete, by default it will go to connect
// second, we are not targeting local files, and modifying them toHadoop HDFson the real storage of big data files
// actual execution steps:
//1, theSpark.txtFile UploadHDFsup
//2, using our earliestPom.xmlconfigured in themavenplug-in, onSparkEngineering for Packaging
//3, will be packaged after theSparkEngineeringJarpackage, upload to the machine to execute
//4, writingSpark-submitScript
//5, ExecutionSpark-submitscripts, submittingSparkapply to cluster execution
sparkconf conf = new sparkconf ()
. Setappname ("Wordcountcluster");
Javasparkcontext sc = new Javasparkcontext (conf);
Javardd<string> lines = Sc.textfile ("Hdfs://node1:9000/spark.txt");
javardd<string> words = Lines.flatmap (new flatmapfunction<string, string> () {
Private static final long serialversionuid = 1L;
@Override
Public iterator<string> Call (String line) throws Exception {
Return Arrays.aslist (Line.split ("")). iterator ();
}
});
javapairrdd<string, integer> pairs = Words.maptopair (
New pairfunction<string, String, integer> () {
Private static final long serialversionuid = 1L;
@Override
Public tuple2<string, integer> call (String Word) throws Exception {
return new tuple2<string, integer> (Word, 1);
}
});
javapairrdd<string, integer> wordcounts = Pairs.reducebykey (
New Function2<integer, Integer, integer> () {
Private static final long serialversionuid = 1L;
@Override
Public integer Call (integer v1, Integer v2) throws Exception {
Return v1 + v2;
}
});
Wordcounts.foreach (New voidfunction<tuple2<string,integer>> () {
Private static final long serialversionuid = 1L;
@Override
public void Call (Tuple2<string, integer> wordCount) throws Exception {
System.out.println (Wordcount._1 + "appeared" + Wordcount._2 + "times.");
}
});
Sc.close ();
}
}
Follow the steps above to configure
Spark's operator Statistics Java code (run in Linux system cluster)