spark读取外部配置文件的方法
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读取外部配置文件的方法的更多相关文章
- jar包读取jar包内部和外部的配置文件,springboot读取外部配置文件的方法
jar包读取jar包内部和外部的配置文件,springboot读取外部配置文件的方法 用系统属性System.getProperty("user.dir")获得执行命令的目录(网上 ...
- SpringBoot读取外部配置文件的方法
SpringBoot读取外部配置文件的方法 Spring高级之注解@PropertySource详解(超详细) 1.@PropertySource(value = {"classpath:c ...
- springboot读取外部配置文件
springboot项目打成jar包后不好进行配置文件修改,可设置为读取外部配置文件,方便进行配置修改. 步骤: 1.将jar包中的application.properties配置文件复制到自定义路径 ...
- java读取properties配置文件的方法
app.properties mail.smtp.host=smtp.163.com mail.transport.protocol=smtp import java.io.InputStream; ...
- linux共享文件samba安装与java读取外部文件夹方法
测试环境RedHat 6.4 一.安装 samba组件安装: (1)首先用“rpm –qa |grep samba”命令检验系统samba服务是否安装. #rpm –qa |grep samba sa ...
- 读取spring配置文件的方法(spring读取资源文件)
1.spring配置文件 <bean id="configproperties" class="org.springframework.beans.factory. ...
- 读取properties配置文件的方法
一般在.properties文件中配置数据库连接的相关信息,我们需要从中读取信息,以便建立与数据库的连接. 文件目录: application.properties配置信息: url=jdbc:ora ...
- Python读取ini配置文件封装方法
读取配置文件 ----rw_ini.py from configparser import ConfigParser def read_config(config_file_path:str): &q ...
- delphi 安卓程序如何读取外部配置文件
1)编辑一个config.txt文件,填写配置系统. 2)有外部加载文件时,安卓发布需要另行指定文件发布目录位置 比如加载config.txt需要在 首先利用Project->Deploymen ...
随机推荐
- 《STL源码剖析》要点摘抄
1. STL的空间配置器 SGI STL设计了双层级配置器,第一级配置器直接使用malloc().free(),第二级配置器则视情况采用不同的策略:当配置区块超过128bytes时,视为“足够大”,便 ...
- 阅读Deep Packet Inspection based Application-Aware Traffic Control for Software Defined Networks
Deep Packet Inspection based Application-Aware Traffic Control for Software Defined Networks Globlec ...
- Gradle Goodness: Excluding Tasks for Execution
In Gradle we can create dependencies between tasks. But we can also exclude certain tasks from those ...
- ArcMap中用python的split方法提取字段的值
提取PROPERTY_L字段空格分隔符前面的地址编号 提取前:5105 ABERDEEN LANE 提取后:5105 提取的表达式:!PROPERTY_L!.split(" ")[ ...
- 【Javascript-基础-ecma6】ecma6学习笔记(持续更新)
makdown语法手册API String 新特性的方法 基本不支持IE浏览器 String.fromCodePoint(6特性) String.prototype.codePointAt(6特性) ...
- TXT文件转换成DataSet数据集
/// <summary> /// TXT文件转换成DataSet数据集 /// </summary> /// <param name="FilePath&qu ...
- Web的基本工作原理、HTTP协议和URL说明
Web工作原理 客户端和Web服务器通过HTTP协议进行通信.Web服务器有是也叫HTTP服务器或Web容器.HTTP协议采用的是请求/响应模式.即客户端发起HTTP请求,web服务器接收并解析处理H ...
- (转载)SendKeys.Send()的使用
SendKeys.Send() 使用SendKeys将键击和组合键击发送到活动应用程序.此类无法实例化.若要发送一个键击给某个类并立即继续程序流,请使用Send.若要等待键击启动的任何进程,请使用Se ...
- PhpStorm中实现代码自动换行
方法一: 随便打开一个页面,在显示行号(最左边)这里鼠标右击,勾选"Use Soft Wraps". 方法二: 选择"File-->>Settings--&g ...
- laravel5.5源码笔记(二、服务提供者provider)
laravel里所谓的provider服务提供者,其实是对某一类功能进行整合,与做一些使用前的初始化引导工作.laravel里的服务提供者也分为,系统核心服务提供者.与一般系统服务提供者.例如上一篇博 ...