Spark Gradle jar 包分离方法
2018-12-30 本文已影响4人
大猪大猪
想不想把spark项目打包更快一点?如果把几百M的依赖包打成一个包是很花时间的,少则1分钟,多则几分钟,这些时间还不如留着跟前台妹子聊会天呢,经过几百遍的测试,平时要花1分20秒才能打成的jar包,现在只需要9秒,没错你没听错就是9秒,咳咳....,好了不吹牛了回归正文吧。
使用方法
build.gradle
group 'com.dounine'
version '1.0.0-SNAPSHOT'
apply plugin: 'application'
apply plugin: 'java'
apply plugin: 'scala'
sourceCompatibility = 1.8
repositories {
mavenLocal()
mavenCentral()
maven { url "http://repo.hortonworks.com/content/repositories/releases/" }
}
configurations {
provided
}
task copyJars(type: Copy) {
from configurations.runtime
into new File('build/libs/lib')
}
compileJava.dependsOn copyJars
def env = hasProperty("pro") ? "RELEASE" : "SNAPSHOT"
sourceSets {
main {
compileClasspath += configurations.provided
resources {
srcDirs = ["src/main/resources", "src/main/profile/$env"]
}
}
}
jar {
String buildDir = project.buildDir
manifest {
attributes 'Implementation-Title': 'HbaseDataInsert',
'Implementation-Version': version,
'Main-Class': 'com.dounine.hbase.HbaseHdfsWrite',
'Class-Path': new File(buildDir+'/libs/lib').list().collect { "lib/${it}" }.join(" ")
}
}
dependencies {
compile 'org.scala-lang:scala-library:2.11.12'
compile group: 'com.alibaba', name: 'fastjson', version: '1.2.54'
compile group: 'org.apache.spark', name: 'spark-sql_2.11', version: '2.3.1.3.0.1.0-187'
compile group: 'org.apache.hbase', name: 'hbase-it', version: '2.0.0.3.0.1.0-187'
}
核心配置
apply plugin: 'application'
configurations {
provided
}
task copyJars(type: Copy) {
from configurations.runtime
into new File('build/libs/lib')
}
compileJava.dependsOn copyJars
jar {
String buildDir = project.buildDir
manifest {
attributes 'Implementation-Title': 'HbaseDataInsert',
'Implementation-Version': version,
'Main-Class': 'com.dounine.hbase.HbaseHdfsWrite',
'Class-Path': new File(buildDir+'/libs/lib').list().collect { "lib/${it}" }.join(" ")
}
}
打出来的依赖包会放在build/libs/lib
里面
那应该如何提交spark任务呢?请看下面
任务提交
spark-submit --master yarn \
--class com.dounine.hbase.HbaseHdfsWrite \
--conf spark.yarn.tokens.hbase.enabled=true \
--executor-memory 1g --driver-memory 1g \
--keytab /etc/security/keytabs/hbase.headless.keytab \
--jars /root/bd/hbase/build/libs/lib/a.jar,/root/bd/hbase/build/libs/lib/b.jar,...... \
--principal hbase-bd@EXAMPLE.COM build/libs/hbase-1.0.0-SNAPSHOT.jar hdfs:///tmp/logdir2
有好多个点的地方就是要大家手动补全的jar包路径啦
好啦,开个玩笑,这么玩还不如回到当初呢
最终解决方案
使用通配符
spark-submit --master yarn \
--class com.dounine.hbase.HbaseHdfsWrite \
--conf spark.yarn.tokens.hbase.enabled=true \
--executor-memory 1g --driver-memory 1g \
--keytab /etc/security/keytabs/hbase.headless.keytab \
--jars "/root/bd/hbase/build/libs/lib/*" \
--principal hbase-bd@EXAMPLE.COM build/libs/hbase-1.0.0-SNAPSHOT.jar hdfs:///tmp/logdir2
其实搞了那么久就是多出了一个双引号而已