Table of Contents
1. IDEA环境配置
2. 使用IDEA开发Spark程序并运行
参考:
Home Database Mysql Tutorial 使用IDEA开发Spark应用

使用IDEA开发Spark应用

Jun 07, 2016 pm 04:38 PM
idea in spark use application develop

IDEA 全称IntelliJ IDEA,是java语言开发的集成环境,IntelliJ在业界被公认为最好的java开发工具之一,尤其在智能代码助手、代码自动提示、重构、J2EE支持、Ant、JUnit、CVS整合、代码审查、 创新的GUI设计等方面的功能都非常棒,而且IDEA是目前Scala支持最

IDEA 全称IntelliJ IDEA,是java语言开发的集成环境,IntelliJ在业界被公认为最好的java开发工具之一,尤其在智能代码助手、代码自动提示、重构、J2EE支持、Ant、JUnit、CVS整合、代码审查、 创新的GUI设计等方面的功能都非常棒,而且IDEA是目前Scala支持最好的IDE。IDEA分ultimate和free edition版,ultimate提供了J2EE等很多非常强力的功能,free edition我觉得已经对于我这样的初学者已经够用了。前面写过一篇配置IntelliJ IDEA 13的SBT和Scala开发环境,本文在这个基础上使用IDEA进行Spark应用的配置和开发。

1. IDEA环境配置

(1). 首先在IntellJ/bin/idea64.exe.vmoptions(对应64位大内存系统),加大IDEA的启动内存:

-Xms512m
-Xmx1024m
-XX:MaxPermSize=512m
Copy after login

(2). 在IDEA中,Project相当于eclipse中的workspace,同一IDEA窗口只能打开一个workspace。而IDEA中的module等同于eclipse中的project,所以通过File – New Module来为当前Project创建一个module。
1
(3). IDEA会生成大量的缓存文件,来于保存配置信息、插件和项目索引文件等。,一般都会有代码的十倍大小左右大小。在Windows下目录为C:\Users\THINKP\.IntelliJIdea13,使用File – Invalidate Caches可以校验索引的有效性并在需要的时候重建。IDEA会经常读写这些缓存文件,所以使用SSD来存储缓存文件会提高不少性能。下面是修改缓存文件路径的方法:
a). 关闭IDEA
b). 将cache目录复制到对应的目录下面。
c). 打开IntelliJ IDEA 13.1.3\bin\idea.properties文件,例如将IDEA转移到目录D:\Program Files\.IntelliJIdea13中,只需要修改
idea.config.path=D:/Program Files/.IntelliJIdea13/config
idea.system.path=D:/Program Files/.IntelliJIdea13/system
(4). 主题和颜色
Settings – IDE Settings – Appearance – Theme:Darcula
然后把下面override font选项勾上,选择Yahei 14号字体。
然后重启IDEA,界面变成了灰黑色风格,瞬间顺眼了很多!
2
编辑器可以设置单独的主题,当前面设置了全局主题时,编辑器的主题也会被修改。接下来,编辑器界面字体有点小,可以在Editor – Colors&Fonts – Fonts另存为一个新的主题,并在这个新主题中修改配置。我的屏幕分辨率有点大,所以设置了15号字体。
3
光标所在行背景颜色
Editor – Colors&Fonts – General – Caret row,选择了蓝色背景,这样就有了较大的色差。
4
(5). 常用快捷键
界面中的Alt+1 project窗口
Alt+7 代码结构图
Alt+2 Favorite
F11打书签,再按一次取消。此时Favorite - Bookmark里就有这一项。
TODO list Alt+6
注释中以TODO开头时,该TODO项就可以在TODO标签页中找到。这样在有一些思路但是来不及做时,可以以TODO的形式写注释
5
同步项目(Detect all externally changed files and reload them from disk)Ctrl+Y
保存(Save all) Ctrl+S
undo Ctrl+Z
redo Ctrl+Shift+Y
剪切 Ctrl+X
复制 Ctrl+C
粘贴 Ctrl+V
查找 Ctrl+F
替换 Ctrl+R
光标的上一个位置(undo navigation) Ctrl+Alt+<br> 光标的下一个位置(redo navigation) <code>Ctrl+Alt+->
make Ctrl+F9
(6). 项目文件设定
行分割模式: File - Separators 选择Windows风格(/r/n), UNIX的风格(/n)或者mac风格(/r)等等。
将文件锁定编辑 - File - Make file read only
文件编码设置 Project Settings - File Encodings
推荐YouMeek IDEA教程,我认为是目前详细的IDEA教程之一。
http://www.youmeek.com/category/software-system/my-intellij-idea/

2. 使用IDEA开发Spark程序并运行

首先编辑build.sbt文件,每个配置项都要有一个空格来分割。

