【Spark分布式内存计算框架——Spark 基础环境】6. IDEA 应用开发

第五章 IDEA 应用开发

实际开发Spark 应用程序使用IDEA集成开发环境,Spark课程所有代码均使用Scala语
言开发,利用函数式编程分析处理数据,更加清晰简洁。企业中也使用Java语言开发Spark
程序,目前基本上都是使用Java8中Lambda表达式和Stream编程实现。

5.1 构建Maven Project

创建Maven Project工程【bigdata-spark_2.11】,设置GAV三要素的值如下:
在这里插入图片描述
创建Maven Module模块【spark-chapter01_2.11】,对应的GAV三要素值如下:
在这里插入图片描述
添加依赖至POM文件中,内容如下:

<!-- 指定仓库位置,依次为aliyun、cloudera和jboss仓库 -->
<repositories>
<repository>
<id>aliyun</id>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</repository>
<repository>
<id>cloudera</id>
<url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
</repository>
<repository>
<id>jboss</id>
<url>http://repository.jboss.com/nexus/content/groups/public</url>
</repository>
</repositories>
<properties>
<scala.version>2.12.10</scala.version>
<scala.binary.version>2.12</scala.binary.version>
<spark.version>2.4.5</spark.version>
<hadoop.version>2.6.0-cdh5.16.2</hadoop.version>
</properties>
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scala.version}</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_${scala.binary.version}</artifactId>
<version>${spark.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>${hadoop.version}</version>
</dependency>
</dependencies>
<build>
<outputDirectory>target/classes</outputDirectory>
<testOutputDirectory>target/test-classes</testOutputDirectory>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
</resource>
</resources>
<!-- Maven 编译的插件 -->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

在Maven Module中创建对应文件夹,截图如下所示:
在这里插入图片描述

5.2 应用入口:SparkContext

Spark Application程序入口为:SparkContext,任何一个应用首先需要构建SparkContext对象,如下两步构建:

  • 第一步、创建SparkConf对象
    • 设置Spark Application基本信息,比如应用的名称AppName和应用运行Master
  • 第二步、传递SparkConf对象,创建SparkContext对象
    • 文档:http://spark.apache.org/docs/2.4.5/rdd-programming-guide.html
      在这里插入图片描述

5.3 编程实现:WordCount

从HDFS上读取数据,所以需要将HDFS Client配置文件放入到Maven Module资源目录下,同时设置应用运行时日志信息。
前面已经在spark-shell中编码实现词频统计WordCount,主要流程如下图所示:
在这里插入图片描述
创建SparkWordCount.scala文件,代码如下:

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}
/**
* 基于Scala语言使用SparkCore编程实现词频统计:WordCount
* 从HDFS上读取数据,统计WordCount,将结果保存到HDFS上
*/
object SparkWordCount {
	// TODO: 当应用运行在集群上的时候,MAIN函数就是Driver Program,必须创建SparkContext对象
	def main(args: Array[String]): Unit = {
		// 创建SparkConf对象,设置应用的配置信息,比如应用名称和应用运行模式
		val sparkConf: SparkConf = new SparkConf()
		.setMaster("local[2]")
		.setAppName("SparkWordCount")
		// TODO: 构建SparkContext上下文实例对象,读取数据和调度Job执行
		val sc: SparkContext = new SparkContext(sparkConf)
		// 第一步、读取数据
		// 封装到RDD集合,认为列表List
		val inputRDD: RDD[String] = sc.textFile("/datas/wordcount.data")
		// 第二步、处理数据
		// 调用RDD中函数,认为调用列表中的函数
		// a. 每行数据分割为单词
		val wordsRDD = inputRDD.flatMap(line => line.split("\\s+"))
		// b. 转换为二元组,表示每个单词出现一次
		val tuplesRDD: RDD[(String, Int)] = wordsRDD.map(word => (word, 1))
		// c. 按照Key分组聚合
		val wordCountsRDD: RDD[(String, Int)] = tuplesRDD.reduceByKey((tmp, item) => tmp + item)
		// 第三步、输出数据
		wordCountsRDD.foreach(println)
		// 保存到为存储系统,比如HDFS
		wordCountsRDD.saveAsTextFile(s"/datas/swc-output-${System.currentTimeMillis()}")
		// 为了测试,线程休眠,查看WEB UI界面
		Thread.sleep(10000000)
		// TODO:应用程序运行接收,关闭资源
		sc.stop()
	}
}

本地模式LocalMode运行应用程序,结果截图如下:
在这里插入图片描述

