scala 在 spark 中操作 hdfs
2015-07-08 10:00
381 查看
问题场景:在编写spark程序时,输出目录存在会造成spark任务失败。处理方法有2种,第一利用脚本方式,在主任务之上加层处理,第二,在spark任务中先处理掉目录。spark支持的可编程语言有Scala、Python、Java。其中Python无需编译打包,十分方便,但是貌似没有处理hdfs系统的Python接口,而Python的执行脚本语言是异步于主任务的,换言之,主任务与脚本任务的执行完成顺序是不确定的。而Java语言貌似不是主流的spark语言。其中最完善的就是原生的Scala语言。所以在Scala中完成对hdfs的控制,及对主任务计算方式的控制,是spark任务最优雅的方式。
所需jar包: spark-assembly-1.2.1-hadoop2.4.0.jar
实例代码:
package com.util
import java.io.BufferedInputStream
import java.io.File
import java.io.FileInputStream
import java.io.InputStream
import org.apache.hadoop.conf._
import org.apache.hadoop.fs._
import org.apache.hadoop.fs.Path._
object ScalaHdfs {
def ls(fileSystem:FileSystem,path:String)= {
println("list path:"+path)
val fs = fileSystem.listStatus(new Path(path))
val listPath = FileUtil.stat2Paths(fs)
for( p <- listPath) {
println(p)
}
println("----------------------------------------")
}
def main(args: Array[String]) {
val conf = new Configuration()
println(conf)
val fileSystem = FileSystem.get(conf)
ls(fileSystem,"/")
}
}
输出结果:
[hadoop@localhost spark-1.2]$ ./spark-submit --class com.util.ScalaHdfs ScalaHdfs.jar
Spark assembly has been built with Hive, including Datanucleus jars on classpath
Configuration: core-default.xml, core-site.xml
list path:/
hdfs://localhost:9000/aaa
hdfs://localhost:9000/bbb
hdfs://localhost:9000/ccc
hdfs://localhost:9000/ddd
hdfs://localhost:9000/count
hdfs://localhost:9000/hbase
hdfs://localhost:9000/tmp
hdfs://localhost:9000/user
----------------------------------------
参考:http://m.blog.csdn.net/blog/linger2012liu/43314651
所需jar包: spark-assembly-1.2.1-hadoop2.4.0.jar
实例代码:
package com.util
import java.io.BufferedInputStream
import java.io.File
import java.io.FileInputStream
import java.io.InputStream
import org.apache.hadoop.conf._
import org.apache.hadoop.fs._
import org.apache.hadoop.fs.Path._
object ScalaHdfs {
def ls(fileSystem:FileSystem,path:String)= {
println("list path:"+path)
val fs = fileSystem.listStatus(new Path(path))
val listPath = FileUtil.stat2Paths(fs)
for( p <- listPath) {
println(p)
}
println("----------------------------------------")
}
def main(args: Array[String]) {
val conf = new Configuration()
println(conf)
val fileSystem = FileSystem.get(conf)
ls(fileSystem,"/")
}
}
输出结果:
[hadoop@localhost spark-1.2]$ ./spark-submit --class com.util.ScalaHdfs ScalaHdfs.jar
Spark assembly has been built with Hive, including Datanucleus jars on classpath
Configuration: core-default.xml, core-site.xml
list path:/
hdfs://localhost:9000/aaa
hdfs://localhost:9000/bbb
hdfs://localhost:9000/ccc
hdfs://localhost:9000/ddd
hdfs://localhost:9000/count
hdfs://localhost:9000/hbase
hdfs://localhost:9000/tmp
hdfs://localhost:9000/user
----------------------------------------
参考:http://m.blog.csdn.net/blog/linger2012liu/43314651
相关文章推荐
- Spark随谈——开发指南(译)
- Spark,一种快速数据分析替代方案
- Windows7下安装Scala 2.9.2教程
- hadoop的hdfs文件操作实现上传文件到hdfs
- java连接hdfs ha和调用mapreduce jar示例
- java实现将ftp和http的文件直接传送到hdfs
- 在Hadoop2.5.0下利用Java读写HDFS
- HDFS 文件操作
- XML 文件解析--含Unicode字符的XML文件
- 读<王垠:一种新的操作系统设计>
- 分分钟掌握快速排序(Java / Scala 实现)
- Scala极速入门
- Spark初探
- Spark Streaming初探
- Scala实现REST操作
- Scala method call syntax
- 关于Scala多重继承的菱形问题
- Scala 高阶函数(high-order function)剖析
- Spray.io搭建Rest服务