管理 Apache Spark 的 Java 和 Scala 依附元件

Spark 應用程式通常依附於第三方 Java 或 Scala 程式庫。向 Managed Service for Apache Spark 叢集提交 Spark 工作時,建議您採用下列方法納入這些依附元件:

  1. 使用 gcloud dataproc jobs submit 指令從本機電腦提交工作時,請使用 --properties spark.jars.packages=[DEPENDENCIES] 旗標。

    範例:

    gcloud dataproc jobs submit spark \
        --cluster=my-cluster \
        --region=region \
        --properties=spark.jars.packages='com.google.cloud:google-cloud-translate:1.35.0,org.apache.bahir:spark-streaming-pubsub_2.11:2.2.0'
    

  2. 直接在叢集上提交工作時,請使用 spark-submit 指令和 --packages=[DEPENDENCIES] 參數。

    範例:

    spark-submit --packages='com.google.cloud:google-cloud-translate:1.35.0,org.apache.bahir:spark-streaming-pubsub_2.11:2.2.0'
    

  3. 加入自訂 JAR 檔案或套件時:

    • 工作專屬依附元件:提交工作時,請使用 --jars 旗標、 Cloud de Confiance 控制台「Jar files」(Jar 檔案) 欄位或 API jarFileUris 欄位,指定 JAR 的 Cloud Storage 路徑。
    • 叢集層級依附元件:在建立叢集時使用 spark:spark.jars 叢集屬性 (--properties="spark:spark.jars=gs://<var>your-bucket</var>/<var>your-jarfile</var>.jar")。

避免依附元件衝突

如果 Spark 應用程式依附元件與 Hadoop 的依附元件衝突,上述方法可能會失敗。之所以會發生這種衝突,是因為 Hadoop 會將其依附元件插入應用程式的類別路徑,因此其依附元件的優先順序高於應用程式的依附元件。發生衝突時,可能會產生 NoSuchMethodError 或其他錯誤。

範例:
Guava 是 Google 的 Java 核心程式庫,許多程式庫和架構 (包括 Hadoop) 都會使用。如果工作或其依附元件需要比 Hadoop 使用的版本更新的 Guava 版本,則可能會發生依附元件衝突。

Hadoop 3.0 版解決了

,但如果應用程式依賴較早的 Hadoop 版本,則需要採取下列兩部分解決方法,避免可能發生的依附元件衝突。

  1. 建立單一 JAR,其中包含應用程式的套件和所有依附元件。
  2. 將 uber JAR 中衝突的依附元件套件重新定位,避免其路徑名稱與 Hadoop 的依附元件套件衝突。請使用外掛程式 (請見下方說明),在封裝過程中自動執行這項重新定位作業 (又稱為「遮蔽」),而非修改程式碼。

使用 Maven 建立 shaded uber JAR

Maven 是用於建構 Java 應用程式的套件管理工具。Maven scala 外掛程式可用於建構以 Scala 編寫的應用程式,這是 Spark 應用程式使用的語言。Maven shade

外掛程式可用來建立 shaded JAR。

以下是 pom.xml 設定檔範例,可遮蔽位於 com.google.common 套件中的 Guava 程式庫。這項設定會指示 Maven 將 com.google.common 套件重新命名為 repackaged.com.google.common,並更新原始套件中所有類別的參照。

