SpingBoot项目下Spark在IDEA中进行本地测试

背景

我们在开发场景中肯定是在本地进行调试,然后打包部署到服务器进行运行,频繁打包测试即没有效率也不实际
下面的场景以我在本地使用spark local模式访问hive为例

相关代码配置

1、代码
    val session = SparkSession.builder().master("local").enableHiveSupport().getOrCreate()
    val conf=new Configuration
    conf.addResource("hebing/core-site.xml")
    conf.addResource("hebing/hdfs-site.xml")
    conf.addResource("hebing/hive-site.xml")
    session.sparkContext.hadoopConfiguration.addResource(conf);
    val frame = session.sql("select * from hive.vbapf20086ecdfc3402f9660ec553ea8ddb2 limit 10")

这里面需要添加core-site.xml hdfs-site.xml hive-site.xml,这些其实在官方文档也有说明,当然了,也得将相应的hosts配置完善。然后即可访问

2、maven配置
       <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-sql_2.11</artifactId>
            <version>2.4.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-hive_2.11</artifactId>
            <version>2.4.0</version>
        </dependency>

        <dependency>
            <groupId>org.codehaus.janino</groupId>
            <artifactId>janino</artifactId>
            <version>3.0.8</version>
        </dependency>

遇到的问题

1、运行IDEA的时候日志很多

当我点击运行的时候,发现里面日志特别多,有INFO和debug的日志,然后查询相关的结果在一大堆日志中发现很不方便,在网上搜索了很多的答案,无非是将log4j.properties放到resource下面并且将打到concole的日志级别提高。但是不管用。
其实原因还是springBoot默认的日志插件是logback 所以在这里log4j是不起作用的,所以我们需要将

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

中这个logging排除,然后再将spark中默认的log4j.properties 拷贝到resources下,将log4j.rootCategory=INFO, console 改为 log4j.rootCategory=ERROR, console 即可

参考资料:http://blog.didispace.com/springbootlog4j/

2、java.lang.NoClassDefFoundError: org/apache/spark/sql/SparkSession

这个错误是因为 依赖的 spark-sql的依赖

<dependency>
	<groupId>org.apache.spark</groupId>
	<artifactId>spark-sql_2.11</artifactId>
	<version>2.4.1</version>
	<scope>provided</scope>
</dependency>

provided 删除即可,因为这个是在运行时依赖无效(maven相关的知识)

3、Caused by: java.lang.ClassNotFoundException:

org.codehaus.janino.InternalCompilerException

缺少依赖

        <dependency>
            <groupId>org.codehaus.janino</groupId>
            <artifactId>janino</artifactId>
            <version>3.0.8</version>
        </dependency>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值