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、运行
相关推荐
༄沐࿆风࿆࿆16 小时前
Spring Boot 3 + Vue 3 全栈外卖系统:RabbitMQ异步解耦、MySQL/Neo4j双引擎推建
vue.js·spring boot·java-rabbitmq
星空1 天前
Springboot复习
java·spring boot·spring
老郑聊AI业财智造1 天前
TOGAF、DDD与微服务:软件架构的多维透视
spring boot·spring cloud·微服务·金融·架构·软件工程·制造
wno7041 天前
Spring Boot JdbcTemplate配置Druid多数据源
java·spring boot·后端
wear工程师1 天前
Spring Boot 服务挂了却没重启?先分清 systemd 的三次判断
spring boot
旺仔学长 哈哈2 天前
52486|高校专业评估不再靠表格:Spring Boot 本科专业质量评估管理系统实战
数据库·spring boot·毕业设计·教育管理
2601_963870212 天前
【计算机毕业设计】基于Spring Boot+Vue的高考志愿填报系统的设计与实现
spring boot·课程设计·高考
weixin_BYSJ19872 天前
springboot小区物业管理小程序---附源码45555
java·javascript·spring boot·python·django·flask·php
RuoyiOffice2 天前
抓包还能看到密码?Vue3 + Spring Boot 3 接口 AES/RSA 加解密(@ApiEncrypt)全链路实战
spring boot·vue3·aes·rsa·接口安全·spring boot 3·apiencrypt
凤山老林2 天前
零停机数据库演进:Spring Boot 集成 Flyway 与平滑DDL变更策略
数据库·spring boot·后端