5.4 编程实现:TopKey

在上述词频统计WordCount代码基础上,对统计出的每个单词的词频Count,按照降序排序,获取词频次数最多Top3单词。数据结构RDD中关于排序函数有如下三个:
1)、sortByKey:针对RDD中数据类型key/value对时,按照Key进行排序
在这里插入图片描述

2)、sortBy:针对RDD中数据指定排序规则
在这里插入图片描述

3)、top:按照RDD中数据采用降序方式排序,如果是Key/Value对,按照Key降序排序
在这里插入图片描述
具体演示代码如下,建议使用sortByKey函数进行数据排序操作,慎用top函数。

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}
/**
* 基于Scala语言使用SparkCore编程实现词频统计:WordCount
* 从HDFS上读取数据,统计WordCount,将结果保存到HDFS上, 获取词频最高三个单词
*/
object SparkTopKey {
	def main(args: Array[String]): Unit = {
	// 创建SparkConf对象,设置应用的配置信息,比如应用名称和应用运行模式
	val sparkConf: SparkConf = new SparkConf()
		.setMaster("local[2]")
		.setAppName("SparkWordCount")
	// TODO: 构建SparkContext上下文实例对象,读取数据和调度Job执行
	val sc: SparkContext = new SparkContext(sparkConf)
	// 第一步、读取数据
	// 封装到RDD集合,认为列表List
	val inputRDD: RDD[String] = sc.textFile("/datas/wordcount.data")
	// 第二步、处理数据
	// 调用RDD中函数,认为调用列表中的函数
	// a. 每行数据分割为单词
	val wordsRDD = inputRDD.flatMap(line => line.split("\\s+"))
	// b. 转换为二元组,表示每个单词出现一次
	val tuplesRDD: RDD[(String, Int)] = wordsRDD.map(word => (word, 1))
	// c. 按照Key分组聚合
	val wordCountsRDD: RDD[(String, Int)] = tuplesRDD.reduceByKey((tmp, item) => tmp + item)
	// 第三步、输出数据
	wordCountsRDD.foreach(println)
	/*
		(spark,7)
		(hadoop,5)
		(hbase,1)
		(hive,3)
		(mapreduce,1)
	*/
	// TODO: 按照词频count降序排序获取前3个单词, 有三种方式
	println("======================== sortByKey =========================")
	// 方式一:按照Key排序sortByKey函数, TODO: 建议使用sortByKey函数
	/*
		def sortByKey(
		ascending: Boolean = true,
		numPartitions: Int = self.partitions.length
		): RDD[(K, V)]
	*/
	wordCountsRDD
		.map(tuple => tuple.swap) //.map(tuple => (tuple._2, tuple._1))
		.sortByKey(ascending = false)
		.take(3)
		.foreach(println)
	println("======================== sortBy =========================")
	// 方式二:sortBy函数, 底层调用sortByKey函数
	/*
		def sortBy[K](
			f: (T) => K, // T 表示RDD集合中数据类型,此处为二元组
			ascending: Boolean = true,
			numPartitions: Int = this.partitions.length
		)
		(implicit ord: Ordering[K], ctag: ClassTag[K]): RDD[T]
	*/
	wordCountsRDD
		.sortBy(tuple => tuple._2, ascending = false)
		.take(3)
		.foreach(println)
	println("======================== top =========================")
	// 方式三:top函数,含义获取最大值,传递排序规则, TODO:慎用
	/*
		def top(num: Int)(implicit ord: Ordering[T]): Array[T]
	*/
	wordCountsRDD
		.top(3)(Ordering.by(tuple => tuple._2))
		.foreach(println)
	// 为了测试,线程休眠,查看WEB UI界面
	Thread.sleep(10000000)
	// TODO:应用程序运行接收,关闭资源
	sc.stop()
	}
}

本地模式运行测试,结果截图如下
在这里插入图片描述

5.5 Spark 应用提交

使用IDEA集成开发工具开发测试Spark Application程序以后,类似MapReduce程序一样,打成jar包,使用命令【spark-submit】提交应用的执行,提交命令帮助文档:

[root@node1 ~]# /export/server/spark/bin/spark-submit --help
Usage: spark-submit [options] <app jar | python file | R file> [app arguments]
Usage: spark-submit --kill [submission ID] --master [spark://…]
Usage: spark-submit --status [submission ID] --master [spark://…]
Usage: spark-submit run-example [options] example-class [example args]
Options:
–master MASTER_URL spark://host:port, mesos://host:port, yarn,
k8s://https://host:port, or local (Default: local[*]).
–deploy-mode DEPLOY_MODE Whether to launch the driver program locally (“client”) or
on one of the worker machines inside the cluster (“cluster”)
(Default: client).
–class CLASS_NAME Your application’s main class (for Java / Scala apps).
–name NAME A name of your application.
–jars JARS Comma-separated list of jars to include on the driver
and executor classpaths.
–packages Comma-separated list of maven coordinates of jars to include
on the driver and executor classpaths. Will search the local
maven repo, then maven central and any additional remote
repositories given by --repositories. The format for the
coordinates should be groupId:artifactId:version.
–exclude-packages Comma-separated list of groupId:artifactId, to exclude while
resolving the dependencies provided in --packages to avoid
dependency conflicts.
–repositories Comma-separated list of additional remote repositories to
search for the maven coordinates given with --packages.
–py-files PY_FILES Comma-separated list of .zip, .egg, or .py files to place
on the PYTHONPATH for Python apps.
–files FILES Comma-separated list of files to be placed in the working
directory of each executor. File paths of these files
in executors can be accessed via SparkFiles.get(fileName).
–conf PROP=VALUE Arbitrary Spark configuration property.
–properties-file FILE Path to a file from which to load extra properties. If not
specified, this will look for conf/spark-defaults.conf.
–driver-memory MEM Memory for driver (e.g. 1000M, 2G) (Default: 1024M).
–driver-java-options Extra Java options to pass to the driver.
–driver-library-path Extra library path entries to pass to the driver.
–driver-class-path Extra class path entries to pass to the driver. Note that
jars added with --jars are automatically included in the
classpath.
–executor-memory MEM Memory per executor (e.g. 1000M, 2G) (Default: 1G).
–proxy-user NAME User to impersonate when submitting the application.
This argument does not work with --principal / --keytab.
–help, -h Show this help message and exit.
–verbose, -v Print additional debug output.
–version, Print the version of current Spark.
Cluster deploy mode only:
–driver-cores NUM Number of cores used by the driver, only in cluster mode
(Default: 1).
Spark standalone or Mesos with cluster deploy mode only:
–supervise If given, restarts the driver on failure.
–kill SUBMISSION_ID If given, kills the driver specified.
–status SUBMISSION_ID If given, requests the status of the driver specified.
Spark standalone and Mesos only:
–total-executor-cores NUM Total cores for all executors.
Spark standalone and YARN only:
–executor-cores NUM Number of cores per executor. (Default: 1 in YARN mode,
or all available cores on the worker in standalone mode)
YARN-only:
–queue QUEUE_NAME The YARN queue to submit to (Default: “default”).
–num-executors NUM Number of executors to launch (Default: 2).
If dynamic allocation is enabled, the initial number of
executors will be at least NUM.
–archives ARCHIVES Comma separated list of archives to be extracted into the
working directory of each executor.
–principal PRINCIPAL Principal to be used to login to KDC, while running on
secure HDFS.
–keytab KEYTAB The full path to the file that contains the keytab for the
principal specified above. This keytab will be copied to
the node running the Application Master via the Secure
Distributed Cache, for renewing the login tickets and the
delegation tokens periodically.

官方文档:http://spark.apache.org/docs/2.4.5/submitting-applications.html

应用提交语法

使用【spark-submit】提交应用语法如下:
Usage: spark-submit [options] <app jar | python file> [app arguments]
如果使用Java或Scala语言编程程序,需要将应用编译后达成Jar包形式,提交运行。
在这里插入图片描述

基本参数配置

提交运行Spark Application时,有些基本参数需要传递值,如下所示:
在这里插入图片描述
动态加载Spark Applicaiton运行时的参数,通过–conf进行指定,如下使用方式:
在这里插入图片描述

Driver Program 参数配置

每个Spark Application运行时都有一个Driver Program,属于一个JVM Process进程,可以设置内存Memory和CPU Core核数。
在这里插入图片描述

Executor 参数配置

每个Spark Application运行时,需要启动Executor运行任务Task,需要指定Executor个数及每个Executor资源信息(内存Memory和CPU Core核数)
在这里插入图片描述

官方案例

Spark 官方提供一些针对不同模式运行Spark Application如何设置参数提供案例,具体如下:

# Run application locally on 8 cores
./bin/spark-submit \
 --class org.apache.spark.examples.SparkPi \
 --master local[8] \
 /path/to/examples.jar \
 100
# Run on a Spark standalone cluster in client deploy mode
./bin/spark-submit \
 --class org.apache.spark.examples.SparkPi \
 --master spark://207.184.161.138:7077 \
 --executor-memory 20G \
 --total-executor-cores 100 \
 /path/to/examples.jar \
 1000
# Run on a Spark standalone cluster in cluster deploy mode with supervise
./bin/spark-submit \
 --class org.apache.spark.examples.SparkPi \
 --master spark://207.184.161.138:7077 \
 --deploy-mode cluster \
 --supervise \
 --executor-memory 20G \
 --total-executor-cores 100 \
 /path/to/examples.jar \
 1000
# Run on a YARN cluster
export HADOOP_CONF_DIR=XXX
./bin/spark-submit \
 --class org.apache.spark.examples.SparkPi \
 --master yarn \
 --deploy-mode cluster \ # can be client for client mode
 --executor-memory 20G \
 --num-executors 50 \
  /path/to/examples.jar \
 1000
# Run a Python application on a Spark standalone cluster
./bin/spark-submit \
 --master spark://207.184.161.138:7077 \
 examples/src/main/python/pi.py \
 1000
# Run on a Mesos cluster in cluster deploy mode with supervise
./bin/spark-submit \
--class org.apache.spark.examples.SparkPi \
--master mesos://207.184.161.138:7077 \
 --deploy-mode cluster \
 --supervise \
 --executor-memory 20G \
 --total-executor-cores 100 \
 http://path/to/examples.jar \
 1000
# Run on a Kubernetes cluster in cluster deploy mode
./bin/spark-submit \
 --class org.apache.spark.examples.SparkPi \
 --master k8s://xx.yy.zz.ww:443 \
 --deploy-mode cluster \
 --executor-memory 20G \
 --num-executors 50 \
 http://path/to/examples.jar \
 1000

5.6 应用打包运行

将开发测试完成的WordCount程序打成jar保存,使用【spark-submit】分别提交运行在本地
模式LocalMode和集群模式Standalone集群。先修改代码,通过master设置运行模式及传递处理数据路径,代码【SparkSubmit.scala】如下:

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}
/**
* 基于Scala语言使用SparkCore编程实现词频统计:WordCount
* 从HDFS上读取数据,统计WordCount,将结果保存到HDFS上
*/
object SparkSubmit {
	def main(args: Array[String]): Unit = {
		// TODO: 为了程序健壮性,判断是否传递参数
		if(args.length != 2){
			println("Usage: SparkSubmit <input> <output>............")
			System.exit(1)
		}
		// 创建SparkConf对象,设置应用的配置信息,比如应用名称和应用运行模式
		val sparkConf: SparkConf = new SparkConf()
		//.setMaster("local[2]")
		.setAppName("SparkWordCount")
		// TODO: 构建SparkContext上下文实例对象,读取数据和调度Job执行
		val sc: SparkContext = new SparkContext(sparkConf)
		// 第一步、读取数据
		// 封装到RDD集合,认为列表List
		val inputRDD: RDD[String] = sc.textFile(args(0))
		// 第二步、处理数据
		// 调用RDD中函数,认为调用列表中的函数
		// a. 每行数据分割为单词
		val wordsRDD = inputRDD.flatMap(line => line.split("\\s+"))
		// b. 转换为二元组,表示每个单词出现一次
		val tuplesRDD: RDD[(String, Int)] = wordsRDD.map(word => (word, 1))
		// c. 按照Key分组聚合
		val wordCountsRDD: RDD[(String, Int)] = tuplesRDD.reduceByKey((tmp, item) => tmp + item)
		// 第三步、输出数据
		// 保存到为存储系统,比如HDFS
		wordCountsRDD.saveAsTextFile(s"${args(1)}-${System.nanoTime()}")
		// TODO:应用程序运行接收,关闭资源
		sc.stop()
	}
}

打成jar包【spark-chapter01_2.11-1.0.0.jar】,如下图所示
在这里插入图片描述
上传jar包至HDFS文件系统目录【/spark/apps/】下,方便提交运行时任何地方都可读取jar包。

## 创建HDFS目录
hdfs dfs -mkdir -p /spark/apps/
## 上传jar包
hdfs dfs -put /export/server/spark/spark-chapter01_2.11-1.0.0.jar /spark/apps/
  • 1)、本地模式LocalMode提交运行
