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、运行
相关推荐
吕永强23 分钟前
基于SpringBoot+Vue小区报修系统的设计与实现(源码+论文+部署)
spring boot·毕业设计·毕业论文·报修系统·小区报修
程序员老邢2 小时前
【产品底稿 05】商助慧 V1.1 里程碑:RAG 文章仿写模块全链路实现
java·spring boot·程序人生·ai·milvus
消失的旧时光-19432 小时前
Spring Boot 实战(三):Service 分层 + 统一返回 + 异常处理(工程级写法)
java·spring boot·接口·解耦
霸道流氓气质2 小时前
SpringBoot中集成LangChain4j实现集成阿里百炼平台进行AI对话记忆功能和对话隔离功能
java·人工智能·spring boot·langchain4j
霸道流氓气质3 小时前
SpringBoot中集成LangChain4j实现集成阿里百炼平台进行AI快速对话
人工智能·spring boot·后端·langchain4j
武子康3 小时前
大数据-270 Spark MLib-机器学习库快速入门(分类/回归/聚类/推荐)
大数据·后端·spark
Flittly4 小时前
【SpringSecurity新手村系列】(1)初识安全框架
java·spring boot·安全·spring·安全架构
码界奇点4 小时前
基于Spring Boot的插件化微服务热更新系统设计与实现
spring boot·后端·微服务·架构·毕业设计·源代码管理
码界奇点4 小时前
基于Spring Boot与Vue的教务管理系统设计与实现
vue.js·spring boot·后端·java-ee·毕业设计·源代码管理