您的位置:首页 > 其它

spark加载外部资源方式

2017-11-02 18:39 162 查看
首选说明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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: