java语言开发实现spark版(提交单机/集群两个运行版本):词计数

单机版本:

package com.itheima.java_wordcount;

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 scala.Tuple2;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 * Date:2019/4/24
 * Author:Lynn.cn.Li
 * Desc:
 */
public class WordCountJava {
    public static void main(String[] args) {
        // 1.创建sparkConf对象。设置appName和master地址
        SparkConf sparkConf = new SparkConf().setAppName("LocalJavaWordCount").setMaster("local[2]");

        // 2.创建sparkContext对象
        JavaSparkContext jsc = new JavaSparkContext(sparkConf);

        // 3.读取数据文件
        JavaRDD textFileRDD = jsc.textFile("g://input/1.txt");

        // 4.切分每一行,得到每一个单词
        JavaRDD flatMapRDD = textFileRDD.flatMap(new FlatMapFunction() {
            public Iterator call(String s) throws Exception {

                // 按照空格切分单词
                String[] arr = s.split(" ");
                return Arrays.asList(arr).iterator();
            }
        });

        // 5.每个单词计数为1
        JavaPairRDD javaPairRDD = flatMapRDD.mapToPair(new PairFunction() {
            public Tuple2 call(String s) throws Exception {

                return new Tuple2(s, 1);
            }
        });


        // 6.相同单词出现的次数累加
        JavaPairRDD resultRDD = javaPairRDD.reduceByKey(new Function2() {
            public Integer call(Integer v1, Integer v2) throws Exception {

                return v1 + v2;
            }
        });


        /**
         * 细节:实现排序
         *  1.先将(单词,次数)进行位置对调成(次数,单词),进行排序
         *  2.排序后再将(次数,单词)进行位置对调成(单词,次数)
         */


        JavaPairRDD sortRDD = resultRDD.mapToPair(new PairFunction, Integer, String>() {
            public Tuple2 call(Tuple2 tuple2) throws Exception {

                return new Tuple2(tuple2._2, tuple2._1);
            }
        });
        // 排序
        JavaPairRDD sortDescRDD = sortRDD.sortByKey(false);

        // 再对调位置
        JavaPairRDD finalResultRDD = sortDescRDD.mapToPair(new PairFunction, String, Integer>() {
            public Tuple2 call(Tuple2 tuple2) throws Exception {


                return new Tuple2(tuple2._2, tuple2._1);
            }
        });

        // 7.收集结果数据
        List> wordData = finalResultRDD.collect();

        // 8.循环打印结果数据
        for (Tuple2 tuple2 : wordData) {
            System.out.println("单词:"+tuple2._1+"出现了"+tuple2._2+"次");
        }
        // 9.关闭SparkCount
        jsc.stop();

    }
}

 

 

提交集群版本:

package com.itheima.java_wordcount;

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 scala.Tuple2;

import java.util.Arrays;
import java.util.Iterator;


/**
 * Date:2019/4/24
 * Author:Lynn.cn.Li
 * Desc:
 */
public class WordCountJavaOnline {

    public static void main(String[] args) {
        // 1.创建sparkConf对象。设置appName和master地址
        SparkConf sparkConf = new SparkConf().setAppName("OnlineJavaWordCount");

        // 2.创建sparkContext对象
        JavaSparkContext jsc = new JavaSparkContext(sparkConf);

        // 3.读取数据文件
        JavaRDD textFileRDD = jsc.textFile(args[0]);//动态参数传入

        // 4.切分每一行,得到每一个单词
        JavaRDD flatMapRDD = textFileRDD.flatMap(new FlatMapFunction() {
            public Iterator call(String s) throws Exception {

                // 按照空格切分单词
                String[] arr = s.split(" ");
                return Arrays.asList(arr).iterator();
            }
        });

        // 5.每个单词计数为1
        JavaPairRDD javaPairRDD = flatMapRDD.mapToPair(new PairFunction() {
            public Tuple2 call(String s) throws Exception {

                return new Tuple2(s, 1);
            }
        });


        // 6.相同单词出现的次数累加
        JavaPairRDD resultRDD = javaPairRDD.reduceByKey(new Function2() {
            public Integer call(Integer v1, Integer v2) throws Exception {

                return v1 + v2;
            }
        });


        resultRDD.saveAsTextFile(args[1]);//动态参数传入


        // 9.关闭SparkCount
        jsc.stop();

    }

}

spark执行脚本:

spark-submit --class com.itheima.java_wordcount.WordCountJavaOnline \
--master spark://node01:7077,node02:7077 \
--executor-memory 512m \
--total-executor-cores 2 \
/export/servers/sparkTestData/wordcount_java.jar \
/spark/wordcount/input2/1.txt \
/spark/wordcount/output4

 

你可能感兴趣的:(spark)