欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spark 异常解决:A master URL must be set in your configuration

程序员文章站 2022-07-12 14:16:09
...

为了省去每次都创建spark对象,写了一个基础类SparkSessionBase,没想到集群抛出异常:A master URL must be set in your configuration。原因就在于spark对象在main函数之外创建…

class SparkSessionBase {
    protected val spark:SparkSession =SparkSession.builder().enableHiveSupport().getOrCreate();
}

object ImportHiveData2Kafka extends SparkSessionBase {
    def main(args: Array[String]): Unit = {
        val topic = args(0)
        val dataType = args(1)
        val dataSourceType = args(2)
        var infos: Dataset[String] = null
        dataSourceType match {
            case "parquet" =>  {infos = spark.read.load(args(3)).toJSON}
            case "hive" => {infos = spark.sql("select * from " + args(3) + " where dt='" + args(4) +"'").toJSON}
            case _  => null
        }
        infos.foreachPartition(info => importToKafka(topic, dataType, info.toList))
        spark.close()
    }
}

spark 异常解决:A master URL must be set in your configuration