广告位联系
返回顶部
分享到

springboot集成spark并使用spark-sql的介绍

java 来源:互联网 作者:秩名 发布时间:2022-02-20 09:14:20 人浏览
摘要

首先添加相关依赖: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

首先添加相关依赖:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <parent>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-parent</artifactId>

    <version>1.5.6.RELEASE</version>

    <relativePath />

  </parent>

  <groupId>com.cord</groupId>

  <artifactId>spark-example</artifactId>

  <version>1.0-SNAPSHOT</version>

  <name>spark-example</name>

  <!-- FIXME change it to the project's website -->

  <url>http://www.example.com</url>

  <properties>

    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

    <java.version>1.8</java.version>

    <scala.version>2.10.3</scala.version>

    <maven.compiler.source>1.8</maven.compiler.source>

    <maven.compiler.target>1.8</maven.compiler.target>

  </properties>

  <dependencies>

    <dependency>

      <groupId>org.springframework.boot</groupId>

      <artifactId>spring-boot-starter</artifactId>

      <version>1.5.6.RELEASE</version>

        <exclusions>

            <exclusion>

                <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-starter-logging</artifactId>

            </exclusion>

        </exclusions>

    </dependency>

    <dependency>

      <groupId>org.apache.spark</groupId>

      <artifactId>spark-core_2.10</artifactId>

      <version>1.6.1</version>

      <scope>provided</scope>

        <exclusions>

            <exclusion>

                <groupId>org.slf4j</groupId>

                <artifactId>slf4j-log4j12</artifactId>

            </exclusion>

            <exclusion>

                <groupId>log4j</groupId>

                <artifactId>log4j</artifactId>

            </exclusion>

        </exclusions>

    </dependency>

    <dependency>

      <groupId>org.apache.spark</groupId>

      <artifactId>spark-sql_2.10</artifactId>

      <version>1.6.1</version>

      <scope>provided</scope>

    </dependency>

    <dependency>

      <groupId>org.apache.spark</groupId>

      <artifactId>spark-hive_2.10</artifactId>

      <version>1.6.1</version>

      <scope>provided</scope>

    </dependency>

    <dependency>

      <groupId>org.scala-lang</groupId>

      <artifactId>scala-library</artifactId>

      <version>${scala.version}</version>

      <scope>provided</scope>

    </dependency>

    <!-- yarn-cluster模式 -->

    <dependency>

      <groupId>mysql</groupId>

      <artifactId>mysql-connector-java</artifactId>

      <version>5.1.22</version>

    </dependency>

  </dependencies>

    <build>

        <plugins>

            <plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-shade-plugin</artifactId>

                <dependencies>

                    <dependency>

                        <groupId>org.springframework.boot</groupId>

                        <artifactId>spring-boot-maven-plugin</artifactId>

                        <version>1.5.6.RELEASE</version>

                    </dependency>

                </dependencies>

                <configuration>

                    <keepDependenciesWithProvidedScope>false</keepDependenciesWithProvidedScope>

                    <createDependencyReducedPom>false</createDependencyReducedPom>

                    <filters>

                        <filter>

                            <artifact>*:*</artifact>

                            <excludes>

                                <exclude>META-INF/*.SF</exclude>

                                <exclude>META-INF/*.DSA</exclude>

                                <exclude>META-INF/*.RSA</exclude>

                            </excludes>

                        </filter>

                    </filters>

                    <transformers>

                        <transformer

                                implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">

                            <resource>META-INF/spring.handlers</resource>

                        </transformer>

                        <transformer

                                implementation="org.springframework.boot.maven.PropertiesMergingResourceTransformer">

                            <resource>META-INF/spring.factories</resource>

                        </transformer>

                        <transformer

                                implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">

                            <resource>META-INF/spring.schemas</resource>

                        </transformer>

                        <transformer

                                implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />

                        <transformer

                                implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">

                            <mainClass>com.cord.StartApplication</mainClass>

                        </transformer>

                    </transformers>

                </configuration>

                <executions>

                    <execution>

                        <phase>package</phase>

                        <goals>

                            <goal>shade</goal>

                        </goals>

                    </execution>

                </executions>

            </plugin>

        </plugins>

    </build>

</project>

需要注意的是依赖中排除掉的日志模块,以及特殊的打包方式

定义配置类:

SparkContextBean.class

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

@Configuration

public class SparkContextBean {

    private String appName = "sparkExp";

    private String master = "local";

    @Bean

    @ConditionalOnMissingBean(SparkConf.class)

    public SparkConf sparkConf() throws Exception {

        SparkConf conf = new SparkConf().setAppName(appName).setMaster(master);

        return conf;

    }

    @Bean

    @ConditionalOnMissingBean

    public JavaSparkContext javaSparkContext() throws Exception {

        return new JavaSparkContext(sparkConf());

    }

    @Bean

    @ConditionalOnMissingBean

    public HiveContext hiveContext() throws Exception {

        return new HiveContext(javaSparkContext());

    }

    ......

}

启动类:

StartApplication.class

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

@SpringBootApplication

public class StartApplication implements CommandLineRunner {

    @Autowired

    private HiveContext hc;

    public static void main(String[] args) {

        SpringApplication.run(StartApplication.class, args);

    }

    @Override

    public void run(String... args) throws Exception {

        DataFrame df = hc.sql("select count(1) from LCS_DB.STAFF_INFO");

        List<Long> result = df.javaRDD().map((Function<Row, Long>) row -> {

            return row.getLong(0);

        }).collect();

        result.stream().forEach(System.out::println);

}

执行方式:

1

2

3

4

5

6

spark-submit \

    --class com.cord.StartApplication  \

    --executor-memory 4G \

    --num-executors 8 \

    --master yarn-client \

/data/cord/spark-example-1.0-SNAPSHOT.jar

参考链接:

https://stackoverflow.com/questions/45189701/submitting-spring-boot-application-jar-to-spark-submit

https://my.oschina.net/woter/blog/1843755


版权声明 : 本文内容来源于互联网或用户自行发布贡献,该文观点仅代表原作者本人。本站仅提供信息存储空间服务和不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权, 违法违规的内容, 请发送邮件至2530232025#qq.cn(#换@)举报,一经查实,本站将立刻删除。
原文链接 : https://www.cnblogs.com/cord/p/9530404.html
相关文章
  • SpringBoot自定义错误处理逻辑介绍

    SpringBoot自定义错误处理逻辑介绍
    1. 自定义错误页面 将自定义错误页面放在 templates 的 error 文件夹下,SpringBoot 精确匹配错误信息,使用 4xx.html 或者 5xx.html 页面可以打印错误
  • Java实现手写一个线程池的代码

    Java实现手写一个线程池的代码
    线程池技术想必大家都不陌生把,相信在平时的工作中没有少用,而且这也是面试频率非常高的一个知识点,那么大家知道它的实现原理和
  • Java实现断点续传功能的代码

    Java实现断点续传功能的代码
    题目实现:网络资源的断点续传功能。 二、解题思路 获取要下载的资源网址 显示网络资源的大小 上次读取到的字节位置以及未读取的字节
  • 你可知HashMap为什么是线程不安全的
    HashMap 的线程不安全 HashMap 的线程不安全主要体现在下面两个方面 在 jdk 1.7 中,当并发执行扩容操作时会造成环形链和数据丢失的情况 在
  • ArrayList的动态扩容机制的介绍

    ArrayList的动态扩容机制的介绍
    对于 ArrayList 的动态扩容机制想必大家都听说过,之前的文章中也谈到过,不过由于时间久远,早已忘却。 所以利用这篇文章做做笔记,加
  • JVM基础之字节码的增强技术介绍

    JVM基础之字节码的增强技术介绍
    字节码增强技术 在上文中,着重介绍了字节码的结构,这为我们了解字节码增强技术的实现打下了基础。字节码增强技术就是一类对现有字
  • Java中的字节码增强技术

    Java中的字节码增强技术
    1.字节码增强技术 字节码增强技术就是一类对现有字节码进行修改或者动态生成全新字节码文件的技术。 参考地址 2.常见技术 技术分类 类
  • Redis BloomFilter布隆过滤器原理与实现

    Redis BloomFilter布隆过滤器原理与实现
    Bloom Filter 概念 布隆过滤器(英语:Bloom Filter)是1970年由一个叫布隆的小伙子提出的。它实际上是一个很长的二进制向量和一系列随机映射
  • Java C++算法题解leetcode801使序列递增的最小交换次

    Java C++算法题解leetcode801使序列递增的最小交换次
    题目要求 思路:状态机DP 实现一:状态机 Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 class Solution { public int minSwap(int[] nums1, int[] nums2) { int n
  • Mybatis结果集映射与生命周期介绍

    Mybatis结果集映射与生命周期介绍
    一、ResultMap结果集映射 1、设计思想 对简单的语句做到零配置,对于复杂一点的语句,只需要描述语句之间的关系就行了 2、resultMap的应用场
  • 本站所有内容来源于互联网或用户自行发布,本站仅提供信息存储空间服务,不拥有版权,不承担法律责任。如有侵犯您的权益,请您联系站长处理!
  • Copyright © 2017-2022 F11.CN All Rights Reserved. F11站长开发者网 版权所有 | 苏ICP备2022031554号-1 | 51LA统计