Scala 重命名hdfs文件

@羲凡——只为了更好的活着

Scala 重命名hdfs文件

楼主使用scala2.11.12版本

1.主要的内容

fileSystem.rename(new Path(beforeFilePath), new Path(afterFilePath))

2.不啰嗦直接上代码
import java.net.URI

import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.fs.{FileStatus, FileSystem, FileUtil, Path}

object RenameHdfsFile {
  def main(args: Array[String]): Unit = {
    
    val path = "/testdata/data/test"
    val sourceFile = "part-"
    renameFile(path, sourceFile)

    def renameFile(path: String, sourceFile: String): Unit = {
      val fileSystem = FileSystem.newInstance(URI.create(path), new Configuration())
      val fsArr: Array[FileStatus] = fileSystem.listStatus(new Path(path))
      val paths: Array[Path] = FileUtil.stat2Paths(fsArr)
      val arr = paths.filter(fileSystem.getFileStatus(_).isFile()).map(_.toString)
      println("===========亲,原文件如下==================")
      arr.foreach(println)

      println("===========亲,下面执行重命名函数===========")
      for (i <- 0.until(arr.length)) {
        val beforeFilePath = arr(i)
        val lastIndex = beforeFilePath.lastIndexOf("/") + 1
        val beforeFileName = beforeFilePath.substring(lastIndex)
        if (beforeFileName.contains(sourceFile)) {
          val num = beforeFilePath.substring(lastIndex + 6, lastIndex + 10).toInt
          val afterFilePath = beforeFilePath.substring(0, lastIndex) + s"#$num-r-00000"
          fileSystem.rename(new Path(beforeFilePath), new Path(afterFilePath))
          println(s"亲,$beforeFileName 被重命名为 #$num-r-00000 ")
        } else {
          println(s"亲,$beforeFileName 这个不是我想要的重命名的文件哦")
        }
      }
    }

    println("====================羲凡success======================")
  }
}
3.执行结果
===========亲,原文件如下==================
hdfs://ns/testdata/data/test/coverage
hdfs://ns/testdata/data/test/part-00000-e9d534da-d1e1-47fa-9d0e-ef9734d900d4-c000.csv
hdfs://ns/testdata/data/test/part-00001-e9d534da-d1e1-47fa-9d0e-ef9734d900d4-c000.csv
hdfs://ns/testdata/data/test/part-00002-e9d534da-d1e1-47fa-9d0e-ef9734d900d4-c000.csv
hdfs://ns/testdata/data/test/part-00003-e9d534da-d1e1-47fa-9d0e-ef9734d900d4-c000.csv
hdfs://ns/testdata/data/test/part-00004-e9d534da-d1e1-47fa-9d0e-ef9734d900d4-c000.csv
===========亲,下面执行重命名函数===========
亲,coverage 这个不是我想要的重命名的文件哦
亲,part-00000-e9d534da-d1e1-47fa-9d0e-ef9734d900d4-c000.csv 被重命名为 #0-r-00000 
亲,part-00001-e9d534da-d1e1-47fa-9d0e-ef9734d900d4-c000.csv 被重命名为 #1-r-00000 
亲,part-00002-e9d534da-d1e1-47fa-9d0e-ef9734d900d4-c000.csv 被重命名为 #2-r-00000 
亲,part-00003-e9d534da-d1e1-47fa-9d0e-ef9734d900d4-c000.csv 被重命名为 #3-r-00000 
亲,part-00004-e9d534da-d1e1-47fa-9d0e-ef9734d900d4-c000.csv 被重命名为 #4-r-00000 
====================羲凡success============

====================================================================
@羲凡——只为了更好的活着

若对博客中有任何问题,欢迎留言交流

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值