工程开发技巧

  1. spark读取配置文件中的配置
spark-submit
--files /data/apps/config.properties 
  1. spring boot 两种配置文件,一种是application.properties,另一种是application.yml
massage:
  data:
    name: haha
-- 注意:中间是有一个空格
  1. spring boot 多环境配置,通过指定启动参数使用不同的profile,比如:
spring:
  profiles:
    active: prod

测试环境:java -jar my-spring-boot.jar --spring.profiles.active=test
生产环境:java -jar my-spring-boot.jar --spring.profiles.active=prod

  1. 配置文件数据的读取
@Value("${message.data.name}")
private String name;
  1. mybatis配置文件
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath*:mybatis-config.xml"/>
<property name="mapperLocations">
    <list>
        <value>classpath:/com/atcl/elwin/platform/mapper/*Mapper.xml</value>
    </list>
</property>

classpath是指WEB-INF文件夹下的classes目录
查询jar中的指定的配置文件需要classpath*

  1. maven
    一个仓库一般分为public(Release)仓和SNAPSHOT仓,前者存放正式版本,后者存放快照版本。
    指定的版本号带有-SNAPSHOT后缀,比如版本号为Junit-4.10-SNAPSHOT,那么打出的包就是一个快照版本
    频繁的发布SNAPSHOT版本,以便让其它项目能实时的使用到最新的功能做联调;当版本趋于稳定时,再发布一个正式版本,供正式使用。当然在做正式发布时,也要确保当前项目的依赖项中不包含对任何SNAPSHOT版本的依赖,保证正式版本的稳定性。
  2. unit test
@RunWith(SpringRunner.class)
@SpringBootTest
  1. applicationContext.xml生成mapper时,要把之前的xml删除掉,否则会往后追加,报以下错误:参考文章
Caused by: java.lang.IllegalArgumentException: Result Maps collection already contains value for com.mapper.PetMapper.BaseResultMap
image.png
  1. sqlSessionFactory报以下错误:
    将公司的MapperScannerConfigurer改成默认org.mybatis.spring.mapper.MapperScannerConfigurer解决问题
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tcMapper'
defined in file xxxxxxxx: Error setting property values;
 nested exception is org.springframework.beans.PropertyBatchUpdateException;
 nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'sqlSessionFactory' threw exception;
 nested exception is java.lang.NullPointerException

参考文章

  1. Spring 配置方式,指定默认配置
    @Value("${NamesrvAddr:192.168.0.1}")
  2. Sprint boot scala
    报错:making jar files - No auto configuration classes found in META-INF/spring.factories

增加如下build plugin即可

 <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
      <fork>true</fork>
      <mainClass>${start-class}</mainClass>
    </configuration>
    <executions>
      <execution>
          <goals>
              <goal>repackage</goal>
          </goals>
      </execution>
    </executions>
  </plugin>

也有可能,打包后执行 java -jar spring-cloud-eureka-0.0.1-SNAPS HOT.jar
提示 spring-xxx-xxx-0.0.1-SNAPSHOT.jar中没有主清单属性

怎么查看jar包的主清单呢?

以SpringBoot为例,jar包中包含了三个文件夹:BOOT-INF,META-INF,org,可以把jar包解压到文件夹下查看,其中META-INF文件夹下有一个MANIFEST.MF文件,该文件指明了程序的入口以及版本信息等内容
Manifest-Version: 1.0
Implementation-Title: spring-xxx-xxx
Implementation-Version: 0.0.1-SNAPSHOT
Archiver-Version: Plexus Archiver
Built-By: XXXX
Implementation-Vendor-Id: com.huyikang.practice
Spring-Boot-Version: 1.5.9.RELEASE
Implementation-Vendor: Pivotal Software, Inc.
Main-Class: org.springframework.boot.loader.JarLauncher
Start-Class: com.huyikang.practice.eureka.Application
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Created-By: Apache Maven 3.5.2
Build-Jdk: 1.8.0_151
Implementation-URL: http://maven.apache.org

这些值都是SpringBoot打包插件会默认生成的,如果没有这些属性,SpringBoot程序自然不能运行,就会报错:jar中没有主清单属性,也就是说没有按照SpringBoot的要求,生成这些必须的属性。

  • Main-Class代表了Spring Boot中启动jar包的程序
  • Start-Class属性就代表了Spring Boot程序的入口类,这个类中应该有一个main方法
  • Spring-Boot-Classes代表了类的路径,所有编译后的class文件,以及配置文件,都存储在该路径下
  • Spring-Boot-Lib表示依赖的jar包存储的位置
  1. spark 平台报错
    Unsupported major.minor version 52.0
    javap -verbose com.xxx.XXClass 可以反编译class信息
    改为使用1.7编译即可,maven中增加以下插件
 <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
  1. maven plugin有执行顺序的
  2. spark 1.6是jdk7,spark 2.0+是jdk8
  3. spring boot 的启动类为:org.springframework.boot.loader.JarLauncher,spark的启动类设置

本机启动命令:mvn clean spring-boot:run

  1. 异常解决
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'spring.datasource.CONFIGURATION_PROPERTIES': Initialization of bean failed; nested exception is javax.validation.Validation
Exception: Unable to create a Configuration, because no Bean Validation provider could be found. Add a provider like Hibernate Validator (RI) to your classpath.

解决方法:https://stackoverflow.com/questions/36938855/error-creating-bean-with-name-spring-datasource-configuration-properties

  1. Maven deploy跳过某个module解决办法
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-deploy-plugin</artifactId>
    <version>2.8.2</version>
    <configuration>
        <skip>true</skip>
    </configuration>
</plugin>
  1. 用户编写的spark程序打包成jar后提交到yarn执行时,经常会遇到jar包中明显存在某个类,但任务提交到yarn运行时却找不到类或方法(java.lang.NoSuchMethodError)的问题。
    原因是本地的jar包被SPARK_HOME/lib中的jar覆盖。spark程序在提交到yarn时,除了上传用户程序的jar,还会上传SPARK_HOME的lib目录下的所有jar包(参考附录2 )。如果你程序用到的jar与SPARK_HOME/lib下的jar发生冲突,那么默认会优先加载SPARK_HOME/lib下的jar,而不是你程序的jar,所以会发生“ NoSuchMethodError”
spark.driver.userClassPathFirst
spark.executor.userClassPathFirst
  1. val df = sqlContext.table("dbName.tableName")
Exception in thread "main" org.apache.spark.sql.AnalysisException: Specifying database name or other qualifiers are not allowed for temporary tables. If the table name has dots (.) in it, please quote the table name with backticks (`).;

You can't do that from sqlContext, you'll need to define a HiveContext for that as followed :
https://stackoverflow.com/questions/33221092/need-to-access-hive-table-using-database-qualifier-from-spark

  1. Save Spark dataframe as dynamic partitioned table in Hive

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,271评论 5 466
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,725评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,252评论 0 328
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,634评论 1 270
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,549评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 47,985评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,471评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,128评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,257评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,233评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,235评论 1 328
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,940评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,528评论 3 302
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,623评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,858评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,245评论 2 344
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,790评论 2 339

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,559评论 18 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,706评论 6 342
  • 《Spring Boot开发:从0到1》 大纲结构v2.0 第一部分Spring Boot基础 第1章 Sprin...
    光剑书架上的书阅读 10,921评论 1 70
  • 我未饮美酒,醉意已融情。晚风渐清凉,明月映长空。云散漫天星,天宫灯火明。不知有情人,能否长相守。
    秋之暖风阅读 316评论 0 0
  • 有道是,余情未尽梦一场,何必挽回过往,不思量,痛将回忆埋葬,相思遗忘。
    秋水葬剑意阅读 206评论 0 0