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. 关于restful开发的疑惑

    if  你没有了解过restful  return; 一.疑惑 restful风格开发是有争议的,restful的设计是请求“resource”,然后只能对“resource”做CRUD操作.抽象于这 ...

  2. 查询表空间及已使用情況的SQL语句

    语句一: select  f.tablespace_name tablespace_name, round((d.sumbytes / 1024 / 1024 / 1024), 2) total_g, ...

  3. DDL-常见的约束

    一.常见的约束NOT NULL:非空,该字段的值必填UNIQUE:唯一,该字段的值不可重复DEFAULT:默认,该字段的值不用手动插入有默认值CHECK:检查,mysql不支持PRIMARY KEY: ...

  4. MessageBox.Show用法

    private void button3_Click(object sender, EventArgs e) { MessageBox.Show("  1  个参数 "); } ~ ...

  5. Java并发编程(七)终结线程

    线程的状态 一个线程会有如下五个状态 1.新建:线程在被创建时会暂时处于这种状态,此时系统为线程分配资源并对其进行初始化 2.就绪:此时线程已经可以运行,只是系统没有为其分配CPU时间. 3.运行:系 ...

  6. vue+element 点击按钮后 导致 刷新页面 致url中拼接 ? 或者拼接参数

    https://blog.csdn.net/sinat_37255207/article/details/88917162 element 自己的<el-form></el-form ...

  7. Ubuntu18.04挂载exfat格式移动硬盘

    1.安装exfat-fuse 命令:sudo apt-get install exfat-fuse 2.重新插拔移动硬盘,即可识别 查看挂载命令:lsblk

  8. Mongodb安装步骤(基于mongodb-3.2.12-tar.gz)

    1. 下载mongodb数据库:https://www.mongodb.com/download-center#community 2. 加压tar.gz压缩包,把解压文件拷贝到程序目录即可 3. 创 ...

  9. 大数据调错系列之hadoop在开发工具控制台上打印不出日志的解决方法

    (1)在windows环境上配置HADOOP_HOME环境变量 (2)在eclipse上运行程序 (3)注意:如果eclipse打印不出日志,在控制台上只显示 1.log4j:WARN No appe ...

  10. go_json解析

    总结: 其他类型转json func Marshal(v interface{}) ([]byte, error)  json 转其他类型 func Unmarshal(data []byte, v ...