使用idea进行spark开发(基于maven)

转载自:https://blog.csdn.net/u012373815/article/details/53266301

使用idea构建maven 管理的spark项目 ,默认已经装好了idea 和Scala,mac安装Scala
那么使用idea 新建maven 管理的spark 项目有以下几步:

  • scala插件的安装
  • 全局JDK和Library的设置
  • 配置全局的Scala SDK
  • 新建maven项目
  • 属于你的”Hello World!”
  • 导入spark依赖
  • 编写sprak代码
  • 打包在spark上运行

1.scala插件的安装

首先在欢迎界面点击Configure,选择plugins如下图所示:

这里写图片描述

因为的安装过了所以事uninstall 没有安装的话是 install ,安装成功后,点击OK退出。

这里写图片描述

注意:插件安装完了之后,记得重启一下IntelliJ IDEA使得插件能够生效。


2.全局JDK和Library的设置

为了不用每次都去配置JDK,这里先进行一次全局配置。首先在欢迎界面点击Configure,然后在Project Defaults的下拉菜单中选择Project Structure,如下图所示:

这里写图片描述

在打开的Default Project Structure界面的左侧边栏选择Project,在右侧打开的页面中创建一个新的JDK选项(一定要本机已经安装过JDK了),如下图所示步骤在下拉菜单中点击JDK后,在打开的对话框中选择你所安装JDK的位置,注意是JDK安装的根目录,就是JAVA_HOME中设置的目录。

这里写图片描述


3.配置全局的Scala SDK

在欢迎页面的右下角点击Configure,然后在Project Defaults的下拉菜单中选择Project Structure,在打开的页面左侧选择Global Libraries,然后在中间一栏中有一个绿色的加号标志 +,点击后在下拉菜单中选择 Scala SDK

然后在打开的对话框中选择系统本身所安装的Scala(即System对应的版本),点击OK确定,这时候会在中间一栏位置处出现Scala的SDK,在其上右键点击后选择Copy to Project Libraries…,这个操作是为了将Scala SDK添加到项目的默认Library中去。整个流程如下面的动图所示。

这里写图片描述


4.新建maven项目

在欢迎界面点击Create New Project,在打开的页面左侧边栏中,选择Maven,然后在右侧的Project SDK一项中,查看是否是正确的JDK配置项正常来说这一栏会自动填充的,因为我们之前在1.3中已经配置过了全局的Project JDK了,如果这里没有正常显示JDK的话,可以点击右侧的New…按钮,然后指定JDK安装路径的根目录即可),然后点击Next,来到Maven项目最重要三个参数的设置页面,这三个参数分别为:GroupId, ArtifactId和Version. 步骤如下图所示:

这里写图片描述


5.属于你的”Hello World!”

在上一步中,我们已经创建了一个Maven工程

  1. 为了让你的首次体验Scala更清爽一些,将一些暂时无关的文件和文件夹都勇敢的删除掉吧,主要有 main\java, main\resources 和 test 这三个;


  2. 将Scala的框架添加到这个项目中,方法是在左侧栏中的项目名称上右键菜单中点击Add Framework Support…,然后在打开的对话框左侧边栏中,勾选Scala前面的复选框,然后点击确定即可(前提是上文中所述步骤都已正确走通,否则你很有可能看不到Scala这个选项的);


  3. 在main文件夹中建立一个名为 scala 的文件夹,并右键点击 scala 文件夹,选择 Make Directory as,然后选择Sources Root ,这里主要意思是将 scala 文件夹标记为一个源文件的根目录,然后在其内的所有代码中的 package ,其路径就从这个根目录下开始算起。


  4. 在已经标记好为源文件根目录的 scala 文件夹 上,右键选择 New,然后选择 Scala Class,随后设置好程序的名称,并且记得将其设置为一个 Object(类似于Java中含有静态成员的静态类),正常的话,将会打开这个 Object 代码界面,并且可以看到IntelliJ IDEA自动添加了一些最基本的信息;


