Spark 和 Hadoop 测试方法
Spark and Hadoop Testing the Methods
我有一个方法可以从 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
}
在处理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)
}
我有一个方法可以从 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
}
在处理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)
}