0103 spark应用提交时加载本地配置文件及解析方式

本文介绍如何在Spark应用提交到Hadoop集群时加载本地配置文件,并将其转化为HashMap进行key-value读取。提交脚本将本地文件作为主函数参数传递,详细步骤和参考资料提供。
摘要由CSDN通过智能技术生成

在spark应用提交到集群时,如果我们需要加载本地(驱动节点)的配置文件时,如:

...
spark-submit \
 -class ...
 -jars ...
 -jar ...
 file:///yourLocalFilePath/conf.properties

在解析该配置文件时,通常我们将其处理为HashMap形式,通过key-value方式实现便捷的配置参数读取。具体实现方式如下:

package com.gendlee1991.utils


import java.io.{
   FileInputStream, FileNotFoundException, InputStreamReader}
import java.util.Properties

import org.apache.commons.logging.LogFactory
import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.fs.{
   FSDataInputStream, Path}

import scala.collection.mutable.HashMap

class BigDataConf(propertiesPath: String) extends Serializable
{
   

    val log = LogFactory.getLog(classOf[BigDataConf])

    private val settings = new HashMap[String, String]()
    load(propertiesPath)

    def load(propertiesPath: String)
    {
   
        loadProPertiesFile(propertiesPath)
    }

    def loadProPertiesFile(propertiesPath: String)
    {
   
        var in: FSDataInputStream = null
        var inr: InputStreamReader = null
        try
        {
   
            val conf = new Configuration()
            val path = new Path(propertiesPath
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值