在创建的 Object 中输入如下语句:

def main(args: Array[String]):Unit = {
println("Hello World!")
}
   
   
  • 1
  • 2
  • 3
5.在程序界面的任意位置,右键单击后选择 Run '你的程序名称',静待程序的编译和运行,然后在下方自动打开的窗口中,你就可以看到振奋人心的 Hello World!了。

   
   
  • 1

流程动图如下:
这里写图片描述


6. 导入spark依赖

此时你已经可以成功的运行一个Scala 项目了。想要运行在spark 上则还需要导入相关依赖。打开pom.xml文件添加如下依赖。

注意:是添加如下依赖;spark 和Scala的版本是对应的。

<properties>
        <spark.version>2.0.2</spark.version>
        <scala.version>2.11</scala.version>
    </properties>
<span class="hljs-tag">&lt;<span class="hljs-title">dependencies</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">dependency</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-title">groupId</span>&gt;</span>org.apache.spark<span class="hljs-tag">&lt;/<span class="hljs-title">groupId</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-title">artifactId</span>&gt;</span>spark-core_${scala.version}<span class="hljs-tag">&lt;/<span class="hljs-title">artifactId</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-title">version</span>&gt;</span>${spark.version}<span class="hljs-tag">&lt;/<span class="hljs-title">version</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-title">dependency</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">dependency</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-title">groupId</span>&gt;</span>org.apache.spark<span class="hljs-tag">&lt;/<span class="hljs-title">groupId</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-title">artifactId</span>&gt;</span>spark-streaming_${scala.version}<span class="hljs-tag">&lt;/<span class="hljs-title">artifactId</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-title">version</span>&gt;</span>${spark.version}<span class="hljs-tag">&lt;/<span class="hljs-title">version</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-title">dependency</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">dependency</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-title">groupId</span>&gt;</span>org.apache.spark<span class="hljs-tag">&lt;/<span class="hljs-title">groupId</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-title">artifactId</span>&gt;</span>spark-sql_${scala.version}<span class="hljs-tag">&lt;/<span class="hljs-title">artifactId</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-title">version</span>&gt;</span>${spark.version}<span class="hljs-tag">&lt;/<span class="hljs-title">version</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-title">dependency</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">dependency</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-title">groupId</span>&gt;</span>org.apache.spark<span class="hljs-tag">&lt;/<span class="hljs-title">groupId</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-title">artifactId</span>&gt;</span>spark-hive_${scala.version}<span class="hljs-tag">&lt;/<span class="hljs-title">artifactId</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-title">version</span>&gt;</span>${spark.version}<span class="hljs-tag">&lt;/<span class="hljs-title">version</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-title">dependency</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">dependency</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-title">groupId</span>&gt;</span>org.apache.spark<span class="hljs-tag">&lt;/<span class="hljs-title">groupId</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-title">artifactId</span>&gt;</span>spark-mllib_${scala.version}<span class="hljs-tag">&lt;/<span class="hljs-title">artifactId</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-title">version</span>&gt;</span>${spark.version}<span class="hljs-tag">&lt;/<span class="hljs-title">version</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-title">dependency</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-title">dependencies</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-title">build</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-title">plugins</span>&gt;</span>

        <span class="hljs-tag">&lt;<span class="hljs-title">plugin</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-title">groupId</span>&gt;</span>org.scala-tools<span class="hljs-tag">&lt;/<span class="hljs-title">groupId</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-title">artifactId</span>&gt;</span>maven-scala-plugin<span class="hljs-tag">&lt;/<span class="hljs-title">artifactId</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-title">version</span>&gt;</span>2.15.2<span class="hljs-tag">&lt;/<span class="hljs-title">version</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-title">executions</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-title">execution</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-title">goals</span>&gt;</span>
                        <span class="hljs-tag">&lt;<span class="hljs-title">goal</span>&gt;</span>compile<span class="hljs-tag">&lt;/<span class="hljs-title">goal</span>&gt;</span>
                        <span class="hljs-tag">&lt;<span class="hljs-title">goal</span>&gt;</span>testCompile<span class="hljs-tag">&lt;/<span class="hljs-title">goal</span>&gt;</span>
                    <span class="hljs-tag">&lt;/<span class="hljs-title">goals</span>&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-title">execution</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-title">executions</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-title">plugin</span>&gt;</span>

        <span class="hljs-tag">&lt;<span class="hljs-title">plugin</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-title">artifactId</span>&gt;</span>maven-compiler-plugin<span class="hljs-tag">&lt;/<span class="hljs-title">artifactId</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-title">version</span>&gt;</span>3.6.0<span class="hljs-tag">&lt;/<span class="hljs-title">version</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-title">configuration</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-title">source</span>&gt;</span>1.8<span class="hljs-tag">&lt;/<span class="hljs-title">source</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-title">target</span>&gt;</span>1.8<span class="hljs-tag">&lt;/<span class="hljs-title">target</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-title">configuration</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-title">plugin</span>&gt;</span>

        <span class="hljs-tag">&lt;<span class="hljs-title">plugin</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-title">groupId</span>&gt;</span>org.apache.maven.plugins<span class="hljs-tag">&lt;/<span class="hljs-title">groupId</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-title">artifactId</span>&gt;</span>maven-surefire-plugin<span class="hljs-tag">&lt;/<span class="hljs-title">artifactId</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-title">version</span>&gt;</span>2.19<span class="hljs-tag">&lt;/<span class="hljs-title">version</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-title">configuration</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-title">skip</span>&gt;</span>true<span class="hljs-tag">&lt;/<span class="hljs-title">skip</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-title">configuration</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-title">plugin</span>&gt;</span>

    <span class="hljs-tag">&lt;/<span class="hljs-title">plugins</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-title">build</span>&gt;</span><div class="hljs-button {2}" data-title="复制" data-report-click="{&quot;spm&quot;:&quot;1001.2101.3001.4259&quot;}"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li><li style="color: rgb(153, 153, 153);">17</li><li style="color: rgb(153, 153, 153);">18</li><li style="color: rgb(153, 153, 153);">19</li><li style="color: rgb(153, 153, 153);">20</li><li style="color: rgb(153, 153, 153);">21</li><li style="color: rgb(153, 153, 153);">22</li><li style="color: rgb(153, 153, 153);">23</li><li style="color: rgb(153, 153, 153);">24</li><li style="color: rgb(153, 153, 153);">25</li><li style="color: rgb(153, 153, 153);">26</li><li style="color: rgb(153, 153, 153);">27</li><li style="color: rgb(153, 153, 153);">28</li><li style="color: rgb(153, 153, 153);">29</li><li style="color: rgb(153, 153, 153);">30</li><li style="color: rgb(153, 153, 153);">31</li><li style="color: rgb(153, 153, 153);">32</li><li style="color: rgb(153, 153, 153);">33</li><li style="color: rgb(153, 153, 153);">34</li><li style="color: rgb(153, 153, 153);">35</li><li style="color: rgb(153, 153, 153);">36</li><li style="color: rgb(153, 153, 153);">37</li><li style="color: rgb(153, 153, 153);">38</li><li style="color: rgb(153, 153, 153);">39</li><li style="color: rgb(153, 153, 153);">40</li><li style="color: rgb(153, 153, 153);">41</li><li style="color: rgb(153, 153, 153);">42</li><li style="color: rgb(153, 153, 153);">43</li><li style="color: rgb(153, 153, 153);">44</li><li style="color: rgb(153, 153, 153);">45</li><li style="color: rgb(153, 153, 153);">46</li><li style="color: rgb(153, 153, 153);">47</li><li style="color: rgb(153, 153, 153);">48</li><li style="color: rgb(153, 153, 153);">49</li><li style="color: rgb(153, 153, 153);">50</li><li style="color: rgb(153, 153, 153);">51</li><li style="color: rgb(153, 153, 153);">52</li><li style="color: rgb(153, 153, 153);">53</li><li style="color: rgb(153, 153, 153);">54</li><li style="color: rgb(153, 153, 153);">55</li><li style="color: rgb(153, 153, 153);">56</li><li style="color: rgb(153, 153, 153);">57</li><li style="color: rgb(153, 153, 153);">58</li><li style="color: rgb(153, 153, 153);">59</li><li style="color: rgb(153, 153, 153);">60</li><li style="color: rgb(153, 153, 153);">61</li><li style="color: rgb(153, 153, 153);">62</li><li style="color: rgb(153, 153, 153);">63</li><li style="color: rgb(153, 153, 153);">64</li><li style="color: rgb(153, 153, 153);">65</li><li style="color: rgb(153, 153, 153);">66</li><li style="color: rgb(153, 153, 153);">67</li><li style="color: rgb(153, 153, 153);">68</li><li style="color: rgb(153, 153, 153);">69</li><li style="color: rgb(153, 153, 153);">70</li><li style="color: rgb(153, 153, 153);">71</li><li style="color: rgb(153, 153, 153);">72</li></ul></pre> 

导入依赖以后记得点击,这个引入jar 包哦

这里写图片描述


7. 编写sprak代码

依赖添加成功后,新建scala 的object 文件然后填写如下代码

//本案例是新建一个int 型的List数组,对数组中的每个元素乘以3 ,再过滤出来数组中大于10 的元素,然后对数组求和。

import org.apache.spark.{SparkConf, SparkContext}

/**

  • Created by yangyibo on 16/11/21.
    */
    object MySpark {

def main(args: Array[String]) {
val conf = new SparkConf().setAppName(“mySpark”)
//setMaster(“local”) 本机的spark就用local,远端的就写ip
//如果是打成jar包运行则需要去掉 setMaster(“local”)因为在参数中会指定。
conf.setMaster(“local”)
val sc =new SparkContext(conf)
val rdd =sc.parallelize(List(1,2,3,4,5,6)).map(*3)
val mappedRDD=rdd.filter(
>10).collect()
//对集合求和
println(rdd.reduce(+))
//输出大于10的元素
for(arg <- mappedRDD)
print(arg+" ")
println()
println(“math is work”)
}
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

代码编写好以后,右键 run ‘mySpark’ 运行。
这里写图片描述

执行结果如下:

这里写图片描述


8. 打包运行

运行成功后,可以讲代码打包成jar 包发送到远端或者本地的spark 集群上运行。打包有以下步骤

点击“File“然后选择“project Structure“

这里写图片描述

然后如图所示进行如下操作
这里写图片描述

在弹出的对话框中点击按钮,选择主类进行如下4步操作。

这里写图片描述

由于我们的jar包实在spark 上运行的,所可以删除其他不需要的依赖包,如下图所示,删除其他不需要的包,只留下红色矩形中的两个。

注意:output directory 的路径。此处是你导出 jar 的路径。

这里写图片描述


执行 bulid 构建你的jar

这里写图片描述

jar 包导出以后就可以在spark上运行了。

此时进入终端,进入到spark安装包的 bin 目录下。执行如下命令

MySpark :是启动类的名字,如果有包命,要加包名,(例如 com.edu.MySpark)

spark1:7077 : 是你远端的spark 的地址 ,(可以是 //192.168.200.66:7077) 写spark1 是因为我在/etc/hosts 中配置了环境参数,至于hosts 怎么配,请自行百度。

/Users/yangyibo/Idea/mySpark/out/artifacts/mySpark_jar/mySpark.jar: 是你jar 包的路径。

./bin/spark-submit --class MySpark --master spark://spark1:7077 /Users/yangyibo/Idea/mySpark/out/artifacts/mySpark_jar/mySpark.jar

 
 
  • 1
  • 2

下一集:sparkStreaming初尝--scala链接mysql分析

参考资料:http://www.jianshu.com/p/ecc6eb298b8f

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值