build.sbt
name := "sbtTest"
version := "1.0"
scalaVersion := "2.10.4"
libraryDependencies += "org.apache.spark" %% "spark-core_2.10" % "1.0.2"
libraryDependencies += "org.apache.spark" % "spark-bagel_2.10" % "1.0.2"
libraryDependencies += "org.apache.spark" % "spark-mllib_2.10" % "1.0.2"
libraryDependencies += "org.apache.spark" % "spark-graphx_2.10" % "1.0.2"
libraryDependencies += "org.apache.spark" % "spark-streaming_2.10" % "1.0.2"
Copy after login

打开SBT,可以观察到SBT正在downloading dependencies。

...
[info] downloading http://repo1.maven.org/maven2/org/apache/spark/spark-bagel_2.10/1.0.2/spark-bagel_2.10-1.0.2.jar ...
[info] 	[SUCCESSFUL ] org.apache.spark#spark-bagel_2.10;1.0.2!spark-bagel_2.10.jar (5672ms)
[info] downloading http://repo1.maven.org/maven2/org/apache/spark/spark-mllib_2.10/1.0.2/spark-mllib_2.10-1.0.2.jar ...
[info] 	[SUCCESSFUL ] org.apache.spark#spark-mllib_2.10;1.0.2!spark-mllib_2.10.jar (7351ms)
[info] downloading http://repo1.maven.org/maven2/org/apache/spark/spark-graphx_2.10/1.0.2/spark-graphx_2.10-1.0.2.jar ...
[info] 	[SUCCESSFUL ] org.apache.spark#spark-graphx_2.10;1.0.2!spark-graphx_2.10.jar (6349ms)
...
...
Copy after login

编写代码,这段代码用于处理web前端日志,其中第二列是session的ID,输出Session访问次数的排名。

/**
 * Created by Debugo on 2014/8/25.
 */
import org.apache.spark.{SparkContext, SparkConf}
import org.apache.spark.SparkContext._
object LogAnalyzer {
  def main(args:Array[String]): Unit ={
    if(args.length!=2) {
      System.err.println("Usage: LogAnalyzer  ")
      System.exit(1)
    }
    val conf = new SparkConf().setAppName("LogAnalyzer")
    val sc = new SparkContext(conf)
    // args(0)=file:///root/access_log/access_log.20080601.decode.filter
    // args(1)=file:///root/access_log/result
    sc.textFile(args(0)).map(_.split("\t| ")).filter(_.length==6).
      map(x=>(x(1),1)).reduceByKey(_+_).map(x=>(x._2,x._1)).
      sortByKey(false).map(x=>(x._2,x._1)).saveAsTextFile(args(1))
    sc.stop()
  }
}
Copy after login

在sbt命令行中中compile&package

> compile
[info] Compiling 1 Scala source to C:\Users\Administrator\IdeaProjects\Spark0\target\scala-2.10\classes...
[success] Total time: 5 s, completed 2014-8-25 16:05:20
>   package
[info] Packaging C:\Users\Administrator\IdeaProjects\Spark0\target\scala-2.10\spark0_2.10-1.0.jar ...
[info] Done packaging.
[success] Total time: 0 s, completed 2014-8-25 16:17:12
Copy after login

将jar上传到配置spark运行库的节点,提交job,spark会创建结果输出的result目录。最终RDD被分割成了5个分区。

spark-submit --master spark://debugo:7077 --class LogAnalyzer --executor-memory=10g /root/spark0_2.10-1.0.jar file:///root/access_log/access_log.20080601.decode.filter file:///root/access_log/result
...
$ ll /root/access_log/result
total 10840
-rw-r--r-- 1 root root 2708325 Aug 25 15:58 part-00000
-rw-r--r-- 1 root root 1114214 Aug 25 15:58 part-00001
-rw-r--r-- 1 root root 2239113 Aug 25 15:58 part-00002
-rw-r--r-- 1 root root       0 Aug 25 15:58 part-00003
-rw-r--r-- 1 root root 5028580 Aug 25 15:58 part-00004
-rw-r--r-- 1 root root       0 Aug 25 15:58 _SUCCESS
$ more part-00000
(11579135515147154,431)
(6383499980790535,385)
(7822241147182134,370)
(900755558064074,335)
(12385969593715146,226)
...
Copy after login

得到了我们想要的按session ID的排名结果。
^^

参考:

Spark Programming Guide
mmicky Spark大数据快速计算平台

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1671
14
PHP Tutorial
1276
29
C# Tutorial
1256
24
BTCC tutorial: How to bind and use MetaMask wallet on BTCC exchange? BTCC tutorial: How to bind and use MetaMask wallet on BTCC exchange? Apr 26, 2024 am 09:40 AM

MetaMask (also called Little Fox Wallet in Chinese) is a free and well-received encryption wallet software. Currently, BTCC supports binding to the MetaMask wallet. After binding, you can use the MetaMask wallet to quickly log in, store value, buy coins, etc., and you can also get 20 USDT trial bonus for the first time binding. In the BTCCMetaMask wallet tutorial, we will introduce in detail how to register and use MetaMask, and how to bind and use the Little Fox wallet in BTCC. What is MetaMask wallet? With over 30 million users, MetaMask Little Fox Wallet is one of the most popular cryptocurrency wallets today. It is free to use and can be installed on the network as an extension

