HBASE-14007 Writing to table through MR should fail upfront if table does not exist/is disabled (Thiruvel Thirumoolan)
This commit is contained in:
parent
385a6e878f
commit
34d62ecfae
|
@ -29,6 +29,9 @@ import org.apache.hadoop.conf.Configuration;
|
|||
import org.apache.hadoop.hbase.HBaseConfiguration;
|
||||
import org.apache.hadoop.hbase.HConstants;
|
||||
import org.apache.hadoop.hbase.TableName;
|
||||
import org.apache.hadoop.hbase.TableNotEnabledException;
|
||||
import org.apache.hadoop.hbase.TableNotFoundException;
|
||||
import org.apache.hadoop.hbase.client.Admin;
|
||||
import org.apache.hadoop.hbase.client.BufferedMutator;
|
||||
import org.apache.hadoop.hbase.client.Connection;
|
||||
import org.apache.hadoop.hbase.client.ConnectionFactory;
|
||||
|
@ -154,7 +157,7 @@ implements Configurable {
|
|||
}
|
||||
|
||||
/**
|
||||
* Checks if the output target exists.
|
||||
* Checks if the output table exists and is enabled.
|
||||
*
|
||||
* @param context The current context.
|
||||
* @throws IOException When the check fails.
|
||||
|
@ -164,8 +167,19 @@ implements Configurable {
|
|||
@Override
|
||||
public void checkOutputSpecs(JobContext context) throws IOException,
|
||||
InterruptedException {
|
||||
// TODO Check if the table exists?
|
||||
|
||||
try (Admin admin = ConnectionFactory.createConnection(getConf()).getAdmin()) {
|
||||
TableName tableName = TableName.valueOf(this.conf.get(OUTPUT_TABLE));
|
||||
if (!admin.tableExists(tableName)) {
|
||||
throw new TableNotFoundException("Can't write, table does not exist:" +
|
||||
tableName.getNameAsString());
|
||||
}
|
||||
|
||||
if (!admin.isTableEnabled(tableName)) {
|
||||
throw new TableNotEnabledException("Can't write, table is not enabled: " +
|
||||
tableName.getNameAsString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -31,6 +31,10 @@ import org.apache.commons.logging.Log;
|
|||
import org.apache.commons.logging.LogFactory;
|
||||
import org.apache.hadoop.fs.FileUtil;
|
||||
import org.apache.hadoop.fs.Path;
|
||||
import org.apache.hadoop.hbase.TableName;
|
||||
import org.apache.hadoop.hbase.TableNotEnabledException;
|
||||
import org.apache.hadoop.hbase.TableNotFoundException;
|
||||
import org.apache.hadoop.hbase.client.Admin;
|
||||
import org.apache.hadoop.hbase.testclassification.LargeTests;
|
||||
import org.apache.hadoop.hbase.client.HTable;
|
||||
import org.apache.hadoop.hbase.client.Put;
|
||||
|
@ -40,6 +44,7 @@ import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
|
|||
import org.apache.hadoop.hbase.util.Bytes;
|
||||
import org.apache.hadoop.mapreduce.Job;
|
||||
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
|
||||
import org.junit.Test;
|
||||
import org.junit.experimental.categories.Category;
|
||||
|
||||
/**
|
||||
|
@ -124,4 +129,25 @@ public class TestTableMapReduce extends TestTableMapReduceBase {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = TableNotEnabledException.class)
|
||||
public void testWritingToDisabledTable() throws IOException {
|
||||
|
||||
try (Admin admin = UTIL.getConnection().getAdmin();
|
||||
HTable table = new HTable(UTIL.getConfiguration(), TABLE_FOR_NEGATIVE_TESTS)) {
|
||||
admin.disableTable(table.getName());
|
||||
runTestOnTable(table);
|
||||
fail("Should not have reached here, should have thrown an exception");
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = TableNotFoundException.class)
|
||||
public void testWritingToNonExistentTable() throws IOException {
|
||||
|
||||
try (HTable table = new HTable(UTIL.getConfiguration(),
|
||||
TableName.valueOf("table-does-not-exist"))) {
|
||||
runTestOnTable(table);
|
||||
fail("Should not have reached here, should have thrown an exception");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -59,6 +59,7 @@ public abstract class TestTableMapReduceBase {
|
|||
withTimeout(this.getClass()).withLookingForStuckThread(true).build();
|
||||
protected static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
|
||||
protected static final TableName MULTI_REGION_TABLE_NAME = TableName.valueOf("mrtest");
|
||||
protected static final TableName TABLE_FOR_NEGATIVE_TESTS = TableName.valueOf("testfailuretable");
|
||||
protected static final byte[] INPUT_FAMILY = Bytes.toBytes("contents");
|
||||
protected static final byte[] OUTPUT_FAMILY = Bytes.toBytes("text");
|
||||
|
||||
|
@ -85,10 +86,12 @@ public abstract class TestTableMapReduceBase {
|
|||
UTIL.createMultiRegionTable(MULTI_REGION_TABLE_NAME, new byte[][] { INPUT_FAMILY,
|
||||
OUTPUT_FAMILY });
|
||||
UTIL.loadTable(table, INPUT_FAMILY, false);
|
||||
UTIL.createTable(TABLE_FOR_NEGATIVE_TESTS, new byte[][] { INPUT_FAMILY, OUTPUT_FAMILY });
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void afterClass() throws Exception {
|
||||
UTIL.deleteTable(TABLE_FOR_NEGATIVE_TESTS);
|
||||
UTIL.shutdownMiniCluster();
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue