首页 文章

httpclient版本与Apache Spark之间的冲突

提问于
浏览
5

我正在使用Apache Spark开发Java应用程序 . 我用这个版本:

<dependency>
    <groupId>org.apache.spark</groupId>
    <artifactId>spark-core_2.10</artifactId>
    <version>1.2.2</version>
</dependency>

在我的代码中,有一个过渡依赖:

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.2</version>
</dependency>

我将我的应用程序打包到一个JAR文件中 . 当使用 spark-submit 在EC2实例上部署它时,我收到此错误 .

Caused by: java.lang.NoSuchFieldError: INSTANCE
    at org.apache.http.conn.ssl.SSLConnectionSocketFactory.<clinit>(SSLConnectionSocketFactory.java:144)
    at com.amazonaws.http.apache.client.impl.ApacheConnectionManagerFactory.getPreferredSocketFactory(ApacheConnectionManagerFactory.java:87)
    at com.amazonaws.http.apache.client.impl.ApacheConnectionManagerFactory.create(ApacheConnectionManagerFactory.java:65)
    at com.amazonaws.http.apache.client.impl.ApacheConnectionManagerFactory.create(ApacheConnectionManagerFactory.java:58)
    at com.amazonaws.http.apache.client.impl.ApacheHttpClientFactory.create(ApacheHttpClientFactory.java:50)
    at com.amazonaws.http.apache.client.impl.ApacheHttpClientFactory.create(ApacheHttpClientFactory.java:38)

此错误清楚地表明 SparkSubmit 已加载相同Apache httpclient库的旧版本,因此发生此冲突 .

解决这个问题的好方法是什么?

出于某种原因,我不能在我的Java代码上升级Spark . 但是,我可以轻松地使用EC2群集 . 是否可以在具有更高版本的1.6.1版本的集群上部署我的Java代码?

1 回答

  • 7

    正如你在帖子中所说,Spark正在加载旧版本的 httpclient . 解决方案是使用Maven的relocation工具来生成一个整洁的无冲突项目 .

    以下是如何在 pom.xml 文件中使用它的示例:

    <project>
      <!-- Your project definition here, with the groupId, artifactId, and it's dependencies --> 
      <build>
        <plugins>
          <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>
                  <relocations>
                    <relocation>
                      <pattern>org.apache.http.client</pattern>
                      <shadedPattern>shaded.org.apache.http.client</shadedPattern>
                    </relocation>
                  </relocations>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    
    </project>
    

    这会将所有文件从 org.apache.http.client 移动到 shaded.org.apache.http.client ,从而解决冲突 .


    Original post :

    如果这只是传递依赖的问题,您可以将其添加到 spark-core 依赖项中以排除Spark使用的HttpClient:

    <dependency>
        <groupId>org.apache.spark</groupId>
        <artifactId>spark-core_2.10</artifactId>
        <version>1.2.2</version>
        <scope>provided</scope>
        <exclusions>
            <exclusion>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpclient</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    

    我还在您的依赖项中添加了 scope 作为 provided ,因为它将由您的群集提供 .

    然而,这可能会破坏Spark 's internal behaviour. If you still get an error after doing this, you could try using Maven' s relocation设施,这应该产生一个整洁的无冲突项目 .

    关于你可以't upgrade Spark'版本的事实,你是否使用了mvnrepository的this dependency声明?

    Spark向后兼容,在具有更高版本的群集上部署作业应该没有任何问题 .

相关问题