Split implements Command { @Override public void run() throws IOException, DecoderException { String action = ((String) args.getOptionSet().nonOptionArguments().get(2)).toLowerCase(); SplitAction splitAction = SplitAction.valueOf(action); Set<String> tableSet = tableSet(admin, args); Map<String, List<byte[]>> splitMap = splitAction.split(tableSet, args); if (!args.isForceProceed() && !Util.askProceed()) return; for (Map.Entry<String, List<byte[]>> entry : splitMap.entrySet()) { String tableName = entry.getKey(); try (HTable table = new HTable(admin.getConfiguration(), tableName)) { for (byte[] splitPoint : entry.getValue()) { System.out.print("splitting - " + tableName + " - " + Bytes.toStringBinary(splitPoint)); try { for (int i = 0; i < Constant.TRY_MAX; i++) { try { split(table, splitPoint); break; } catch (NotServingRegionException e) { Thread.sleep(Constant.WAIT_INTERVAL_MS); } if (i == Constant.TRY_MAX - 1) throw new RuntimeException("Splitting is not finished in " + (Constant.TRY_MAX * Constant.WAIT_INTERVAL_MS / 1000) + " seconds."); } } catch (IOException e) { if (!e.getMessage().contains("should not give a splitkey which equals to startkey")) throw e; System.out.println(" - SKIPPING"); continue; } System.out.println(" - OK"); } } catch (InterruptedException e) { e.printStackTrace(); } } } Split(HBaseAdmin admin, Args args); static String usage(); @Override void run();  }
@Test public void testSplitWithFile() throws Exception { final String keyFileName = "exportkeys_test.keys"; List<HRegionInfo> regionInfoList; try { { splitTable("a".getBytes()); splitTable("b".getBytes()); waitForSplitting(3); String[] argsParam = {"zookeeper", tableName, keyFileName, "--force-proceed"}; Args args = new ManagerArgs(argsParam); assertEquals("zookeeper", args.getZookeeperQuorum()); ExportKeys command = new ExportKeys(admin, args); command.run(); regionInfoList = getRegionInfoList(tableName); mergeRegion(regionInfoList.get(0), regionInfoList.get(1)); regionInfoList = getRegionInfoList(tableName); mergeRegion(regionInfoList.get(0), regionInfoList.get(1)); assertEquals(1, getRegionInfoList(tableName).size()); } String[] argsParam = {"zookeeper", tableName, "filE", keyFileName, "--force-proceed"}; Args args = new ManagerArgs(argsParam); assertEquals("zookeeper", args.getZookeeperQuorum()); Split command = new Split(admin, args); command.run(); waitForSplitting(3); regionInfoList = getRegionInfoList(tableName); assertEquals(3, regionInfoList.size()); } finally { Files.delete(Paths.get(keyFileName)); } }
@Test public void testSplitWithDecimalString() throws Exception { List<HRegionInfo> regionInfoList; regionInfoList = getRegionInfoList(tableName); assertEquals(1, regionInfoList.size()); String[] argsParam = {"zookeeper", tableName, "Rule", "decimalString", "3", "10", "--force-proceed"}; Args args = new ManagerArgs(argsParam); assertEquals("zookeeper", args.getZookeeperQuorum()); Split command = new Split(admin, args); command.run(); waitForSplitting(3); regionInfoList = getRegionInfoList(tableName); assertEquals(3, regionInfoList.size()); for (HRegionInfo hRegionInfo : regionInfoList) { byte[] startKey = hRegionInfo.getStartKey(); if (startKey.length > 0) { assertTrue(Bytes.toString(startKey).matches("[0-9]*")); assertFalse(Bytes.toString(startKey).matches("[A-Za-z]*")); } } }
@Test public void testSplitFileRegex() throws Exception { String keyFileName = "exportkeys_test.keys"; List<HRegionInfo> regionInfoList; byte[] splitPoint = "splitpoint".getBytes(); splitTable(splitPoint); byte[] splitPoint2 = "splitpoint2".getBytes(); String tableName2 = createAdditionalTable(tableName + "2"); splitTable(tableName2, splitPoint2); byte[] splitPoint31 = Bytes.toBytes(3100L); byte[] splitPoint32 = Bytes.toBytes(3200L); String tableName3 = createAdditionalTable(tableName + "22"); splitTable(tableName3, splitPoint31); splitTable(tableName3, splitPoint32); try { String tableNameRegex = tableName + ".*"; String[] argsParam = {"zookeeper", tableNameRegex, keyFileName}; Args args = new ManagerArgs(argsParam); Assert.assertEquals("zookeeper", args.getZookeeperQuorum()); Command command = new ExportKeys(admin, args); waitForSplitting(2); waitForSplitting(tableName2, 2); waitForSplitting(tableName3, 3); command.run(); regionInfoList = getRegionInfoList(tableName); mergeRegion(regionInfoList.get(0), regionInfoList.get(1)); assertEquals(1, getRegionInfoList(tableName).size()); regionInfoList = getRegionInfoList(tableName2); mergeRegion(tableName2, regionInfoList.get(0), regionInfoList.get(1)); assertEquals(1, getRegionInfoList(tableName2).size()); regionInfoList = getRegionInfoList(tableName3); mergeRegion(tableName3, regionInfoList.get(0), regionInfoList.get(1)); regionInfoList = getRegionInfoList(tableName3); mergeRegion(tableName3, regionInfoList.get(0), regionInfoList.get(1)); assertEquals(1, getRegionInfoList(tableName3).size()); tableNameRegex = tableName2 + ".*"; String[] argsParam2 = {"zookeeper", tableNameRegex, "filE", keyFileName, "--force-proceed"}; Args args2 = new ManagerArgs(argsParam2); assertEquals("zookeeper", args2.getZookeeperQuorum()); command = new Split(admin, args2); command.run(); waitForSplitting(tableName2, 2); waitForSplitting(tableName3, 3); ArrayList<HRegionInfo> regionInfoList1 = getRegionInfoList(tableName); ArrayList<HRegionInfo> regionInfoList2 = getRegionInfoList(tableName2); ArrayList<HRegionInfo> regionInfoList3 = getRegionInfoList(tableName3); assertEquals(1, regionInfoList1.size()); assertEquals(2, regionInfoList2.size()); assertArrayEquals(splitPoint2, regionInfoList2.get(1).getStartKey()); assertEquals(3, regionInfoList3.size()); assertArrayEquals(splitPoint31, regionInfoList3.get(1).getStartKey()); assertArrayEquals(splitPoint32, regionInfoList3.get(2).getStartKey()); } finally { Path filePath = Paths.get(keyFileName); if (Files.exists(filePath)) Files.delete(filePath); } }