miniCluster位置:
在你项目下的build/test/data/dfs/name1/current/
有data 和 name1 name2 name3 等等
image文件就在name下的current下
nimiCluster搭建与启动:
demo:
public class ImageToHbaseTest {
private static Configuration conf;
private static MiniDFSCluster dfsCluster;
private static DistributedFileSystem dfs;
@BeforeClass
public static void setUpClass() throws IOException {
System.out.println("before class");
conf = new Configuration();
conf.set(HDFS_IMAGE_INPUT_DIR_KEY, imagePath);
conf.set(HdfsRaidConfigKeys.HDFS_IMAGE_OUTPUT_DIR_KEY, resultDir);
conf.set(HdfsRaidConfigKeys.HDFS_IMAGE_OUTPUT_FILE_KEY, resultFile);
dfsCluster = new MiniDFSCluster.Builder(conf).build(); // 建立mini cluster
dfsCluster.waitActive(); // ?
dfs = dfsCluster.getFileSystem();
fs = dfsCluster.getFileSystem();
FSUtils.init(conf); // 一些工具
RaidTestUtil.init(dfsCluster, conf);
}
@Before
public void before() throws IOException { //因为miniCluster每启一次就要重新构造一次,
//所以放在before中,其他Test都能用。
dfs.mkdirs(new Path("/foo")); //建立一个目录
dfs.setQuota(new Path("/foo"), 1000, 100); //给这个目录设quota
dfs.mkdirs(new Path("/user"));
dfs.setQuota(new Path("/user") ,2000,200);
//这里要是不建这两个目录的话,就只有一个根目录。
}
@AfterClass
public static void tearDownClass() throws Exception {
System.out.println("after class");
dfsCluster.shutdown();
}
@Test
public void testQuota2Hbase() throws Exception {
List<String> qthFiles = new ArrayList<String>();
runAnalyzer();
Assert.assertEquals(qthFiles, list);
}
private void runAnalyzer() throws Exception {
RaidTestUtil.saveImageToPath(dfsCluster, dfs, imagePath);
ImageToHbase imageToHbase = new ImageToHbase();
imageToHbase.init(conf);
ImageReader imageReader = new ImageReader(imageToHbase, conf);
imageReader.run();
list = imageToHbase.getList();
}
}
这个miniCluster的用法,build起来之后,直接用fs或者dfs来调就好了,用法很简单。
但是如果不知道的话,看别人的代码还是有些吃力。