通过IntelliJ IDEA编写及编译spark(scala)程序

通过IntelliJ IDEA编写及编译spark(scala)程序的步骤如下:

创建工程

  • 选择File->New->Project,弹出如下界面
    通过IntelliJ IDEA编写及编译spark(scala)程序
  • 选择Scala下的SBT,点击Next,进入新工程设置界面
    通过IntelliJ IDEA编写及编译spark(scala)程序
  • 设置工程名称,可选择scala版本,点击Finish完成新工程创建如下
    通过IntelliJ IDEA编写及编译spark(scala)程序

添加程序文件

  • 右键左侧导航栏目录结构src->main->scala,弹出菜单如下
    通过IntelliJ IDEA编写及编译spark(scala)程序
  • 选择New-》File,弹出设置文件名字对话框
    通过IntelliJ IDEA编写及编译spark(scala)程序
  • 设置好文件名,点击OK,由于没有设置扩展名,会弹出确认文件类型的对话框
    通过IntelliJ IDEA编写及编译spark(scala)程序
  • 选择Scala,点击OK完成,可见导航路径下多了Test文件
    通过IntelliJ IDEA编写及编译spark(scala)程序
  • 现在可以编写spark(scala)程序了
    通过IntelliJ IDEA编写及编译spark(scala)程序

添加spark库

编写spark(scala)程序时,由于工程没有添加spark库,会找不到库的情况(见上图红字),需要手动添加spark库路径

  • 选择File->Project Structure,弹出如下的设置窗口
    通过IntelliJ IDEA编写及编译spark(scala)程序
  • 选择Libraries,点击中间+号,在弹出的下拉列表中选择Java,弹出如下的窗口
    通过IntelliJ IDEA编写及编译spark(scala)程序
  • 选择spark安装目录/jars后,点击OK,在弹出的窗口再点击OK,可以看到添加spark的库
    通过IntelliJ IDEA编写及编译spark(scala)程序
  • 别忘了点击上面窗口的Apply,否则不会将刚才选择的spark库真正添加到工程中,点击后可看到工程的外部依赖有了spark库,spark(scala)程序中也没有了红色字体了
    通过IntelliJ IDEA编写及编译spark(scala)程序

编译spark(scala)为Jar包

  • 点击Build->Build Project,没有程序错误即可编译成功,编译完成后,在工程目录/target/scala-2.1.1/classes目录下,有编译生成的class文件
    通过IntelliJ IDEA编写及编译spark(scala)程序
    通过IntelliJ IDEA编写及编译spark(scala)程序
  • Spark只能提交jar包,所以还需要打包成jar包。首先选择File->Project Structure,在弹出的窗口中选择Artifacts
    通过IntelliJ IDEA编写及编译spark(scala)程序
  • 点击+号,选择JAR->Empty后,弹出设置界面
    通过IntelliJ IDEA编写及编译spark(scala)程序
  • 设置好Jar包名称后,点击+号,选择Model Output,在弹出的窗口中点击OK即可
    通过IntelliJ IDEA编写及编译spark(scala)程序
  • 回到原来界面,点击OK即完成设置
  • 点击Build->Build Artifacts
    通过IntelliJ IDEA编写及编译spark(scala)程序
  • 会弹出小菜单,选择Build,就开始生成Jar包
    通过IntelliJ IDEA编写及编译spark(scala)程序
  • 编译完成后,可在工程目录/out/artifacts/jar名字文件夹下,看到生成的jar包,该jar可用于提交spark集群
    通过IntelliJ IDEA编写及编译spark(scala)程序