SPARK_HOME=/export/server/spark
${SPARK_HOME}/bin/spark-submit \
--master local[2] \
--class cn.itcast.spark.submit.SparkSubmit \
hdfs://node1.itcast.cn:8020/spark/apps/spark-chapter01_2.11-1.0.0.jar \
/datas/wordcount.data /datas/swc-output
  • 2)、Standalone集群提交运行
SPARK_HOME=/export/server/spark
${SPARK_HOME}/bin/spark-submit \
--master spark://node1.itcast.cn:7077,node2.itcast.cn:7077 \
--class cn.itcast.spark.submit.SparkSubmit \
--driver-memory 512m \
--executor-memory 512m \
--num-executors 1 \
--total-executor-cores 2 \
hdfs://node1.itcast.cn:8020/spark/apps/spark-chapter01_2.11-1.0.0.jar \
/datas/wordcount.data /datas/swc-output

运行完成以后,从历史服务器HistoryServer中查看应用,切换到【Executors】Tab页面:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
手把手视频详细讲解项目开发全过程,需要的小伙伴自行百度网盘下载,链接见附件,永久有效。 课程简介 知识点介绍、代码演示、逻辑分析、灵活举例、使用图形的方式详细演示代码的流程和细节、整合企业级实战案例,全面讲解并突出重点,让学习也变成一种快乐。 课程亮点 1,知识体系完备,阶段学习者都能学有所获。 2,综合各种方式演示代码、分析逻辑,生动形象,化繁为简,讲解通俗易懂。 3,结合工作实践及分析应用,培养解决实际问题的能力。 4,使用综合案例来加强重点知识,用切实的应用场景提升编程能力,充分巩固各个知识点的应用。 5,整个课程的讲解思路是先提出问题,然后分析问题,并编程解决解题。 适用人群 1、对大数据感兴趣的在校生及应届毕业生。 2、对目前职业有进一步提升要求,希望从事大数据行业高薪工作的在职人员。 3、对大数据行业感兴趣的相关人员。 课程内容 第一章、Spark 基础环境 1.课程安排说明 2.Spark 框架概述 3.快速入门 4.Standalone集群及HA 5.Spark 应用开发入门 6.Spark 应用提交 7.Spark on YARN 8.应用部署模式DeployMode 第二章、SparkCore 模块 1.RDD 概念及特性 2.RDD 创建 3.RDD 函数及使用 4.RDD 持久化 5.案例:SogouQ日志分析 6.RDD Checkpoint 7.外部数据源(HBase和MySQL) 8.广播变量和累加器 9.Spark 内核调度 10.Spark 并行度 第三章、SparkSQL 模块 1.快速入门:词频统计 2.SparkSQL 概述 3.DataFrame 4.RDD与DataFrame转换 5.数据分析SQL和DSL 6.案例:电影评分数据分析 7.DataSet 8.外部数据源Exeternal DataSource 9.集成Hive 10.自定义函数UDF 11.分布式SQL引擎(spakr-sql和Spark ThriftServer) 12.Catalyst 优化器 第四章、离线综合实战 1.综合实战概述(需求、调研、业务) 2.环境搭建(大数据环境应用开发环境) 3.项目初始化(工具类和属性文件) 4.广告数据ETL 5.Spark 分布式缓存 6.业务报表分析 7.应用执行部署 8.Oozie和Hue集成调度Spark 应用 第五章、SparkStreaming 模块 1.Streaming流式应用概述 2.Streaming 计算模式 3.SparkStreaming计算思路 4.入门案例 5.SparkStreaming工作原理 6.DStream及函数 7.集成Kafka 8.案例:百度搜索风云榜(实时ELT、窗口Window和状态State) 9.SparkStreaming Checkpoint 10.消费Kafka偏移量管理 第六章、StructuredStreaming模块 1.StructuredStreaming 概述(核心设计和编程模型) 2.入门案例:WordCount 3.输入源InputSources 4.Streaming Query 设置 5.输出终端OutputSink 6.集成Kafka(Source和Sink) 7.案例:物联网设备数据分析 8.事件时间窗口分析 9.Streaming Deduplication数据去重 10.Continues Processing连续流处理 第七章、实时综合实战 1.综合实战概述(需求、环境搭建和项目初始化) 2.模拟交易订单数据 3.数据实时ETL存储Kafka 4.实时应用停止 5.实时增量存储(存储HBase和Elasticsearch) 6.实时订单报表(Kafka-StructuredStreaming-Redis) 7.实时应用性能调优(数据本地性、反压机制、动态资源和日志管理)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

csdnGuoYuying

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值