环境:

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程序了,下边附上一段小的测试代码:

  1. import java.util.Arrays;
  2.  
  3. import org.apache.spark.SparkConf;
  4. import org.apache.spark.api.java.JavaPairRDD;
  5. import org.apache.spark.api.java.JavaRDD;
  6. import org.apache.spark.api.java.JavaSparkContext;
  7. import org.apache.spark.api.java.function.FlatMapFunction;
  8. import org.apache.spark.api.java.function.Function2;
  9. import org.apache.spark.api.java.function.PairFunction;
  10. import org.apache.spark.api.java.function.VoidFunction;
  11.  
  12. import scala.Tuple2;
  13.  
  14. public class WordCount {
  15. public static void main(String[] args) {
  16. SparkConf conf = new SparkConf().setMaster("local").setAppName("wc");
  17. JavaSparkContext sc = new JavaSparkContext(conf);
  18.  
  19. JavaRDD<String> text = sc.textFile("hdfs://master:9000/user/hadoop/input/test");
  20. JavaRDD<String> words = text.flatMap(new FlatMapFunction<String, String>() {
  21. private static final long serialVersionUID = 1L;
  22. @Override
  23. public Iterable<String> call(String line) throws Exception {
  24. return Arrays.asList(line.split(" "));//把字符串转化成list
  25. }
  26. });
  27.  
  28. JavaPairRDD<String, Integer> pairs = words.mapToPair(new PairFunction<String, String, Integer>() {
  29. private static final long serialVersionUID = 1L;
  30. @Override
  31. public Tuple2<String, Integer> call(String word) throws Exception {
  32. // TODO Auto-generated method stub
  33. return new Tuple2<String, Integer>(word, 1);
  34. }
  35. });
  36.  
  37. JavaPairRDD<String, Integer> results = pairs.reduceByKey(new Function2<Integer, Integer, Integer>() {
  38. private static final long serialVersionUID = 1L;
  39. @Override
  40. public Integer call(Integer value1, Integer value2) throws Exception {
  41. // TODO Auto-generated method stub
  42. return value1 + value2;
  43. }
  44. });
  45.  
  46. JavaPairRDD<Integer, String> temp = results.mapToPair(new PairFunction<Tuple2<String,Integer>, Integer, String>() {
  47. private static final long serialVersionUID = 1L;
  48. @Override
  49. public Tuple2<Integer, String> call(Tuple2<String, Integer> tuple)
  50. throws Exception {
  51. return new Tuple2<Integer, String>(tuple._2, tuple._1);
  52. }
  53. });
  54.  
  55. JavaPairRDD<String, Integer> sorted = temp.sortByKey(false).mapToPair(new PairFunction<Tuple2<Integer,String>, String, Integer>() {
  56. private static final long serialVersionUID = 1L;
  57. @Override
  58. public Tuple2<String, Integer> call(Tuple2<Integer, String> tuple)
  59. throws Exception {
  60. // TODO Auto-generated method stub
  61. return new Tuple2<String, Integer>(tuple._2,tuple._1);
  62. }
  63. });
  64.  
  65. sorted.foreach(new VoidFunction<Tuple2<String,Integer>>() {
  66. private static final long serialVersionUID = 1L;
  67. @Override
  68. public void call(Tuple2<String, Integer> tuple) throws Exception {
  69. System.out.println("word:" + tuple._1 + " count:" + tuple._2);
  70. }
  71. });
  72.  
  73. sc.close();
  74. }
  75. }

统计的文件是下边的内容:

  1. Look! at the window there leans an old maid. She plucks the
  2.  
  3. withered leaf from the balsam, and looks at the grass-covered rampart,
  4.  
  5. on which many children are playing. What is the old maid thinking
  6.  
  7. of? A whole life drama is unfolding itself before her inward gaze.
  8.  
  9. "The poor little children, how happy they are- how merrily they
  10.  
  11. play and romp together! What red cheeks and what angels' eyes! but
  12.  
  13. they have no shoes nor stockings. They dance on the green rampart,
  14.  
  15. just on the place where, according to the old story, the ground always
  16.  
  17. sank in, and where a sportive, frolicsome child had been lured by
  18.  
  19. means of flowers, toys and sweetmeats into an open grave ready dug for
  20.  
  21. it, and which was afterwards closed over the child; and from that
  22.  
  23. moment, the old story says, the ground gave way no longer, the mound
  24.  
  25. remained firm and fast, and was quickly covered with the green turf.
  26.  
  27. The little people who now play on that spot know nothing of the old
  28.  
  29. tale, else would they fancy they heard a child crying deep below the
  30.  
  31. earth, and the dewdrops on each blade of grass would be to them
  32.  
  33. tears of woe. Nor do they know anything of the Danish King who here,
  34.  
  35. in the face of the coming foe, took an oath before all his trembling
  36.  
  37. courtiers that he would hold out with the citizens of his capital, and
  38.  
  39. die here in his nest; they know nothing of the men who have fought
  40.  
  41. here, or of the women who from here have drenched with boiling water
  42.  
  43. the enemy, clad in white, and 'biding in the snow to surprise the
  44.  
  45. city.
  46.  
  47. .

