spark 迁移数据

有时候我们经常会用到以下场景,比如说将各种各样的文件从一个地方复制到另外一个地方,类似于hadoop 的dstcp功能,当然我们可以通过各种hdfs或者s3命令,以下的demo就是通过spark去实现类似这样的功能

val source=""
val target=""
val data=sc.binaryFiles(source)
data.foreach(x =>{
         val path=target + x._2.getPath().substring(x._2.getPath().lastIndexOf("/"))
         val dir = new Path(path)
         val fs = FileSystem.get(URI.create(path),conf)
         val inputStream = x._2.open()
         val outStream: FSDataOutputStream = fs.create(dir)
         IOUtils.copyBytes(inputStream, outStream, 4096, true)
         outStream.close()
         inputStream.close()
         fs.close()
})

猜你喜欢

转载自blog.csdn.net/zhouyan8603/article/details/84302783