Index: hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestSnapshotFromClient.java =================================================================== --- hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestSnapshotFromClient.java (revision 1483440) +++ hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestSnapshotFromClient.java (working copy) @@ -17,12 +17,14 @@ */ package org.apache.hadoop.hbase.client; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; import java.io.IOException; import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.regex.Pattern; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -141,6 +143,43 @@ } /** + * Test HBaseAdmin#deleteSnapshots(String) which deletes snapshots whose names match the parameter + * + * @throws Exception + */ + @Test + public void testSnapshotDeletionWithRegex() throws Exception { + HBaseAdmin admin = UTIL.getHBaseAdmin(); + // make sure we don't fail on listing snapshots + SnapshotTestingUtils.assertNoSnapshots(admin); + + // put some stuff in the table + HTable table = new HTable(UTIL.getConfiguration(), TABLE_NAME); + UTIL.loadTable(table, TEST_FAM); + table.close(); + + byte[] snapshot1 = Bytes.toBytes("TableSnapshot1"); + admin.snapshot(snapshot1, TABLE_NAME); + LOG.debug("Snapshot1 completed."); + + byte[] snapshot2 = Bytes.toBytes("TableSnapshot2"); + admin.snapshot(snapshot2, TABLE_NAME); + LOG.debug("Snapshot2 completed."); + + String snapshot3 = "3rdTableSnapshot"; + admin.snapshot(Bytes.toBytes(snapshot3), TABLE_NAME); + LOG.debug(snapshot3 + " completed."); + + // delete the first two snapshots + admin.deleteSnapshots("TableSnapshot.*"); + List snapshots = admin.listSnapshots(); + assertEquals(1, snapshots.size()); + assertEquals(snapshots.get(0).getName(), snapshot3); + + admin.deleteSnapshot(snapshot3); + admin.close(); + } + /** * Test snapshotting a table that is offline * @throws Exception */ Index: hbase-client/src/main/java/org/apache/hadoop/hbase/client/HBaseAdmin.java =================================================================== --- hbase-client/src/main/java/org/apache/hadoop/hbase/client/HBaseAdmin.java (revision 1483440) +++ hbase-client/src/main/java/org/apache/hadoop/hbase/client/HBaseAdmin.java (working copy) @@ -2553,6 +2553,35 @@ } /** + * List all the completed snapshots matching the given regular expression. + * + * @param regex The regular expression to match against + * @return - returns a List of SnapshotDescription + * @throws IOException if a remote or network exception occurs + */ + public List listSnapshots(String regex) throws IOException { + return listSnapshots(Pattern.compile(regex)); + } + + /** + * List all the completed snapshots matching the given pattern. + * + * @param pattern The compiled regular expression to match against + * @return - returns a List of SnapshotDescription + * @throws IOException if a remote or network exception occurs + */ + public List listSnapshots(Pattern pattern) throws IOException { + List matched = new LinkedList(); + List snapshots = listSnapshots(); + for (SnapshotDescription snapshot : snapshots) { + if (pattern.matcher(snapshot.getName()).matches()) { + matched.add(snapshot); + } + } + return matched; + } + + /** * Delete an existing snapshot. * @param snapshotName name of the snapshot * @throws IOException if a remote or network exception occurs @@ -2583,6 +2612,36 @@ } /** + * Delete existing snapshots whose names match the pattern passed. + * @param regex The regular expression to match against + * @throws IOException if a remote or network exception occurs + */ + public void deleteSnapshots(final String regex) throws IOException { + deleteSnapshots(Pattern.compile(regex)); + } + + /** + * Delete existing snapshots whose names match the pattern passed. + * @param pattern pattern for names of the snapshot to match + * @throws IOException if a remote or network exception occurs + */ + public void deleteSnapshots(final Pattern pattern) throws IOException { + List snapshots = listSnapshots(pattern); + for (final SnapshotDescription snapshot : snapshots) { + // do the delete + execute(new MasterAdminCallable() { + @Override + public Void call() throws ServiceException { + masterAdmin.deleteSnapshot( + null, + DeleteSnapshotRequest.newBuilder().setSnapshot(snapshot).build()); + return null; + } + }); + } + } + + /** * @see {@link #execute(MasterAdminCallable)} */ private abstract static class MasterAdminCallable implements Callable{