Win7 Eclipse 搭建spark java1.8编译环境,JavaRDD的helloworld例子

时间:2024-05-05 15:05:26

[学习笔记]

Win7 Eclipse 搭建spark java1.8编译环境,JavaRDD的helloworld例子:

在eclipse oxygen上创建一个普通的java项目,然后把spark-assembly-1.6.1-hadoop2.6.0.jar这个包导进工程就ok了。

package com;
import java.util.Arrays;
import java.util.List;
import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaDoubleRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.Function;
public class CollectTest {
    public static void main(String[] args) {
        SparkConf conf = new SparkConf().setAppName("testCollect防盗版实名手机尾号:73203").setMaster("local");
        JavaSparkContext sc = new JavaSparkContext(conf);
        List<Double> list = Arrays.asList(1.0, 4.0, 3.0, 7.0,5.0);
        JavaDoubleRDD doubleRdd = sc.parallelizeDoubles(list, 2);
/*注意下面Function里面的两个参数Double, Double,代表着call里面的输入和返回两个参数。*/        
        JavaRDD<Double> mapRdd = doubleRdd.map(new Function<Double, Double>() {
            public Double call(Double in) throws Exception {
                return in + 2;
            }
        });
        List<Double> douList = mapRdd.collect();
        for (Double d : douList) {
            System.out.println("d:" + d);
        }
    }
}
文章转载自原文:https://blog.****.net/qq_44596980/article/details/93384494