Spark's operator Statistics Java code (run in Linux system cluster)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.