How to use NetEase Mailbox Master How to use NetEase Mailbox Master Mar 27, 2024 pm 05:32 PM

NetEase Mailbox, as an email address widely used by Chinese netizens, has always won the trust of users with its stable and efficient services. NetEase Mailbox Master is an email software specially created for mobile phone users. It greatly simplifies the process of sending and receiving emails and makes our email processing more convenient. So how to use NetEase Mailbox Master, and what specific functions it has. Below, the editor of this site will give you a detailed introduction, hoping to help you! First, you can search and download the NetEase Mailbox Master app in the mobile app store. Search for "NetEase Mailbox Master" in App Store or Baidu Mobile Assistant, and then follow the prompts to install it. After the download and installation is completed, we open the NetEase email account and log in. The login interface is as shown below

How to use Baidu Netdisk app How to use Baidu Netdisk app Mar 27, 2024 pm 06:46 PM

Cloud storage has become an indispensable part of our daily life and work nowadays. As one of the leading cloud storage services in China, Baidu Netdisk has won the favor of a large number of users with its powerful storage functions, efficient transmission speed and convenient operation experience. And whether you want to back up important files, share information, watch videos online, or listen to music, Baidu Cloud Disk can meet your needs. However, many users may not understand the specific use method of Baidu Netdisk app, so this tutorial will introduce in detail how to use Baidu Netdisk app. Users who are still confused can follow this article to learn more. ! How to use Baidu Cloud Network Disk: 1. Installation First, when downloading and installing Baidu Cloud software, please select the custom installation option.

Four recommended AI-assisted programming tools Four recommended AI-assisted programming tools Apr 22, 2024 pm 05:34 PM

This AI-assisted programming tool has unearthed a large number of useful AI-assisted programming tools in this stage of rapid AI development. AI-assisted programming tools can improve development efficiency, improve code quality, and reduce bug rates. They are important assistants in the modern software development process. Today Dayao will share with you 4 AI-assisted programming tools (and all support C# language). I hope it will be helpful to everyone. https://github.com/YSGStudyHards/DotNetGuide1.GitHubCopilotGitHubCopilot is an AI coding assistant that helps you write code faster and with less effort, so you can focus more on problem solving and collaboration. Git

How to Undo Delete from Home Screen in iPhone How to Undo Delete from Home Screen in iPhone Apr 17, 2024 pm 07:37 PM

Deleted something important from your home screen and trying to get it back? You can put app icons back on the screen in a variety of ways. We have discussed all the methods you can follow and put the app icon back on the home screen. How to Undo Remove from Home Screen in iPhone As we mentioned before, there are several ways to restore this change on iPhone. Method 1 – Replace App Icon in App Library You can place an app icon on your home screen directly from the App Library. Step 1 – Swipe sideways to find all apps in the app library. Step 2 – Find the app icon you deleted earlier. Step 3 – Simply drag the app icon from the main library to the correct location on the home screen. This is the application diagram

The role and practical application of arrow symbols in PHP The role and practical application of arrow symbols in PHP Mar 22, 2024 am 11:30 AM

The role and practical application of arrow symbols in PHP In PHP, the arrow symbol (-&gt;) is usually used to access the properties and methods of objects. Objects are one of the basic concepts of object-oriented programming (OOP) in PHP. In actual development, arrow symbols play an important role in operating objects. This article will introduce the role and practical application of arrow symbols, and provide specific code examples to help readers better understand. 1. The role of the arrow symbol to access the properties of an object. The arrow symbol can be used to access the properties of an object. When we instantiate a pair

Which AI programmer is the best? Explore the potential of Devin, Tongyi Lingma and SWE-agent Which AI programmer is the best? Explore the potential of Devin, Tongyi Lingma and SWE-agent Apr 07, 2024 am 09:10 AM

On March 3, 2022, less than a month after the birth of the world's first AI programmer Devin, the NLP team of Princeton University developed an open source AI programmer SWE-agent. It leverages the GPT-4 model to automatically resolve issues in GitHub repositories. SWE-agent's performance on the SWE-bench test set is similar to Devin, taking an average of 93 seconds and solving 12.29% of the problems. By interacting with a dedicated terminal, SWE-agent can open and search file contents, use automatic syntax checking, edit specific lines, and write and execute tests. (Note: The above content is a slight adjustment of the original content, but the key information in the original text is retained and does not exceed the specified word limit.) SWE-A

Learn how to develop mobile applications using Go language Learn how to develop mobile applications using Go language Mar 28, 2024 pm 10:00 PM

Go language development mobile application tutorial As the mobile application market continues to boom, more and more developers are beginning to explore how to use Go language to develop mobile applications. As a simple and efficient programming language, Go language has also shown strong potential in mobile application development. This article will introduce in detail how to use Go language to develop mobile applications, and attach specific code examples to help readers get started quickly and start developing their own mobile applications. 1. Preparation Before starting, we need to prepare the development environment and tools. head

See all articles