背景
我们在开发场景中肯定是在本地进行调试,然后打包部署到服务器进行运行,频繁打包测试即没有效率也不实际
下面的场景以我在本地使用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>