spark加载外部资源方式

首选说明spark加载文件:

1、采用 Source. fromFile (LocalPath)方式加载,可加载本地文件,这里本地文件指的是非集群方式

2、加载hdfs,sc.textfile()

3、采用 sc.textFile(“file:///path to the file/”),要求本地文件必须存在集群上的所有节点,且路径相同 (集群模式),local模式可以加载。个人测试机 yarn-client模式, 在client机器上是没办法读取,找不到路径。

spark-submit 方式提交任务,  同时上传资源配置文件。   也可以通过 Main方法  传参方式

1、yarn-client




这样打包就会将资源配置文件打包到jar包,调用:

/*    val in = getClass.getResourceAsStream("/mavenconfig.properties")
    val prop = new Properties()
    prop.load(new BufferedInputStream(in))
    println(prop.getProperty("a"))*/

这里要注意路径,是在jar包中的路径  /    根目录

如果不是配置文件(key=value形式),

val in = getClass.getResourceAsStream("/mavenconfig.properties")

输入流的处理,譬如一般文件或者图片


2、--properties-files    config.properties 

这里config.properties 文件中的名字 要以spark. 开头 。 spark.host=1.1.1.1    spark版本1.6 不知道高版本什么情况

/*    val host= sc.getConf.get("spark.host")
    println("host:"+host)*/

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包中;

4、addfile()  addjar() 没测试过,具体看下面的博客。  addfile(path) path如果是本地路径,需要在本地有该文件 。



http://blog.csdn.net/zhzhji440/article/details/54023020

http://blog.csdn.net/guohecang/article/details/52095387

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值