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 ...
随机推荐
- 解决error possibly undefined macro AC_MSG_ERROR
问题 出现如下缺少宏的问题 error: possibly undefined macro: AC_MSG_ERROR error: possibly undefined macro: AC_SUBS ...
- MAPREDUCE框架结构及核心运行机制
1.2.1 结构 一个完整的mapreduce程序在分布式运行时有三类实例进程: 1.MRAppMaster:负责整个程序的过程调度及状态协调 2.mapTask:负责map阶段的整个数据处理流程 3 ...
- java点滴之MulticastSocket的使用
一基本概念 该类恰是上文介绍的DatagramSocket的子类. DatagramSocket仅仅同意数据报发送给指定的目标地址,而MulticastSocket能够将数据报以广播的方式发送到多个c ...
- 修改eclipse中文件打开默认方式
Window--->prefrence---->Editors----->FileAssociation 选择文件后缀,如果没有就添加,然后在上添加,删除,设置默认打开方式.
- Apache2.4.34 + php 7.28 + MySQL8.0.12 安装及配置
服务端的学习 Apache2.4.34 的安装及配置 1.基本安装 最新的 Apache 已经不提供 Windows 的安装版本,所以我们这里使用的是解压版 -下载地址:https://www.apa ...
- linux 学习第十一天
一.配置服务说明 1.1.linux系统中的一切都是文件 1.2.配置一个服务就是在修改去配置文件 1.3.要想让新的配置文件立即生效,需要重启对应的服务 二.配置网卡 2.1.编辑配置文件 vim ...
- 微信小程序——长按复制、一键复制
wxml: 订单号:<text selectable='true' bindlongtap='copy' >{{OrderModel.OrderNo}}</text><b ...
- Redis之Redis持久化
Redis(Remote Dictionary Server)是一个可持久化的内存.Key-Value数据库. 作为内存数据库,为了防止因服务器断电或系统宕机而引起的数据丢失问题,Redis自带了持久 ...
- php文件相关操作
//遍历目录及文件 function myBianli($dirname){ //1.打开 $dir = opendir($dirname); //2.读取 while($filename = rea ...
- 关于flume的filechannel的 full 问题
事务启动以后,批量向事务Transaction的一个putList的尾部写入,putlist是一个LinkedBlockingDeque . 事务提交的时候, 把putlist中的event批量移除, ...