CDH6.3.2 Spark-submit 提交作业到 yarn

4 篇文章 0 订阅
3 篇文章 0 订阅

一、在idea中 写一个 wordcount 案例:

这里一定不能设置 .setMaster(“local[*]”)
这里一定不能设置 .setMaster(“local[*]”)
这里一定不能设置 .setMaster(“local[*]”)

重要的事情要说三遍!!!!否则 在启动yarn 模式的时候会报错!!!
val conf = new SparkConf().setMaster(“local[*]”).setAppName(“WordCount”)

package submit_test
import org.apache.spark.{SparkConf, SparkContext, SparkFiles}
object WordCount {
  def main(args: Array[String]): Unit = {
    //scala代码实现spark入口 (这里没有指明是什么模式,在提交时指明)
    //yarn这里一定不能设置.setMaster("local[*]")
    //val conf = new SparkConf().setMaster("local[*]").setAppName("WordCount")
    val conf = new SparkConf().setAppName("WordCount")
   
    val sc = new SparkContext(conf)
    //加载hdfs文件到SparkRDD
    val lines = sc.textFile(args(0))
    //WordCount实现
    val result = lines.flatMap(x=>x.split(" ")).map((_,1)).reduceByKey(_+_)
    //结果保存在hdfs
    result.saveAsTextFile(args(1))
    sc.stop()
  }
}

二、使用idea打包:在这里插入图片描述

在这里插入图片描述
2.1 默认都点ok (有的依赖不需要 ,可以自选)
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
2.2build 和 rebuild 都可以:
在这里插入图片描述
2.3 将spark-submit.jar 上传到 虚拟机上:
在这里插入图片描述

三、虚拟机上的操作

[root@cdh01 sparktest]# pwd
/opt/sparktest
[root@cdh01 sparktest]# ll
总用量 178308
-rw-r--r-- 1 root root 182583469 323 20:51 spark-submit.jar
[root@cdh01 sparktest]# zip -d spark-submit.jar META-INF/*.RSA META-INF/*.DSA META-INF/*.SF
	
	zip warning: name not matched: META-INF/*.RSA
deleting: META-INF/DUMMY.SF
deleting: META-INF/DUMMY.DSA

[root@cdh01 sparktest]# 

四、在hdfs 上创建一个 文件夹 sparktest

	在sparktest文件夹下创建一个 word.txt文件
	
	文件内容如下:
hello world
hello print

在这里插入图片描述

五、提交任务 :

[root@cdh01 sparktest]# spark-submit --master yarn --deploy-mode cluster --driver-memory 500m --executor-memory 500m --executor-cores 2 --class submit_test.WordCount /opt/sparktest/spark-submit.jar hdfs://192.168.2.112:8020/sparktest/word.txt hdfs://192.168.2.112:8020/sparktest/wcOut


其中 这个输入、输出路径 的ip 是高可用活跃的那天机器的ip
hdfs://192.168.2.112:8020/sparktest/word.txt
hdfs://192.168.2.112:8020/sparktest/wcOut

输出的文件夹如下:
在这里插入图片描述
wordcount 的结果:
在这里插入图片描述

备注:

我用这种打包方式 spark-submit 也可以:
在这里插入图片描述

[root@cdh01 sparktest]# ll
总用量 366372
-rw-r--r-- 1 root root 194196468 323 21:13 spark-submit-1.0-SNAPSHOT.jar
-rw-r--r-- 1 root root 180963015 323 20:53 spark-submit.jar

[root@cdh01 sparktest]# spark-submit --master yarn --deploy-mode cluster --driver-memory 500m --executor-memory 500m --executor-cores 2 --class submit_test.WordCount /opt/sparktest/spark-submit-1.0-SNAPSHOT.jar hdfs://192.168.2.112:8020/sparktest/word.txt hdfs://192.168.2.112:8020/sparktest/wcOut333

在这里插入图片描述
pom.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.atguidian.sparksubmit</groupId>
    <artifactId>spark-submit</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <encoding>UTF-8</encoding>
        <spark.version>2.4.0</spark.version>
        <scala.version>2.11</scala.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-core_${scala.version}</artifactId>
            <version>${spark.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-streaming_${scala.version}</artifactId>
            <version>${spark.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-sql_${scala.version}</artifactId>
            <version>${spark.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-hive_${scala.version}</artifactId>
            <version>${spark.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-mllib_${scala.version}</artifactId>
            <version>${spark.version}</version>
        </dependency>
    </dependencies>
    <build>
        <pluginManagement>
            <plugins>
                <!--  scala-maven-plugin:编译scala程序的Maven插件 -->
                <plugin>
                    <groupId>net.alchim31.maven</groupId>
                    <artifactId>scala-maven-plugin</artifactId>
                    <version>3.2.2</version>
                </plugin>
                <!--  maven-compiler-plugin:编译java程序的Maven插件 -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.6.1</version>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <!--  编译scala程序的Maven插件的一些配置参数 -->
            <plugin>
                <groupId>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>scala-compile-first</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>add-source</goal>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>scala-test-compile</id>
                        <phase>process-test-resources</phase>
                        <goals>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!--  编译java程序的Maven插件的一些配置参数 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!--  maven-shade-plugin:打jar包用的Mavne插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4.3</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <filters>
                                <filter>
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SF</exclude>
                                        <exclude>META-INF/*.DSA</exclude>
                                        <exclude>META-INF/*.RSA</exclude>
                                    </excludes>
                                </filter>
                            </filters>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值