ExportKeys implements Command { @Override public void run() throws Exception { try (PrintWriter writer = new PrintWriter(outputFileName, Constant.CHARSET.name())) { writer.print(""); TableInfo tableInfo = new TableInfo(admin, args.getTableName(), args); tableInfo.refresh(); Set<HRegionInfo> regions = tableInfo.getRegionInfoSet(); int exported = 0, storeFileSizeMBSum = 0; for (HRegionInfo regionInfo : regions) { System.out.println(regionInfo.toString()); if (args.getOptionSet().has(Args.OPTION_OPTIMIZE)) { storeFileSizeMBSum += tableInfo.getRegionLoad(regionInfo).getStorefileSizeMB(); if (storeFileSizeMBSum < exportThreshold) { continue; } } if (export(writer, regionInfo)) exported++; storeFileSizeMBSum = 0; } System.out.println("\n" + regions.size() + " regions are scanned."); System.out.println(exported + " startkeys are exported."); System.out.println("\n" + outputFileName + " file is successfully created."); } } ExportKeys(HBaseAdmin admin, Args args); static String usage(); @Override void run();  }
@Test public void testRun() throws Exception { String outputFile = "exportkeys_test.keys"; try { byte[] splitPoint = "splitpoint".getBytes(); splitTable(splitPoint); String[] argsParam = {"zookeeper", tableName, outputFile}; Args args = new ManagerArgs(argsParam); assertEquals("zookeeper", args.getZookeeperQuorum()); ExportKeys command = new ExportKeys(admin, args); waitForSplitting(2); command.run(); int i = 0; List<Triple<String, String, String>> results = new ArrayList<>(); for (String keys : Files.readAllLines(Paths.get(outputFile), Constant.CHARSET)) { i++; String[] split = keys.split(ExportKeys.DELIMITER); results.add(new ImmutableTriple<>(split[0], split[1], split[2])); } assertEquals(2, i); assertEquals(tableName, results.get(0).getLeft().trim()); assertArrayEquals("".getBytes(), Bytes.toBytesBinary(results.get(0).getMiddle().trim())); assertArrayEquals(splitPoint, Bytes.toBytesBinary(results.get(0).getRight().trim())); assertEquals(tableName, results.get(1).getLeft().trim()); assertArrayEquals(splitPoint, Bytes.toBytesBinary(results.get(1).getMiddle().trim())); assertArrayEquals("".getBytes(), Bytes.toBytesBinary(results.get(1).getRight().trim())); byte[] splitPoint2 = Bytes.toBytes(100L); splitTable(splitPoint2); command.run(); i = 0; results.clear(); for (String keys : Files.readAllLines(Paths.get(outputFile), Constant.CHARSET)) { i++; String[] split = keys.split(ExportKeys.DELIMITER); results.add(new ImmutableTriple<>(split[0], split[1], split[2])); } assertEquals(3, i); assertEquals(tableName, results.get(0).getLeft().trim()); assertArrayEquals("".getBytes(), Bytes.toBytesBinary(results.get(0).getMiddle().trim())); assertArrayEquals(splitPoint2, Bytes.toBytesBinary(results.get(0).getRight().trim())); assertEquals(tableName, results.get(1).getLeft().trim()); assertArrayEquals(splitPoint2, Bytes.toBytesBinary(results.get(1).getMiddle().trim())); assertArrayEquals(splitPoint, Bytes.toBytesBinary(results.get(1).getRight().trim())); assertEquals(tableName, results.get(2).getLeft().trim()); assertArrayEquals(splitPoint, Bytes.toBytesBinary(results.get(2).getMiddle().trim())); assertArrayEquals("".getBytes(), Bytes.toBytesBinary(results.get(2).getRight().trim())); } finally { Files.delete(Paths.get(outputFile)); } }
@Test public void testRunOptimize() throws Exception { String outputFile = "exportkeys_test.keys"; try { String splitPoint = "splitpoint"; splitTable(splitPoint.getBytes()); String[] argsParam = {"zookeeper", tableName, outputFile, "--optimize=1g"}; Args args = new ManagerArgs(argsParam); assertEquals("zookeeper", args.getZookeeperQuorum()); ExportKeys command = new ExportKeys(admin, args); waitForSplitting(2); command.run(); List<Triple<String, String, String>> results = new ArrayList<>(); for (String keys : Files.readAllLines(Paths.get(outputFile), Constant.CHARSET)) { String[] split = keys.split(ExportKeys.DELIMITER); results.add(new ImmutableTriple<>(split[0], split[1], split[2])); } assertEquals(0, results.size()); } finally { Files.delete(Paths.get(outputFile)); } }
@Test public void testRegex() throws Exception { String outputFile = "exportkeys_test.keys"; try { String splitPoint = "splitpoint"; splitTable(splitPoint.getBytes()); String tableName2 = createAdditionalTable(tableName + "2"); splitTable(tableName2, splitPoint.getBytes()); String tableNameRegex = tableName + ".*"; String[] argsParam = {"zookeeper", tableNameRegex, outputFile}; Args args = new ManagerArgs(argsParam); assertEquals("zookeeper", args.getZookeeperQuorum()); ExportKeys command = new ExportKeys(admin, args); waitForSplitting(2); waitForSplitting(tableName2, 2); command.run(); List<Triple<String, String, String>> results = new ArrayList<>(); for (String keys : Files.readAllLines(Paths.get(outputFile), Constant.CHARSET)) { String[] split = keys.split(ExportKeys.DELIMITER); results.add(new ImmutableTriple<>(split[0], split[1], split[2])); } assertEquals(4, results.size()); } finally { Files.delete(Paths.get(outputFile)); } }
@Test public void testRegexAll() throws Exception { if (miniCluster) { String outputFile = "exportkeys_test.keys"; try { String splitPoint = "splitpoint"; splitTable(splitPoint.getBytes()); String tableName2 = createAdditionalTable(tableName + "2"); splitTable(tableName2, splitPoint.getBytes()); String[] argsParam = {"zookeeper", ".*", outputFile}; Args args = new ManagerArgs(argsParam); assertEquals("zookeeper", args.getZookeeperQuorum()); ExportKeys command = new ExportKeys(admin, args); waitForSplitting(2); waitForSplitting(tableName2, 2); command.run(); List<Triple<String, String, String>> results = new ArrayList<>(); for (String keys : Files.readAllLines(Paths.get(outputFile), Constant.CHARSET)) { String[] split = keys.split(ExportKeys.DELIMITER); results.add(new ImmutableTriple<>(split[0], split[1], split[2])); } assertEquals(4, results.size()); } finally { Files.delete(Paths.get(outputFile)); } } }
@Test public void testInvalidTable() throws Exception { String[] argsParam = {"zookeeper"}; Args args = new ManagerArgs(argsParam); assertEquals("zookeeper", args.getZookeeperQuorum()); try { ExportKeys command = new ExportKeys(admin, args); command.run(); fail(); } catch (IllegalArgumentException e) { if (!e.getMessage().contains(Args.INVALID_ARGUMENTS)) throw e; } }