sparkSql 直接执行外部 sql/hql文件
作者:互联网
话不多说上代码:
======idea 部分代码======begin=====
import org.apache.hadoop.security.UserGroupInformation
import org.apache.spark.sql.SparkSession
import java.io.File
import java.util.Properties
import scala.io.{BufferedSource, Source}
/**
* ods层数据清洗落地到dwd层
*/
object Ods2DwdFilterSql {
def main(args: Array[String]): Unit = {
val filePath: String = args(0)
//读取集群配置文件
val prop: Properties = PropertieUtil.load("config.properties")
//本地测试读文件
// val prop: Properties = PropertieUtil.getProperties("/config.properties")
System.setProperty("java.security.krb5.conf", prop.getProperty(PropConstants.KRB5_CONF_PATH))
System.setProperty("HADOOP_USER_NAME", prop.getProperty(PropConstants.HADOOP_USER_NAME))
System.setProperty("user.name", prop.getProperty(PropConstants.USER_NAME))
UserGroupInformation.loginUserFromKeytab(
prop.getProperty(PropConstants.KEYTAB_NAME), prop.getProperty(PropConstants.KEYTAB_FILE_PATH)
)
System.out.println(UserGroupInformation.getLoginUser)
val session: SparkSession = SparkSession.builder().master("local[2]")
.appName("SparkSeesionApp")
.enableHiveSupport() //支持hive
.getOrCreate()
// session.sparkContext.setLogLevel("error")
val sql: String = doFile(filePath)
println("=================sql开始=================")
println(sql)
println("=================sql结束=================")
session.sql(sql).show()
session.stop()
}
//读取外部sql文件文件
def doFile(fileName: String): String = {
val file: File = new File(fileName)
import java.io.FileInputStream
val stream: FileInputStream = new FileInputStream(file)
val buff: BufferedSource = Source.fromInputStream(stream)
//读取拼装SQL
val sql = buff
.getLines()
.mkString("\n")
sql
}
}
======idea 部分代码======end=====
=====集群部分====begin====
执行脚本:sh local_start.sh dwdFilters.sql
dwdFilters.sql 是没有语法错误的可执行sql文件
local_start.sh 是启动spark任务的
脚本内容:
#!/bin/bash
if [ $# -eq 1 ];then
spark-submit --master local[4] --class hx.com.Ods2DwdFilterSql --files /home/etl_admin/spark/config.properties sparkDwdFilter-1.0-SNAPSHOT.jar $1
else
echo "Please input command. eg: ./$0 filename.sql"
fi
那些写博客只VIP可见的装B者,一点互联网分享精神都没有,还特么不如不分享,这种B不准抄我的这个方法,不然诅咒你天天出bug,其他朋友欢迎,你不点个赞?!
标签:getProperty,String,val,prop,hql,sparkSql,sql,import 来源: https://blog.csdn.net/qq_34009542/article/details/118344114