Java-spark代码变成了python-spark

时间:2018-05-12 03:03:29

标签: java python apache-spark pyspark

熟悉python,java和spark的人有时间把这些代码变成python-spark,包括包。

https://github.com/ccnky123/spark-java/blob/master/spark-study-java/src/main/java/cn/spark/study/core/WordCountLocal.java

最后一个dep使用了Java-spark环境,新部门正在使用py-Spark..looking将一些代码改为python ..提前谢谢!

package cn.spark.study.core;

import java.util.Arrays;

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;

/**
 * local wordcount
 * @author Administrator
 *
 */
public class WordCountLocal {

    public static void main(String[] args) {

        SparkConf conf = new SparkConf()
                .setAppName("WordCountLocal")
                .setMaster("local");  


        JavaSparkContext sc = new JavaSparkContext(conf);


        JavaRDD<String> lines = sc.textFile("C://Users//Administrator//Desktop//spark.txt");


        JavaRDD<String> words = lines.flatMap(new FlatMapFunction<String, String>() {

            private static final long serialVersionUID = 1L;

            @Override
            public Iterable<String> call(String line) throws Exception {
                return Arrays.asList(line.split(" "));  
            }

        });


        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();
    }

}

0 个答案:

没有答案