spark读取外部配置文件的方法

spark-submit  --files /tmp/fileName /tmp/test.jar

使用spark提交时使用--files参数,spark会将将本地的文件上传的hdfs,然后分发给每个executor

在程序中只需要使用文件名获取数据

val    filePath ="fileName"

val    props =newProperties()

props.load(newFileInputStream(filePath))

//发送到executor去执行

val  rdd=sc.parallelize(0to3)

rdd.foreach(index=>

props.keySet().toArray().foreach(x=>println(x+"\t"+props.getProperty(x.toString)))

)

java的方式也是一样的,在这就不写了

3、--files   ./config.properties

读一般文件:
val t: BufferedSource = scala.io.Source.fromFile("config.properties")
t.getLines().foreach(t=>println(t))

读配置文件:

/*    val config = "config.properties"
val prop = new Properties()
prop.load(new FileInputStream(config))
val keyset = prop.keySet().toArray()
keyset.foreach(t=>println(t+" "+prop.getProperty(t.toString)))*/
配置文件类加载测试 配置采用 key=value 的形式 client/cluster 采用 sc.getConf.get 方法;配合submit 参数–properties-file 上传配置文件; 配置文件key value 以空格为分隔符
配置文件类加载测试 配置采用 key=value 的形式 client/cluster 采用java.util.Properties 方法;配置文件打包到jar包里; 配置文件key value 以“=”为分隔符
资源文件类加载测试 普通文本格式,非key value模式 client/cluster 采用scala.io.Source.fromFile 方法;资源文件采用submit 参数–files 上传;
资源文件类加载测试 普通文本格式,非key value模式 client/cluster 采用scala.io.Source.fromFile和getResourceAsStream方法;资源文件打包到jar包中;

在/tmp下创建a文件,内容为:

this is  a test data
this is a test data
this is a test data
this is a test data
this is a test data
this is a test data
this is a test data
this is a test data
this is a test data
this is a test data
this is a test data

spark-shell --master yarn --files "/tmp/a"

可以看到a文件被上传到hdfs上了:

在代码中读取该文件,如下

可以看见这个文件在excutor被正确读取:且在两个excutor上分别执行,一个打印了22行,一个打印11行,原文件总共11行;上述rdd公有三个元素,每个元素遍历时打印一遍,总共

3*11=33

spark读取外部配置文件的方法的更多相关文章

  1. jar包读取jar包内部和外部的配置文件,springboot读取外部配置文件的方法

    jar包读取jar包内部和外部的配置文件,springboot读取外部配置文件的方法 用系统属性System.getProperty("user.dir")获得执行命令的目录(网上 ...

  2. SpringBoot读取外部配置文件的方法

    SpringBoot读取外部配置文件的方法 Spring高级之注解@PropertySource详解(超详细) 1.@PropertySource(value = {"classpath:c ...

  3. springboot读取外部配置文件

    springboot项目打成jar包后不好进行配置文件修改,可设置为读取外部配置文件,方便进行配置修改. 步骤: 1.将jar包中的application.properties配置文件复制到自定义路径 ...

  4. java读取properties配置文件的方法

    app.properties mail.smtp.host=smtp.163.com mail.transport.protocol=smtp import java.io.InputStream; ...

  5. linux共享文件samba安装与java读取外部文件夹方法

    测试环境RedHat 6.4 一.安装 samba组件安装: (1)首先用“rpm –qa |grep samba”命令检验系统samba服务是否安装. #rpm –qa |grep samba sa ...

  6. 读取spring配置文件的方法(spring读取资源文件)

    1.spring配置文件 <bean id="configproperties" class="org.springframework.beans.factory. ...

  7. 读取properties配置文件的方法

    一般在.properties文件中配置数据库连接的相关信息,我们需要从中读取信息,以便建立与数据库的连接. 文件目录: application.properties配置信息: url=jdbc:ora ...

  8. Python读取ini配置文件封装方法

    读取配置文件 ----rw_ini.py from configparser import ConfigParser def read_config(config_file_path:str): &q ...

  9. delphi 安卓程序如何读取外部配置文件

    1)编辑一个config.txt文件,填写配置系统. 2)有外部加载文件时,安卓发布需要另行指定文件发布目录位置 比如加载config.txt需要在 首先利用Project->Deploymen ...

随机推荐

  1. [转载]WebDriver工作原理

    转载自:https://www.cnblogs.com/testermark/p/3546287.html WebDriver的工作原理:  在我们new一个WebDriver的过程中,Seleniu ...

  2. C 标准库 中 操作 字符串 的 代码

    1)字符串操作 strcpy(p, p1) 复制字符串 strncpy(p, p1, n) 复制指定长度字符串 strcat(p, p1) 附加字符串 strncat(p, p1, n) 附加指定长度 ...

  3. sqldeveloper建立新的连接是出现Status : Failure -Test failed: IO Error: The Network Adapter could not establish the connection

    Status : Failure -Test failed: IO Error: The Network Adapter could not establish the connection解决办法: ...

  4. 虚拟机重启网络服务失败,当查看状态显示错误Failed to start LSB......

    重启网络失败截图 从本质上来看出现这样的问题,是因为拷贝过来的虚拟机重新分配了网卡MAC地址.这样造成的结果是配置文件中MAC与当前网卡MAC不一致.所以只需要修改一下配置文件即可. 用ip addr ...

  5. hubilder 打包app ios高版本不支持问题

    <script type="text/javascript"> document.addEventListener('plusready', function(){ v ...

  6. 【gitlab平台的搭建】

    gitlab同github相同,具有把源码集中存放的功能,同时依靠git进行code的同步,在实际的开发过程中可保证团队的项目同步,同时便于便于维护等 #下载这个rpm包 #gitlab.rb访问地址 ...

  7. php 面试常问 基础知识

    字符串都是海针 数组是针海 输出及打印 echo 8%(-2); //输出结果为0 模后的正负号可无视 模前的有用 echo date('Y-m-d H:i:s',strtotime('-1 day' ...

  8. 常用PHP方法

    个人常用的一些方法记录/** * 返回错误 * * @param int $err_no * @param string $err_msg * @param array $data * @return ...

  9. [project X] tiny210(s5pv210)上电启动流程(BL0-BL2)

    建议参考文档: S5PV210-iROM-ApplicationNote-Preliminary-20091126 S5PV210_UM_REV1.1 项目介绍参考 [project X] tiny2 ...

  10. python3 实现一个多级菜单小功能

    记录下一下 #!/usr/bin/env python3 ''' 需求:三级菜单 三级菜单,依次进入子菜单 ''' City = { '北京':{ '大兴区':[ '亦庄','黄村','中信新城',' ...