当然也可以把这个工程打包成jar包,放在spark集群上运行,比如我打成jar包的名称是WordCount.jar

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

windows下 eclipse搭建spark java编译环境的更多相关文章

  1. Win7 Eclipse 搭建spark java1.8环境:WordCount helloworld例子

    [学习笔记] Win7 Eclipse 搭建spark java1.8环境:WordCount helloworld例子在eclipse oxygen上创建一个普通的java项目,然后把spark-a ...

  2. 配置 Windows 下的 nodejs C++ 模块编译环境 安装 node-gyp

    配置 Windows 下的 nodejs C++ 模块编译环境 根据 node-gyp 指示的 Windows 编译环境说明, 简单一句话就是 "Python + VC++ 编译环境&quo ...

  3. windows下eclipse搭建android_ndk开发环境

    安装cygwin: 由于NDK编译代码时必须要用到make和gcc,所以你必须先搭建一个linux环境, cygwin是一个在windows平台上运行的unix模拟环境,它对于学习unix/linux ...

  4. Ubuntu杂记——Ubuntu下Eclipse搭建Maven、SVN环境

    正在实习的公司项目是使用Maven+SVN管理的,所以转到Ubuntu下也要靠自己搭环境,自己动手,丰衣足食.步骤有点简略,但还是能理解的. 一.安装JDK7 打开终端(Ctrl+Alt+T),输入  ...

  5. Ubuntu(Linux)使用Eclipse搭建C/C++编译环境

    转自:http://www.cppblog.com/kangnixi/archive/2010/02/10/107636.html 首先是安装Eclipse,方法有两种:       第一种是通过Ub ...

  6. Ubuntu 12.04 使用Eclipse搭建C/C++编译环境

    首先是安装Eclipse,方法有两种:       第一种是通过Ubuntu自带的程序安装功能安装Eclipse,应用程序->Ubtuntu软件中心,搜Eclipse安装即可.       第二 ...

  7. Windows下Eclipse+PyDev配置Python开发环境

    1.简介 Eclipse是一款基于Java的可扩展开发平台.其官方下载中包括J2EE.Java.C/C++.Android等诸多版本.除此之外,Eclipse还可以通过安装插件的方式进行包括Pytho ...

  8. Windows下Eclipse+PyDev安装Python开发环境

    .简介 Eclipse是一款基于Java的可扩展开发平台.其官方下载中包括J2EE方向版本.Java方向版本.C/C++方向版本.移动应用方向版本等诸多版本.除此之外,Eclipse还可以通过安装插件 ...

  9. Windows下配置cygwin和ndk编译环境

    cygwin安装 正确的安装步骤其实很简单:1. 下载setup-86_64.exe 2. 直接从网上下载安装,选择包时,顶部选择“default”不变 3. 搜索make,勾选make,cmake, ...

随机推荐

  1. MVVM、MVC框架的认识

    推荐博客: https://blog.csdn.net/jia12216/article/details/55520426 https://www.cnblogs.com/sunny_z/p/7093 ...

  2. #leetcode刷题之路20-有效的括号

    #include <iostream> #include <string> #include <stack> using namespace std; bool i ...

  3. 第13届景驰-埃森哲杯广东工业大学ACM程序设计大赛--K-密码

    链接:https://www.nowcoder.com/acm/contest/90/K 来源:牛客网 - 1.题目描述 ZiZi登录各种账号的时候,总是会忘记密码,所以他把密码都记录在一个记事本上. ...

  4. spring boot 小案例

    1. SpringBoot 1.1. 概要 在传统的SSM框架应用过程中,存在大量的配置文件,及相关的配置项,例如: 1. DispatcherServlet 2. CharacterEncoding ...

  5. Percona-Tookit工具包之pt-table-sync

      Preface       We've used pt-table-checksum to checksum the different table data bwtween replicatio ...

  6. Ajax异步交互

    一.简介 Ajax(Asynchronous JavaScript and XML).一般都写为Ajax. Ajax是与服务器交换数组并更新部分网页的艺术.最初的使用时2005中Google Sugg ...

  7. (转)Windows安装和使用zookeeper

    (转)原地址https://www.cnblogs.com/shanyou/p/3221990.html 之前整理过一篇文章<zookeeper 分布式锁服务>,本文介绍的 Zookeep ...

  8. linux服务基础之ftp服务

    ftp是一种文件传输协议,我们以redhat6.9为服务器系统,来介绍一下ftp服务器,这里我们先介绍一下ftp协议工作的原理 ftp协议可以在不同类型的计算机之间传输文件,工作流程大致为 1:客户机 ...

  9. linux下进程的最大线程数、进程最大数、进程打开的文件数

    linux下进程的最大线程数.进程最大数.进程打开的文件数   ===========最大线程数============== linux 系统中单个进程的最大线程数有其最大的限制 PTHREAD_TH ...

  10. background兼容IE9以下版本

    .box {    width:100%;    height:80%;        background: url('img/nav_bg.png') no-repeat;    backgrou ...