利用idea对spark程序进行远程提交和调试

利用idea对spark程序进行远程提交和调试

本文以WordCount程序来实现idea对spark程序进行远程提交和调试
环境
- 利用虚拟机搭建拥有3台主机的spark集群
spark1:192.168.6.137
spark2:192.168.6.138
spark3:192.168.6.139
- idea-IU-2016.3.7

前提是集群和调试的主机在同一个网段内。

一、利用idea对spark程序进行远程提交

WordCount scala程序

/**
  * Created by cuiyufei on 2018/2/13.
  */
import org.apache.spark.SparkContext
import org.apache.spark.SparkContext._
import org.apache.spark.SparkConf

object WordCount {
  private val master = "spark://spark1:7077"
  private val remote_file = "hdfs://spark1:9000/user/spark/data/spark.txt"
  def main(args: Array[String]) {
    val conf = new SparkConf()
      .setAppName("WordCount")
      .setMaster(master)
      .set("spark.executor.memory", "512m")
      .setJars(List("D:\\JetBrains\\workspace\\WordCount\\out\\artifacts\\WordCount_jar\\WordCount.jar"))

    val sc = new SparkContext(conf)
    val textFile = sc.textFile(remote_file)
    val wordCount = textFile.flatMap(line => line.split(" ")).map(word => (word, 1)).reduceByKey((a, b) => a + b)
    wordCount.foreach(println)
  }
}

pom.xml文件


<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.0modelVersion>

    <groupId>WODASgroupId>
    <artifactId>WordCountartifactId>
    <version>1.0-SNAPSHOTversion>
    <properties>
        <spark.version>2.1.0spark.version>
        <scala.version>2.11scala.version>
    properties>
    <repositories>
        <repository>
            <id>nexus-aliyunid>
            <name>Nexus aliyunname>
            <url>http://maven.aliyun.com/nexus/content/groups/publicurl>
        repository>
    repositories>

    <dependencies>
        <dependency>
            <groupId>org.apache.sparkgroupId>
            <artifactId>spark-core_${scala.version}artifactId>
            <version>${spark.version}version>
        dependency>
        <dependency>
            <groupId>org.apache.sparkgroupId>
            <artifactId>spark-streaming_${scala.version}artifactId>
            <version>${spark.version}version>
        dependency>
        <dependency>
            <groupId>org.apache.sparkgroupId>
            <artifactId>spark-sql_${scala.version}artifactId>
            <version>${spark.version}version>
        dependency>
        <dependency>
            <groupId>org.apache.sparkgroupId>
            <artifactId>spark-hive_${scala.version}artifactId>
            <version>${spark.version}version>
        dependency>
        <dependency>
            <groupId>org.apache.sparkgroupId>
            <artifactId>spark-mllib_${scala.version}artifactId>
            <version>${spark.version}version>
        dependency>

    dependencies>

    <build>
        <plugins>

            <plugin>
                <groupId>org.scala-toolsgroupId>
                <artifactId>maven-scala-pluginartifactId>
                <version>2.15.2version>
                <executions>
                    <execution>
                        <goals>
                            <goal>compilegoal>
                            <goal>testCompilegoal>
                        goals>
                    execution>
                executions>
            plugin>

            <plugin>
                <artifactId>maven-compiler-pluginartifactId>
                <version>3.6.0version>
                <configuration>
                    <source>1.8source>
                    <target>1.8target>
                configuration>
            plugin>

            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-surefire-pluginartifactId>
                <version>2.19version>
                <configuration>
                    <skip>trueskip>
                configuration>
            plugin>

        plugins>
    build>

project>

进行远程提交,注意两点
- setMaster(master):master变量必须为远程集群
- setJars(List(“D:\JetBrains\workspace\WordCount\out\artifacts\WordCount_jar\WordCount.jar”)):设置本地jar的目录

设置好后,点击运行即可

二、对程序进行远程调试

1.首先,在集群配置文件sparkk-env.sh中加入一下代码

export SPARK_SUBMIT_OPTS="-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005"
这里对上面的几个参数进行说明:
-Xdebug 启用调试特性
-Xrunjdwp 启用JDWP实现,包含若干子选项:
transport=dt_socket JPDA front-end和back-end之间的传输方法。dt_socket表示使用套接字传输。
address=8888 JVM在8888端口上监听请求,这个设定为一个不冲突的端口即可。
server=y y表示启动的JVM是被调试者。如果为n,则表示启动的JVM是调试器。
suspend=y y表示启动的JVM会暂停等待,直到调试器连接上才继续执行。suspend=n,则JVM不会暂停等待。

2.scala代码和远程提交的代码一样
3.idea的设置
对运行进行配置
利用idea对spark程序进行远程提交和调试_第1张图片
添加远程设置
利用idea对spark程序进行远程提交和调试_第2张图片
根据spark集群中spark-env.sh的SPARK_SUBMIT_OPTS的变量,对远程执行进行配置
利用idea对spark程序进行远程提交和调试_第3张图片
配置完成后,设置断点,在scala程序右键debug即可
利用idea对spark程序进行远程提交和调试_第4张图片
利用idea对spark程序进行远程提交和调试_第5张图片

你可能感兴趣的:(spark从入门到放弃)