<?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>

  <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <groupId><!-- YOUR_GROUP_ID --></groupId>
  <artifactId><!-- YOUR_ARTIFACT_ID --></artifactId>
  <version><!-- YOUR_PACKAGE_VERSION --></version>

  <dependencies>

    <dependency>
      <groupId>org.apache.spark</groupId>
      <artifactId>spark-sql_2.11</artifactId>
      <version><!-- YOUR_SPARK_VERSION --></version>
      <scope>provided</scope>
    </dependency>

    <!-- YOUR_DEPENDENCIES -->

  </dependencies>

  <build>
    <plugins>

      <plugin>
        <groupId>net.alchim31.maven</groupId>
        <artifactId>scala-maven-plugin</artifactId>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>
              <goal>testCompile</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <scalaVersion><!-- YOUR_SCALA_VERSION --></scalaVersion>
        </configuration>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <mainClass><!-- YOUR_APPLICATION_MAIN_CLASS --></mainClass>
                </transformer>
                <!-- This is needed if you have dependencies that use Service Loader. Most Google Cloud client libraries do. -->
                <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
              </transformers>
              <filters>
                <filter>
                  <artifact>*:*</artifact>
                  <excludes>
                    <exclude>META-INF/maven/**</exclude>
                    <exclude>META-INF/*.SF</exclude>
                    <exclude>META-INF/*.DSA</exclude>
                    <exclude>META-INF/*.RSA</exclude>
                  </excludes>
                </filter>
              </filters>
              <relocations>
                <relocation>
                  <pattern>com</pattern>
                  <shadedPattern>repackaged.com.google.common</shadedPattern>
                  <includes>
                    <include>com.google.common.**</include>
                  </includes>
                </relocation>
              </relocations>
            </configuration>
          </execution>
        </executions>
      </plugin>

    </plugins>
  </build>

</project>

若要執行建構:

mvn package

pom.xml 相關注意事項:

  • ManifestResourceTransformer 會處理 Uber JAR 資訊清單檔案 (MANIFEST.MF) 中的屬性。資訊清單也可以指定應用程式的進入點。
  • Spark 的範圍為 provided,因為 Spark 是安裝在 Managed Service for Apache Spark。
  • 指定安裝在 Managed Service for Apache Spark 叢集上的 Spark 版本 (請參閱 Managed Service for Apache Spark 版本清單)。如果應用程式需要的 Spark 版本與 Managed Service for Apache Spark 叢集上安裝的版本不同,您可以編寫初始化動作或建構自訂映像檔,安裝應用程式使用的 Spark 版本。
  • <filters> 項目會從依附元件的 META-INF 目錄中排除簽章檔案。如果沒有這個項目,可能會發生 java.lang.SecurityException: Invalid signature file digest for Manifest main attributes 執行階段例外狀況,因為簽章檔案在 Uber JAR 的內容中無效。
  • 您可能需要遮蔽多個程式庫。如要這麼做,請加入多個路徑。下一個範例會遮蔽 Guava 和 Protobuf 程式庫。
    <relocation>
      <pattern>com</pattern>
      <shadedPattern>repackaged.com</shadedPattern>
      <includes>
        <include>com.google.protobuf.**</include>
        <include>com.google.common.**</include>
      </includes>
    </relocation>

使用 SBT 建立 shaded uber JAR

SBT 是建構 Scala 應用程式的工具。如要使用 SBT 建立 shaded JAR,請將 sbt-assembly 新增至

外掛程式,請先在 project/ 目錄下建立名為 assembly.sbt 的檔案:

├── src/
└── build.sbt
└── project/
    └── assembly.sbt

... 然後在 assembly.sbt 中加入以下程式碼:

addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.6")

以下是 build.sbt 設定檔範例,可遮蔽位於 com.google.common package 的 Guava 程式庫:

lazy val commonSettings = Seq(
 organization := "YOUR_GROUP_ID",
 name := "YOUR_ARTIFACT_ID",
 version := "YOUR_PACKAGE_VERSION",
 scalaVersion := "YOUR_SCALA_VERSION",
)

lazy val shaded = (project in file("."))
 .settings(commonSettings)

mainClass in (Compile, packageBin) := Some("YOUR_APPLICATION_MAIN_CLASS")

libraryDependencies ++= Seq(
 "org.apache.spark" % "spark-sql_2.11" % "YOUR_SPARK_VERSION" % "provided",
 // YOUR_DEPENDENCIES
)

assemblyShadeRules in assembly := Seq(
  ShadeRule.rename("com.google.common.**" -> "repackaged.com.google.common.@1").inAll
)

若要執行建構:

sbt assembly

build.sbt 相關注意事項:

  • 上述範例中的遮蔽規則可能無法解決所有依附元件衝突,因為 SBT 會使用嚴格的衝突解決策略。因此,您可能需要提供更精細的規則,使用 MergeStrategy.first、last、concat、filterDistinctLines、rename 或 discard 策略明確合併特定類型的衝突檔案。詳情請參閱 sbt-assembly 的合併策略。
  • 您可能需要遮蔽多個程式庫。如要這麼做,請加入多個路徑。下一個範例會遮蔽 Guava 和 Protobuf 程式庫。
    assemblyShadeRules in assembly := Seq(
      ShadeRule.rename("com.google.common.**" -> "repackaged.com.google.common.@1").inAll,
      ShadeRule.rename("com.google.protobuf.**" -> "repackaged.com.google.protobuf.@1").inAll
    )

將 Uber JAR 提交至 Managed Service for Apache Spark

建立包含 Spark 應用程式及其依附元件的 shaded uber JAR 後,請將 JAR 上傳至 Cloud Storage,並提交工作至 Managed Service for Apache Spark (使用 Google Cloud CLI 中的 --jars 旗標、 Cloud de Confiance 控制台中的「Jar files」(Jar 檔案) 欄位、API jarFileUris 欄位,或將 shaded JAR 指定為主要應用程式 JAR)。

後續步驟