springboot远程链接spark

springboot远程链接spark

1、导入依赖
xml 复制代码
<!--        spark依赖-->
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-core_2.12</artifactId>
            <version>3.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-sql_2.12</artifactId>
            <version>3.2.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.spark/spark-mllib -->
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-mllib_2.12</artifactId>
            <version>3.2.2</version>
        </dependency>
2、配置spark信息
  • 建立一个配置文件,配置spark信息

    java 复制代码
    import org.apache.spark.SparkConf;
    import org.apache.spark.sql.SparkSession;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    //将文件交于spring管理
    @Configuration
    public class SparkConfig {
    
        //使用yml中的配置
        @Value("${spark.master}")
        private String sparkMaster;
    
        @Value("${spark.appName}")
        private String sparkAppName;
        @Bean
        public SparkConf sparkConf() {
            SparkConf conf = new SparkConf();
            conf.setMaster(sparkMaster);
            conf.setAppName(sparkAppName);
            return conf;
        }
    
        @Bean
        public SparkSession sparkSession() {
            return SparkSession.builder()
                    .config(sparkConf())
                    .getOrCreate();
        }
    }
3、controller和service
  • controller类

    java 复制代码
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import xyz.zzj.traffic_main_code.service.SparkService;
    
    @RestController
    @RequestMapping("/spark")
    public class SparkController {
    
        @Autowired
        private SparkService sparkService;
    
        @GetMapping("/run")
        public String runSparkJob() {
            sparkService.executeSparkJob();
            return "Spark job executed successfully!";
        }
    }
  • service

    java 复制代码
    import org.springframework.stereotype.Service;
    
    @Service
    public class SparkService {
    
        public void executeSparkJob() {
            System.out.println("Spark job started");
        }
    }
4、运行
相关推荐
CoderYanger42 分钟前
Java EE 进阶:8.MyBatis 操作数据库(入门)(第二弹)
java·数据库·spring boot·spring·面试·java-ee·mybatis
计算机毕设定制辅导-无忧学长11 小时前
《基于微信小程序的居家医疗陪护预约系统设计与实现》
java·spring boot·微信小程序·uniapp·居家医疗陪护预约系统
小郎君65817 小时前
注解式脱敏:用 Jackson 的 ContextualSerializer 做字段级策略(一)
spring boot·spring cloud
jktantan19 小时前
注解式脱敏:用 Jackson 的 ContextualSerializer 做字段级策略(一)
spring boot·后端·算法·spring cloud·系统架构
露天赏雪1 天前
第3篇:Sentinel熔断降级实战:大模型接口抖动时的服务兜底方案
java·开发语言·人工智能·spring boot·spring·servlet·sentinel
蜗牛8752 天前
为 Spring Boot 团队做一个 Elasticsearch 查询工作台:六种模式、语义补全与浏览器直连
spring boot
养军博客2 天前
Spring Boot 3.x + Spring Security 6.x 整合 JWT 实战:jjwt 与 java-jwt 两种工具库详解
java·spring boot·springsecurity
txxzjmzlh2 天前
SpringBoot 统⼀功能处理
java·spring boot·后端
阿里云大数据AI技术2 天前
EMR Serverless Ray:让 AI 推理融入数据处理
人工智能·spark
秋天的怀念_David3 天前
算法一千零一夜 · 第二十六夜 协议的母语
java·spring boot·算法