如何使用Java調(diào)用Spark集群
我搭建的Spark集群的版本是2.4.4。

在網(wǎng)上找的maven依賴,鏈接忘記保存了。。。。
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<hadoop.version>2.6.0-cdh5.14.2</hadoop.version>
<hive.version>1.1.0-cdh5.14.2</hive.version>
<hbase.version>1.2.0-cdh5.14.2</hbase.version>
<scala.version>2.11.8</scala.version>
<spark.version>2.4.4</spark.version>
</properties>
<repositories>
<repository>
<id>cloudera</id>
<url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
</repository>
</repositories>
<dependencies>
<!--scala-->
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.11.8</version>
</dependency>
<!-- spark-core -->
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.11</artifactId>
<version>${spark.version}</version>
</dependency>
<!-- spark-sql -->
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_2.11</artifactId>
<version>${spark.version}</version>
</dependency>
<!-- spark-hive -->
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-hive_2.11</artifactId>
<version>2.4.4</version>
</dependency>
<!-- spark-graphx -->
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-graphx_2.11</artifactId>
<version>${spark.version}</version>
</dependency>
<!-- hadoop -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>${hadoop.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>${hadoop.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>${hadoop.version}</version>
</dependency>
<!-- log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<!-- kafka-clients -->
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>0.11.0.2</version>
</dependency>
<!-- mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.31</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.0.1.RELEASE</version>
<configuration>
<mainClass>gdut.spark.SparkInit</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>Java客戶端連接示例:
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.SparkConf;
import java.util.Arrays;
import java.util.List;
public class SparkInit {
public static void main(String[] args) {
try {
SparkConf conf = new SparkConf().setAppName("liufeifei").setMaster("spark://x.x.x.x:30010");
conf.set("spark.executor.cores","1");
conf.set("spark.executor.memory", "1024m");
JavaSparkContext sc = new JavaSparkContext(conf);
List<Integer> data = Arrays.asList(1, 2, 3, 4, 5);
JavaRDD<Integer> distData = sc.parallelize(data);
System.out.println("result is " + distData.count());
} catch (Exception e) {
e.printStackTrace();
}
}
}遇到問題:
(1)spark集群中,worker節(jié)點(diǎn)提示:Failed to send RPC
master pod的spark-shell執(zhí)行collect方法,日志輸出如下:

worker pod輸出如下:

worker節(jié)點(diǎn)無法創(chuàng)建Executor,在worker節(jié)點(diǎn)的安裝目錄下有個(gè)work目錄,有每次創(chuàng)建Executor的日志。查看是worker節(jié)點(diǎn)與master節(jié)點(diǎn)無法通信。但是worker節(jié)點(diǎn)有向master注冊,在master的UI界面有顯示注冊的worker節(jié)點(diǎn)。在網(wǎng)上不經(jīng)意看到有人說可能是istio影響了,然后想起自己之前部署過istio。查看spark部署的命名空間確實(shí)是開啟istio注入。

換個(gè)沒有istio注入的命名空間創(chuàng)建spark集群。在master節(jié)點(diǎn)的spark-shell可以執(zhí)行collect方法,可以調(diào)度到worker節(jié)點(diǎn)的Executor。
(2)Caused by: java.net.UnknownHostException: XXX
無論在本地還是在虛擬機(jī)執(zhí)行上面的客戶端連接,都會提示UnknownHostException。這是因?yàn)樵趙orker容器的/etc/hosts找不到客戶端主機(jī)名稱和IP的映射關(guān)系。
解決辦法:使用 HostAliases 向 Pod /etc/hosts 文件添加條目
hostAliases:
- ip: "127.0.0.1"
hostnames:
- "foo.local"
- "bar.local"
- ip: "10.1.2.3"
hostnames:
- "foo.remote"
- "bar.remote"我在yaml文件添加了hostAliases之后,提示主機(jī)名不符合規(guī)定,然后修改了自己虛擬機(jī)上的主機(jī)名。
修改主機(jī)名后遇到:java.net.UnknownHostException:Name or Service not known
修改了/etc/hosts文件可以解決。
因?yàn)閟park集群是部署在一臺虛擬機(jī)上,本地不能和虛擬機(jī)通信,所以要把spring boot項(xiàng)目打包成jar在虛擬機(jī)上執(zhí)行。
Main方法輸出:

worker日志輸出(k8s容器和宿主機(jī)時(shí)間相差了8個(gè)小時(shí)):

到此這篇關(guān)于使用Java調(diào)用Spark集群的文章就介紹到這了,更多相關(guān)Java Spark集群內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot+thymeleaf+layui的實(shí)現(xiàn)示例
本文主要介紹了springboot+thymeleaf+layui的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-12-12
SpringBoot整合canal實(shí)現(xiàn)數(shù)據(jù)緩存一致性解決方案
canal主要用途是基于?MySQL?數(shù)據(jù)庫增量日志解析,提供增量數(shù)據(jù)訂閱和消費(fèi),canal是借助于MySQL主從復(fù)制原理實(shí)現(xiàn),本文將給大家介紹SpringBoot整合canal實(shí)現(xiàn)數(shù)據(jù)緩存一致性解決方案,需要的朋友可以參考下2024-03-03
SpringSecurity6.4中一次性令牌登錄(One-Time Token Login)實(shí)現(xiàn)
Spring Security為一次性令牌認(rèn)證提供了支持,本文就來介紹一下SpringSecurity6.4中一次性令牌登錄(One-Time Token Login)實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2025-03-03
Java模擬HTTP Get Post請求實(shí)現(xiàn)論壇自動回帖功能
這篇文章主要介紹了Java模擬HTTP Get Post請求實(shí)現(xiàn)論壇自動回帖功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
SpringBoot讀取資源目錄中JSON文件的方法實(shí)例
最近做項(xiàng)目遇到需要將json類型的配置文件引用到項(xiàng)目中,已經(jīng)將讀取json文件的方法封裝成工具類,下面這篇文章主要給大家介紹了關(guān)于SpringBoot讀取資源目錄中JSON文件的相關(guān)資料,需要的朋友可以參考下2023-04-04
Layui前后臺交互數(shù)據(jù)獲取java實(shí)例
下面小編就為大家分享一篇Layui前后臺交互數(shù)據(jù)獲取java實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
解決spring @ControllerAdvice處理異常無法正確匹配自定義異常
這篇文章主要介紹了解決spring @ControllerAdvice處理異常無法正確匹配自定義異常的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
Spring5+SpringMvc+Hibernate5整合的實(shí)現(xiàn)
這篇文章主要介紹了Spring5+SpringMvc+Hibernate5整合的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06

