gpt4 book ai didi

unit-testing - Hadoop:如何对文件系统进行单元测试

转载 作者:可可西里 更新时间:2023-11-01 14:10:44 29 4
gpt4 key购买 nike

我想运行单元测试,但我需要一个 org.apache.hadoop.fs.FileSystem 实例。是否有用于创建文件系统的任何模拟或任何其他解决方案?

最佳答案

如果您使用的是 hadoop 2.0.0 及更高版本 - 考虑使用 hadoop-minicluster

<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-minicluster</artifactId>
<version>2.5.0</version>
<scope>test</scope>
</dependency>

有了它,你可以在你的本地机器上创建一个临时的 hdfs,并在上面运行你的测试。 setUp 方法可能如下所示:

baseDir = Files.createTempDirectory("test_hdfs").toFile().getAbsoluteFile();
Configuration conf = new Configuration();
conf.set(MiniDFSCluster.HDFS_MINIDFS_BASEDIR, baseDir.getAbsolutePath());
MiniDFSCluster.Builder builder = new MiniDFSCluster.Builder(conf);
hdfsCluster = builder.build();

String hdfsURI = "hdfs://localhost:"+ hdfsCluster.getNameNodePort() + "/";
DistributedFileSystem fileSystem = hdfsCluster.getFileSystem();

在 tearDown 方法中,您应该关闭您的迷你 hdfs 集群,并删除临时目录。

hdfsCluster.shutdown();
FileUtil.fullyDelete(baseDir);

关于unit-testing - Hadoop:如何对文件系统进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8308828/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com