2011-11-29 41 views

Trả lời

6

Hãy xem jar hadoop-test

<dependency> 
    <groupId>org.apache.hadoop</groupId> 
    <artifactId>hadoop-test</artifactId> 
    <version>0.20.205.0</version> 
</dependency> 

nó đã phân loại thành lập một MiniDFSCluster và MiniMRCluster vì vậy bạn có thể kiểm tra mà không hadoop

0

Những gì tôi đã làm (cho đến khi tôi tìm ra giải pháp tốt hơn) Tôi đã mở rộng Hệ thống tệp.

5

Tại sao không sử dụng khung mocking như Mockito hoặc PowerMock để giả lập các giao diện của bạn với FileSystem? Các bài kiểm tra đơn vị của bạn không nên phụ thuộc vào một Hệ thống tệp thực tế, nhưng chỉ nên xác minh hành vi trong mã của bạn khi tương tác với Hệ thống tệp.

10

Nếu bạn đang sử dụng hadoop 2.0.0 và ở trên - hãy xem xét sử dụng hadoop-minicluster

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

Với nó, bạn có thể tạo một hdfs tạm thời trên máy cục bộ và chạy thử nghiệm trên đó. Phương thức setUp có thể trông giống như sau:

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(); 

Và trong phương thức tearDown, bạn nên tắt cụm mini hdfs và xóa thư mục tạm thời.

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

Bạn có thể muốn xem xét RawLocalFileSystem. Mặc dù tôi nghĩ tốt hơn là bạn nên thử.

0

Bạn có thể sử dụng HBaseTestingUtility:

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); 
     ... 
    } 
} 
Các vấn đề liên quan