windows下 eclipse搭建spark java編譯環境


環境:

win10

jdk1.8

之前有在虛擬機或者集群上安裝spark安裝包的,解壓到你想要放spark的本地目錄下,比如我的目錄就是D:\Hadoop\spark-1.6.0-bin-hadoop2.6

/**

*注意:

之前在linux環境下安裝的spark的版本是spark-2.2.0-bin-hadoop2.6,但后來搭建eclipse的spark開發環境時發現spark-2.2.0-bin-hadoop2.6解壓后沒有lib文件,也就沒有關鍵的spark-assembly-1.6.0-hadoop2.6.0.jar這個jar包,不知道spark-2.2.0以后怎么支持eclipse的開發,所以我換了spark-1.6.0,如果有知道的大神,謝謝在下邊留言指導一下

**/

下邊就簡單了,先配置spark的環境變量,先添加一個SPARK_HOME,如下:

然后把SPARK_HOME配置到path,如下:

 這樣環境就搭好了,然后就是在eclipse上創建一個普通的java項目,然后把spark-assembly-1.6.0-hadoop2.6.0.jar這個包復制進工程並且導入,如下圖

就可以開發spark程序了,下邊附上一段小的測試代碼:

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;

public class WordCount {
    public static void main(String[] args) {
        SparkConf conf = new SparkConf().setMaster("local").setAppName("wc");
        JavaSparkContext sc = new JavaSparkContext(conf);
        
        JavaRDD<String> text = sc.textFile("hdfs://master:9000/user/hadoop/input/test");
        JavaRDD<String> words = text.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(" "));//把字符串轉化成list
            }
        });
        
        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 {
                // TODO Auto-generated method stub
                return new Tuple2<String, Integer>(word, 1);
            }
        });
        
        JavaPairRDD<String, Integer> results = pairs.reduceByKey(new Function2<Integer, Integer, Integer>() {            
            private static final long serialVersionUID = 1L;
            @Override
            public Integer call(Integer value1, Integer value2) throws Exception {
                // TODO Auto-generated method stub
                return value1 + value2;
            }
        });
        
        JavaPairRDD<Integer, String> temp = results.mapToPair(new PairFunction<Tuple2<String,Integer>, Integer, String>() {
            private static final long serialVersionUID = 1L;
            @Override
            public Tuple2<Integer, String> call(Tuple2<String, Integer> tuple)
                    throws Exception {
                return new Tuple2<Integer, String>(tuple._2, tuple._1);
            }
        });
        
        JavaPairRDD<String, Integer> sorted = temp.sortByKey(false).mapToPair(new PairFunction<Tuple2<Integer,String>, String, Integer>() {
            private static final long serialVersionUID = 1L;
            @Override
            public Tuple2<String, Integer> call(Tuple2<Integer, String> tuple)
                    throws Exception {
                // TODO Auto-generated method stub
                return new Tuple2<String, Integer>(tuple._2,tuple._1);
            }
        });
        
        sorted.foreach(new VoidFunction<Tuple2<String,Integer>>() {
            private static final long serialVersionUID = 1L;
            @Override
            public void call(Tuple2<String, Integer> tuple) throws Exception {
                System.out.println("word:" + tuple._1 + " count:" + tuple._2);
            }
        });
        
        sc.close();
    }
}

統計的文件是下邊的內容:

 Look! at the window there leans an old maid. She plucks the  
  
withered leaf from the balsam, and looks at the grass-covered rampart,  
  
on which many children are playing. What is the old maid thinking  
  
of? A whole life drama is unfolding itself before her inward gaze.  
  
    "The poor little children, how happy they are- how merrily they  
  
play and romp together! What red cheeks and what angels' eyes! but  
  
they have no shoes nor stockings. They dance on the green rampart,  
  
just on the place where, according to the old story, the ground always  
  
sank in, and where a sportive, frolicsome child had been lured by  
  
means of flowers, toys and sweetmeats into an open grave ready dug for  
  
it, and which was afterwards closed over the child; and from that  
  
moment, the old story says, the ground gave way no longer, the mound  
  
remained firm and fast, and was quickly covered with the green turf.  
  
The little people who now play on that spot know nothing of the old  
  
tale, else would they fancy they heard a child crying deep below the  
  
earth, and the dewdrops on each blade of grass would be to them  
  
tears of woe. Nor do they know anything of the Danish King who here,  
  
in the face of the coming foe, took an oath before all his trembling  
  
courtiers that he would hold out with the citizens of his capital, and  
  
die here in his nest; they know nothing of the men who have fought  
  
here, or of the women who from here have drenched with boiling water  
  
the enemy, clad in white, and 'biding in the snow to surprise the  
  
city.  
  
.  

 當然也可以把這個工程打包成jar包,放在spark集群上運行,比如我打成jar包的名稱是WordCount.jar

運行命令:/usr/local/spark/bin/spark-submit --master local --class cn.spark.test.WordCount /home/hadoop/Desktop/WordCount.jar


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM