【问题标题】:Spark and Hadoop Testing the MethodsSpark 和 Hadoop 测试方法
【发布时间】:2018-02-07 19:57:31
【问题描述】:

我有一个从 Hdfs 读取文件并尝试测试该方法的方法

我首先尝试了 HDFSMini 集群,但没有成功。这种类型的方法是否可以测试。如果是这样,需要什么依赖项来测试它以及如何在不安装 hadoop 的情况下在本地模拟 Hdfs 文件系统。应该没有hadoop安装的依赖。我不能要求所有想测试安装 hadoop 的人。

def readFiles(fs: FileSystem,path: Path): String = {
    val sb = new mutable.StringBuilder()
    var br : BufferedReader =null
    var line : String = ""
    try{
      if(fs.exists(path)){
        if(fs.isFile(path)){
          br = new BufferedReader(new InputStreamReader(fs.open(path)))
          while ((line = br.readLine()) != null)
            sb.append(line.trim)
        } else {
          throw new InvalidPathException(s"${path.toString} is a directory, please provide the full path")
        }
      }else {
        throw new InvalidPathException(s"${path.toString} is an invalid file path ")
      }
    } catch {
      case e: Exception => throw e
    } finally {
      if (br != null){
        try {
          br.close()
        } catch {
          case e: Exception => throw e
        }
      }
    }

    sb.toString

  }

【问题讨论】:

  • 如何模拟 Hdfs 文件系统 - 聪明人说不要模拟你不拥有的东西。您可能同意或不同意,购买肯定要考虑的东西。只需浏览一下您的代码 - 您到底要测试什么?
  • @user6910411 我将从文件中读取数据并将其与字符串进行比较。
  • @user6910411 如果我能够模拟 hdfs,我可以使用数据帧阅读器读取数据并验证它

标签: scala testing mockito hdfs scalatest


【解决方案1】:

在处理 org.apache.hadoop.fs.FileSystem(Spark 也是如此)时,我通常将测试数据文件存储在:

src/test/resources

例如

src/test/resources/test.txt

本地 org.apache.hadoop.fs.FileSystem 可以使用相对于项目根目录的路径访问,即“src/test/resources/test.txt”:

test("Some test") {
  val fileSystem = FileSystem.get(new Configuration())
  val fileToRead = new Path("src/test/resources/test.txt")
  val computedContent = readFiles(fileSystem, fileToRead)
  val expectedContent = "todo"
  assert(computedContent === expectedContent)
}

【讨论】:

  • 我收到以下错误。 Invalid path name ../resources/test.txt is an invalid file path org.apache.hadoop.fs.InvalidPathException: Invalid path name ../resources/test.txt is an invalid file path
  • 您是否使用了相对于您的测试文件的相对路径“../resources/test.txt”?您可以尝试使用项目根目录的相对路径(项目结构中的绝对路径)吗?即“src/test/resources/test.txt”。
  • 我将它添加为相对路径,这是错误 ../resources/test.txt Invalid path name ../resources/test.txt is an invalid file path org.apache.hadoop.fs .InvalidPathException: 无效的路径名 ../resources/test.txt 是 com.rxcorp.ds.utils.CommonUtils$.readFiles(CommonUtils.scala:124) 处的无效文件路径
  • 我的意思是你可以尝试使用“src/test/resources/test.txt”而不是“../resources/test.txt”吗? src 在你的 scala 项目的根文件夹中
  • 我已经使用过时路径 C:/Users/XXX/Documents/Projects/XXX/XXXX/src/test/resources/test.txt 但现在我得到了 java.lang.NullPointerException抛出。 java.lang.NullPointerException
猜你喜欢
  • 1970-01-01
  • 2016-06-02
  • 1970-01-01
  • 2017-08-27
  • 2014-07-30
  • 1970-01-01
  • 2014-07-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多