博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spark进阶之路-Spark提交Jar包执行
阅读量:7080 次
发布时间:2019-06-28

本文共 11947 字,大约阅读时间需要 39 分钟。

            Spark进阶之路-Spark提交Jar包执行

                                    作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

 

  在实际开发中,使用spark-submit提交jar包是很常见的方式,因为用spark-shell来开发项目是很苦难的(一般都用IDE),当我们开发程序完毕后,需要打成jar包。

 

一.通过jar包提交任务注意事项【工作中主要的工作方式】

  1>.需要通过spark-submit来提交;

  2>.必须使用“--class”指定你jar包的主类;

  3>.必须使用“--master”指定你访问的集群地址,如果你的jar包中已经配置了master,那么可以不指定master地址;

  4>.必须指定你jar包的具体路径(即:jar的参数)

  5>.我们在命令行输入“spark-submit”可以看到所有的你可以指定的参数,常用的参数就是以上几点。

[yinzhengjie@s101 ~]$ spark-submit Usage: spark-submit [options] 
[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, or 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 local 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. --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. Spark standalone with cluster deploy mode only: --driver-cores NUM Cores for driver (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: --driver-cores NUM Number of cores used by the driver, only in cluster mode (Default: 1). --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. [yinzhengjie@s101 ~]$
获取spark-submit过多帮助信息([yinzhengjie@s101 ~]$ spark-submit )

 

二.使用idea的Maven编写WordCount

1>.新建一个项目

2>.选择Maven类型

3>.指定项目的版本,点击下一步

 

4>.指定项目的路径

5>.添加框架支持

6>.选择Maven

 

7>.删除src目录

8>.添加二级Maven项目

9>.选择二级Maven类型

10>.输入二级Maven的名称

11>.指定二级Maven存放路径,默认即可。

 

12>.删除二级Maven的src目录并创建三级Maven

13>.点击下一步

 

14>.输入三级Maven的名称

15>.安装路径默认即可

16>.编辑三个Maven的pom文件内容

1 
2
5
4.0.0
6 7
cn.org.yinzhengjie
8
Spark
9
1.0-yinzhengjieCode
10
11
spark-core
12
13 14
15
pom
16 17
18
19
2.1.1
20
2.11.8
21
1.2.17
22
1.7.22
23
24 25
26
27
28
29
30
org.slf4j
31
jcl-over-slf4j
32
${slf4j.version}
33
34 35
36
org.slf4j
37
slf4j-api
38
${slf4j.version}
39
40 41
42
org.slf4j
43
slf4j-log4j12
44
${slf4j.version}
45
46 47
48
log4j
49
log4j
50
${log4j.version}
51
52
53 54
55
56
57
org.scala-lang
58
scala-library
59
${scala.verison}
60
61 62 63
64 65
66
67
68
69
70
org.apache.spark
71
spark-core_2.11
72
${spark.version}
73
74 75
76
77 78
79
80
81
82
83
84
org.apache.maven.plugins
85
maven-compiler-plugin
86
3.6.1
87
88
1,8 89
1.8
90
91
92 93
94
95
net.alchim31.maven
96
scala-maven-plugin
97
3.2.2
98
99
100
101
compile
102
testCompile
103
104
105
106
107
108 109
110
111
112
113
114
org.apache.maven.plugins
115
maven-assembly-plugin
116
3.0.0
117
118
119
make-assembly
120
package
121
122
single
123
124
125
126
127
128
129
130 131
Spark.xml 文件内容
1 
2
5
6
Spark
7
cn.org.yinzhengjie
8
1.0-yinzhengjieCode
9
10
4.0.0
11 12
spark-core
13
pom
14
15
spark-wordCount
16
17 18
19
20
org.apache.spark
21
spark-core_2.11
22
23
24
25 26
spark-core.xml 文件内容
1 
2
5
6
spark-core
7
cn.org.yinzhengjie
8
1.0-yinzhengjieCode
9
10
4.0.0
11 12
spark-wordCount
13 14 15
spark-wordCount.xml 文件内容

17>.编写代码

package cn.org.yinzhengjie.wordCountimport org.apache.spark.{SparkConf, SparkContext}object WordCount  extends Serializable {    def main(args: Array[String]): Unit = {        //设置访问hdfs的用户名,Windows操作系统默认是user=Administrator,因此这里需要指定具体的写入用户。        System.setProperty("HADOOP_USER_NAME", "yinzhengjie")        //声明配置        val sparkConf = new SparkConf()        //设置应用的名称        sparkConf.setAppName("WordCount")        //指定master,如果这里指定的话,在提交jar包时就可以不指定。        sparkConf.setMaster("spark://s101:7077,s105:7077")        //创建SparkContext        val sc = new SparkContext(sparkConf)        /**          * 以下代码就是业务逻辑          */        //读取hdfs的配置文件        val  file = sc.textFile("hdfs://s105:8020/yinzhengjie/data/README.md")        //按照空格切割文件,得到每个单词        val words = file.flatMap(_.split(" "))        //将words里面的每一个单词都标记为1        val wordOne = words.map((_,1))        //聚合操作,将相同的key进行聚合        val result = wordOne.reduceByKey(_+_)        //将计算的结果写入到hdfs中        result.saveAsTextFile("hdfs://s105:8020/yinzhengjie/data/wordCount2")        //关闭Spark链接        sc.stop()    }}

 

 

 

 

10>.

 

11>.

 

12>.

 

 

三.使用spark-shell来开发提交jar包执行

 

你可能感兴趣的文章
区块链技术在金融领域的七大武器
查看>>
天润融通牵手葵网新保险电销青睐云呼叫
查看>>
一起领略css3动画的强大
查看>>
证监会发布公募基金流动性风险管理新规,余额宝需计提71.5亿元风险准备金
查看>>
Centos 6 Zabbix : Install
查看>>
为ASP.NET MVC创建一个基于Unity的ControllerFactory
查看>>
让工作更高效,助力HR的5大云利器
查看>>
Java HashMap 核心源码解读
查看>>
需要更换DRaaS供应商的八个迹象
查看>>
网录科技CEO吕旭军:国内区块链产业时间紧迫感更强,很快会超美国
查看>>
创客总部陈荣根:做科技成果变现“第一站”
查看>>
《数字逻辑设计与计算机组成》一3.2 算术函数
查看>>
存储帮助Facebook 150万用户顺畅交友
查看>>
《OpenACC并行程序设计:性能优化实践指南》一 2.5 在多核系统中并行运行
查看>>
mybatis generator生成带有分页的Mybatis代码
查看>>
防火墙迁移:提高安全弹性与可用性的5种方法
查看>>
服务型存储市场的现状
查看>>
绿盟科技网络安全威胁周报2017.07 请关注OpenSSL拒绝服务漏洞CVE-2017-3733
查看>>
Qlik潘应麒:大数据时代下的现代分析学
查看>>
从技术上升到业务 混合云容灾提上日程
查看>>