Birim testini çalıştırmak istiyorum ancak org.apache.hadoop.fs.FileSystem örneğim olması gerekiyor. Dosya sistemi oluşturmak için herhangi bir sahte veya başka bir çözüm var mı?Hadoop: Birim testi nasıl yapılır FileSystem
cevap
o size Hadoop 2.0.0 kullanıyorsanız Hadoop
Olası bir yol, TemporaryFolder'un Junit 4.7'de kullanılmasıdır.
Bkz .: http://www.infoq.com/news/2009/07/junit-4.7-rules veya http://weblogs.java.net/blog/johnsmart/archive/2009/09/29/working-temporary-files-junit-47.
Yaptığım şey (daha iyi bir çözüm bulana kadar) FileSystem'i genişlettim.
Niçin dosya sisteminizi FileSystem ile eşleştirmek için Mockito veya PowerMock gibi alaycı bir çerçeve kullanmıyorsunuz? Ünite testleriniz gerçek bir FileSystem'e bağlı olmamalıdır, ancak sadece kodunuzdaki davranışları FileSystem ile etkileşimde doğrulamalıdır.
olmadan test etmek ve böylece bir MiniDFSCluster ve MiniMRCluster kurmak için sınıflandırılır etti
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-test</artifactId>
<version>0.20.205.0</version>
</dependency>
Hadoop-testi kavanoz bir göz atın yukarıdaki - yerel makinede geçici HDF'ler oluşturmak ve üzerinde sizin testleri çalıştırabilirsiniz, bununla beraber
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-minicluster</artifactId>
<version>2.5.0</version>
<scope>test</scope>
</dependency>
bir Hadoop-minicluster kullanmayı düşünün.
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();
Ve tearDown yönteminde Eğer mini HDF'ler küme kapatıldı olmalı ve geçici dizini kaldırın: Bir kurulum yöntem bu gibi görünebilir.
hdfsCluster.shutdown();
FileUtil.fullyDelete(baseDir);
RawLocalFileSystem'a bir göz atmak isteyebilirsiniz. Yine de, sadece alay etsen iyi olur.
Sen HBaseTestingUtility kullanabilirsiniz:
public class SomeTest {
private HBaseTestingUtility testingUtil = new HBaseTestingUtility();
@Before
public void setup() throws Exception {
testingUtil.startMiniDFSCluster(1);
}
@After
public void tearDown() throws IOException {
testingUtil.shutdownMiniDFSCluster();
}
@Test
public void test() throws Exception {
DistributedFileSystem fs = testingUtil.getDFSCluster().getFileSystem();
final Path dstPath = new Path("/your/path/file.txt);
final Path srcPath = new Path(SomeTest.class.getResource("file.txt").toURI());
fs.copyFromLocalFile(srcPath, dstPath);
...
}
}