HBASE-18433 Convenience method for creating simple ColumnFamilyDescriptor

Signed-off-by: Chia-Ping Tsai <chia7712@gmail.com>
This commit is contained in:
Mike Drob 2017-07-21 23:05:21 -05:00 committed by Chia-Ping Tsai
parent ec3cb19664
commit 317ce73963
13 changed files with 40 additions and 28 deletions

View File

@ -104,6 +104,7 @@ public class HColumnDescriptor implements ColumnFamilyDescriptor, Comparable<HCo
* *
* @param familyName Column family name. Must be 'printable' -- digit or * @param familyName Column family name. Must be 'printable' -- digit or
* letter -- and may not contain a <code>:</code> * letter -- and may not contain a <code>:</code>
* @deprecated use {@link ColumnFamilyDescriptorBuilder#of(String)}
*/ */
public HColumnDescriptor(final String familyName) { public HColumnDescriptor(final String familyName) {
this(Bytes.toBytes(familyName)); this(Bytes.toBytes(familyName));
@ -115,6 +116,7 @@ public class HColumnDescriptor implements ColumnFamilyDescriptor, Comparable<HCo
* *
* @param familyName Column family name. Must be 'printable' -- digit or * @param familyName Column family name. Must be 'printable' -- digit or
* letter -- and may not contain a <code>:</code> * letter -- and may not contain a <code>:</code>
* @deprecated use {@link ColumnFamilyDescriptorBuilder#of(byte[])}
*/ */
public HColumnDescriptor(final byte [] familyName) { public HColumnDescriptor(final byte [] familyName) {
this(new ModifyableColumnFamilyDescriptor(familyName)); this(new ModifyableColumnFamilyDescriptor(familyName));
@ -125,6 +127,7 @@ public class HColumnDescriptor implements ColumnFamilyDescriptor, Comparable<HCo
* Makes a deep copy of the supplied descriptor. * Makes a deep copy of the supplied descriptor.
* Can make a modifiable descriptor from an UnmodifyableHColumnDescriptor. * Can make a modifiable descriptor from an UnmodifyableHColumnDescriptor.
* @param desc The descriptor. * @param desc The descriptor.
* @deprecated use {@link ColumnFamilyDescriptorBuilder#copy(ColumnFamilyDescriptor)}
*/ */
public HColumnDescriptor(HColumnDescriptor desc) { public HColumnDescriptor(HColumnDescriptor desc) {
this(desc, true); this(desc, true);

View File

@ -32,6 +32,8 @@ import org.apache.hadoop.hbase.util.Bytes;
* number of versions, compression settings, etc. * number of versions, compression settings, etc.
* *
* It is used as input when creating a table or adding a column. * It is used as input when creating a table or adding a column.
*
* To construct a new instance, use the {@link ColumnFamilyDescriptorBuilder} methods
*/ */
@InterfaceAudience.Public @InterfaceAudience.Public
public interface ColumnFamilyDescriptor { public interface ColumnFamilyDescriptor {

View File

@ -376,6 +376,14 @@ public class ColumnFamilyDescriptorBuilder {
return new ModifyableColumnFamilyDescriptor(desc); return new ModifyableColumnFamilyDescriptor(desc);
} }
public static ColumnFamilyDescriptor of(String name) {
return of(Bytes.toBytes(name));
}
public static ColumnFamilyDescriptor of(byte[] name) {
return newBuilder(name).build();
}
private ColumnFamilyDescriptorBuilder(final byte[] name) { private ColumnFamilyDescriptorBuilder(final byte[] name) {
this.desc = new ModifyableColumnFamilyDescriptor(name); this.desc = new ModifyableColumnFamilyDescriptor(name);
} }

View File

@ -92,7 +92,7 @@ public class TestColumnFamilyDescriptorBuilder {
public void testHColumnDescriptorShouldThrowIAEWhenFamiliyNameEmpty() public void testHColumnDescriptorShouldThrowIAEWhenFamiliyNameEmpty()
throws Exception { throws Exception {
try { try {
ColumnFamilyDescriptorBuilder.newBuilder("".getBytes()).build(); ColumnFamilyDescriptorBuilder.of("");
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
assertEquals("Column Family name can not be empty", e.getLocalizedMessage()); assertEquals("Column Family name can not be empty", e.getLocalizedMessage());
} }

View File

@ -382,7 +382,7 @@ public class RegionSplitter {
TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(tableName); TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(tableName);
for (String cf : columnFamilies) { for (String cf : columnFamilies) {
builder.addColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(cf)).build()); builder.addColumnFamily(ColumnFamilyDescriptorBuilder.of(cf));
} }
try (Connection connection = ConnectionFactory.createConnection(conf)) { try (Connection connection = ConnectionFactory.createConnection(conf)) {
Admin admin = connection.getAdmin(); Admin admin = connection.getAdmin();

View File

@ -133,7 +133,7 @@ public abstract class TestAsyncAdminBase {
byte[]... families) { byte[]... families) {
TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(tableName); TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(tableName);
for (byte[] family : families) { for (byte[] family : families) {
builder.addColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(family).build()); builder.addColumnFamily(ColumnFamilyDescriptorBuilder.of(family));
} }
admin.createTable(builder.build(), splitKeys).join(); admin.createTable(builder.build(), splitKeys).join();
} }

View File

@ -284,7 +284,7 @@ public class TestAsyncClusterAdminApi extends TestAsyncAdminBase {
private void createAndLoadTable(TableName[] tables) { private void createAndLoadTable(TableName[] tables) {
for (TableName table : tables) { for (TableName table : tables) {
TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(table); TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(table);
builder.addColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(FAMILY).build()); builder.addColumnFamily(ColumnFamilyDescriptorBuilder.of(FAMILY));
admin.createTable(builder.build(), Bytes.toBytes("aaaaa"), Bytes.toBytes("zzzzz"), 16).join(); admin.createTable(builder.build(), Bytes.toBytes("aaaaa"), Bytes.toBytes("zzzzz"), 16).join();
RawAsyncTable asyncTable = ASYNC_CONN.getRawTable(table); RawAsyncTable asyncTable = ASYNC_CONN.getRawTable(table);
List<Put> puts = new ArrayList<>(); List<Put> puts = new ArrayList<>();

View File

@ -66,7 +66,7 @@ public class TestAsyncDrainAdminApi extends TestAsyncAdminBase {
// Try for 20 seconds to create table (new region). Will not complete because all RSs draining. // Try for 20 seconds to create table (new region). Will not complete because all RSs draining.
TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(tableName); TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(tableName);
builder.addColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(FAMILY).build()); builder.addColumnFamily(ColumnFamilyDescriptorBuilder.of(FAMILY));
final Runnable createTable = new Thread() { final Runnable createTable = new Thread() {
@Override @Override
public void run() { public void run() {

View File

@ -188,7 +188,7 @@ public class TestAsyncRegionAdminApi extends TestAsyncAdminBase {
throws IOException, InterruptedException, ExecutionException { throws IOException, InterruptedException, ExecutionException {
TableDescriptor desc = TableDescriptor desc =
TableDescriptorBuilder.newBuilder(tableName) TableDescriptorBuilder.newBuilder(tableName)
.addColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(FAMILY).build()).build(); .addColumnFamily(ColumnFamilyDescriptorBuilder.of(FAMILY)).build();
admin.createTable(desc, Bytes.toBytes("A"), Bytes.toBytes("Z"), 5).get(); admin.createTable(desc, Bytes.toBytes("A"), Bytes.toBytes("Z"), 5).get();
// wait till the table is assigned // wait till the table is assigned

View File

@ -133,7 +133,7 @@ public class TestAsyncTableAdminApi extends TestAsyncAdminBase {
byte[][] families = { FAMILY, FAMILY_0, FAMILY_1 }; byte[][] families = { FAMILY, FAMILY_0, FAMILY_1 };
TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(tableName); TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(tableName);
for (byte[] family : families) { for (byte[] family : families) {
builder.addColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(family).build()); builder.addColumnFamily(ColumnFamilyDescriptorBuilder.of(family));
} }
TableDescriptor desc = builder.build(); TableDescriptor desc = builder.build();
admin.createTable(desc).join(); admin.createTable(desc).join();
@ -178,7 +178,7 @@ public class TestAsyncTableAdminApi extends TestAsyncAdminBase {
final TableName tableName3 = TableName.valueOf(tableName.getNameAsString() + "_3"); final TableName tableName3 = TableName.valueOf(tableName.getNameAsString() + "_3");
TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(tableName3); TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(tableName3);
builder.addColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(FAMILY).build()); builder.addColumnFamily(ColumnFamilyDescriptorBuilder.of(FAMILY));
admin.createTable(builder.build(), "a".getBytes(), "z".getBytes(), 3).join(); admin.createTable(builder.build(), "a".getBytes(), "z".getBytes(), 3).join();
regionLocations = regionLocations =
AsyncMetaTableAccessor.getTableHRegionLocations(metaTable, Optional.of(tableName3)).get(); AsyncMetaTableAccessor.getTableHRegionLocations(metaTable, Optional.of(tableName3)).get();
@ -186,7 +186,7 @@ public class TestAsyncTableAdminApi extends TestAsyncAdminBase {
final TableName tableName4 = TableName.valueOf(tableName.getNameAsString() + "_4"); final TableName tableName4 = TableName.valueOf(tableName.getNameAsString() + "_4");
builder = TableDescriptorBuilder.newBuilder(tableName4); builder = TableDescriptorBuilder.newBuilder(tableName4);
builder.addColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(FAMILY).build()); builder.addColumnFamily(ColumnFamilyDescriptorBuilder.of(FAMILY));
try { try {
admin.createTable(builder.build(), "a".getBytes(), "z".getBytes(), 2).join(); admin.createTable(builder.build(), "a".getBytes(), "z".getBytes(), 2).join();
fail("Should not be able to create a table with only 2 regions using this API."); fail("Should not be able to create a table with only 2 regions using this API.");
@ -196,7 +196,7 @@ public class TestAsyncTableAdminApi extends TestAsyncAdminBase {
final TableName tableName5 = TableName.valueOf(tableName.getNameAsString() + "_5"); final TableName tableName5 = TableName.valueOf(tableName.getNameAsString() + "_5");
builder = TableDescriptorBuilder.newBuilder(tableName5); builder = TableDescriptorBuilder.newBuilder(tableName5);
builder.addColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(FAMILY).build()); builder.addColumnFamily(ColumnFamilyDescriptorBuilder.of(FAMILY));
admin.createTable(builder.build(), new byte[] { 1 }, new byte[] { 127 }, 16).join(); admin.createTable(builder.build(), new byte[] { 1 }, new byte[] { 127 }, 16).join();
regionLocations = regionLocations =
AsyncMetaTableAccessor.getTableHRegionLocations(metaTable, Optional.of(tableName5)).get(); AsyncMetaTableAccessor.getTableHRegionLocations(metaTable, Optional.of(tableName5)).get();
@ -269,7 +269,7 @@ public class TestAsyncTableAdminApi extends TestAsyncAdminBase {
expectedRegions = 10; expectedRegions = 10;
final TableName tableName2 = TableName.valueOf(tableName.getNameAsString() + "_2"); final TableName tableName2 = TableName.valueOf(tableName.getNameAsString() + "_2");
TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(tableName2); TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(tableName2);
builder.addColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(FAMILY).build()); builder.addColumnFamily(ColumnFamilyDescriptorBuilder.of(FAMILY));
admin.createTable(builder.build(), startKey, endKey, expectedRegions).join(); admin.createTable(builder.build(), startKey, endKey, expectedRegions).join();
regions = regions =
@ -319,7 +319,7 @@ public class TestAsyncTableAdminApi extends TestAsyncAdminBase {
expectedRegions = 5; expectedRegions = 5;
final TableName tableName3 = TableName.valueOf(tableName.getNameAsString() + "_3"); final TableName tableName3 = TableName.valueOf(tableName.getNameAsString() + "_3");
builder = TableDescriptorBuilder.newBuilder(tableName3); builder = TableDescriptorBuilder.newBuilder(tableName3);
builder.addColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(FAMILY).build()); builder.addColumnFamily(ColumnFamilyDescriptorBuilder.of(FAMILY));
admin.createTable(builder.build(), startKey, endKey, expectedRegions).join(); admin.createTable(builder.build(), startKey, endKey, expectedRegions).join();
regions = regions =
@ -614,14 +614,14 @@ public class TestAsyncTableAdminApi extends TestAsyncAdminBase {
public void testAddColumnFamily() throws Exception { public void testAddColumnFamily() throws Exception {
// Create a table with two families // Create a table with two families
TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(tableName); TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(tableName);
builder.addColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(FAMILY_0).build()); builder.addColumnFamily(ColumnFamilyDescriptorBuilder.of(FAMILY_0));
admin.createTable(builder.build()).join(); admin.createTable(builder.build()).join();
admin.disableTable(tableName).join(); admin.disableTable(tableName).join();
// Verify the table descriptor // Verify the table descriptor
verifyTableDescriptor(tableName, FAMILY_0); verifyTableDescriptor(tableName, FAMILY_0);
// Modify the table removing one family and verify the descriptor // Modify the table removing one family and verify the descriptor
admin.addColumnFamily(tableName, ColumnFamilyDescriptorBuilder.newBuilder(FAMILY_1).build()) admin.addColumnFamily(tableName, ColumnFamilyDescriptorBuilder.of(FAMILY_1))
.join(); .join();
verifyTableDescriptor(tableName, FAMILY_0, FAMILY_1); verifyTableDescriptor(tableName, FAMILY_0, FAMILY_1);
} }
@ -630,21 +630,20 @@ public class TestAsyncTableAdminApi extends TestAsyncAdminBase {
public void testAddSameColumnFamilyTwice() throws Exception { public void testAddSameColumnFamilyTwice() throws Exception {
// Create a table with one families // Create a table with one families
TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(tableName); TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(tableName);
builder.addColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(FAMILY_0).build()); builder.addColumnFamily(ColumnFamilyDescriptorBuilder.of(FAMILY_0));
admin.createTable(builder.build()).join(); admin.createTable(builder.build()).join();
admin.disableTable(tableName).join(); admin.disableTable(tableName).join();
// Verify the table descriptor // Verify the table descriptor
verifyTableDescriptor(tableName, FAMILY_0); verifyTableDescriptor(tableName, FAMILY_0);
// Modify the table removing one family and verify the descriptor // Modify the table removing one family and verify the descriptor
admin.addColumnFamily(tableName, ColumnFamilyDescriptorBuilder.newBuilder(FAMILY_1).build()) admin.addColumnFamily(tableName, ColumnFamilyDescriptorBuilder.of(FAMILY_1)).join();
.join();
verifyTableDescriptor(tableName, FAMILY_0, FAMILY_1); verifyTableDescriptor(tableName, FAMILY_0, FAMILY_1);
try { try {
// Add same column family again - expect failure // Add same column family again - expect failure
this.admin.addColumnFamily(tableName, this.admin.addColumnFamily(tableName,
ColumnFamilyDescriptorBuilder.newBuilder(FAMILY_1).build()).join(); ColumnFamilyDescriptorBuilder.of(FAMILY_1)).join();
Assert.fail("Delete a non-exist column family should fail"); Assert.fail("Delete a non-exist column family should fail");
} catch (Exception e) { } catch (Exception e) {
// Expected. // Expected.
@ -654,7 +653,7 @@ public class TestAsyncTableAdminApi extends TestAsyncAdminBase {
@Test @Test
public void testModifyColumnFamily() throws Exception { public void testModifyColumnFamily() throws Exception {
TableDescriptorBuilder tdBuilder = TableDescriptorBuilder.newBuilder(tableName); TableDescriptorBuilder tdBuilder = TableDescriptorBuilder.newBuilder(tableName);
ColumnFamilyDescriptor cfd = ColumnFamilyDescriptorBuilder.newBuilder(FAMILY_0).build(); ColumnFamilyDescriptor cfd = ColumnFamilyDescriptorBuilder.of(FAMILY_0);
int blockSize = cfd.getBlocksize(); int blockSize = cfd.getBlocksize();
admin.createTable(tdBuilder.addColumnFamily(cfd).build()).join(); admin.createTable(tdBuilder.addColumnFamily(cfd).build()).join();
admin.disableTable(tableName).join(); admin.disableTable(tableName).join();
@ -674,7 +673,7 @@ public class TestAsyncTableAdminApi extends TestAsyncAdminBase {
@Test @Test
public void testModifyNonExistingColumnFamily() throws Exception { public void testModifyNonExistingColumnFamily() throws Exception {
TableDescriptorBuilder tdBuilder = TableDescriptorBuilder.newBuilder(tableName); TableDescriptorBuilder tdBuilder = TableDescriptorBuilder.newBuilder(tableName);
ColumnFamilyDescriptor cfd = ColumnFamilyDescriptorBuilder.newBuilder(FAMILY_0).build(); ColumnFamilyDescriptor cfd = ColumnFamilyDescriptorBuilder.of(FAMILY_0);
int blockSize = cfd.getBlocksize(); int blockSize = cfd.getBlocksize();
admin.createTable(tdBuilder.addColumnFamily(cfd).build()).join(); admin.createTable(tdBuilder.addColumnFamily(cfd).build()).join();
admin.disableTable(tableName).join(); admin.disableTable(tableName).join();
@ -697,8 +696,8 @@ public class TestAsyncTableAdminApi extends TestAsyncAdminBase {
public void testDeleteColumnFamily() throws Exception { public void testDeleteColumnFamily() throws Exception {
// Create a table with two families // Create a table with two families
TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(tableName); TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(tableName);
builder.addColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(FAMILY_0).build()) builder.addColumnFamily(ColumnFamilyDescriptorBuilder.of(FAMILY_0))
.addColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(FAMILY_1).build()); .addColumnFamily(ColumnFamilyDescriptorBuilder.of(FAMILY_1));
admin.createTable(builder.build()).join(); admin.createTable(builder.build()).join();
admin.disableTable(tableName).join(); admin.disableTable(tableName).join();
// Verify the table descriptor // Verify the table descriptor
@ -713,8 +712,8 @@ public class TestAsyncTableAdminApi extends TestAsyncAdminBase {
public void testDeleteSameColumnFamilyTwice() throws Exception { public void testDeleteSameColumnFamilyTwice() throws Exception {
// Create a table with two families // Create a table with two families
TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(tableName); TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(tableName);
builder.addColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(FAMILY_0).build()) builder.addColumnFamily(ColumnFamilyDescriptorBuilder.of(FAMILY_0))
.addColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(FAMILY_1).build()); .addColumnFamily(ColumnFamilyDescriptorBuilder.of(FAMILY_1));
admin.createTable(builder.build()).join(); admin.createTable(builder.build()).join();
admin.disableTable(tableName).join(); admin.disableTable(tableName).join();
// Verify the table descriptor // Verify the table descriptor

View File

@ -305,7 +305,7 @@ public class TestReplicaWithCluster {
TableDescriptor td = TableDescriptorBuilder.newBuilder(TableName.valueOf("testChangeTable")) TableDescriptor td = TableDescriptorBuilder.newBuilder(TableName.valueOf("testChangeTable"))
.setRegionReplication(NB_SERVERS) .setRegionReplication(NB_SERVERS)
.addCoprocessor(SlowMeCopro.class.getName()) .addCoprocessor(SlowMeCopro.class.getName())
.addColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(f).build()) .addColumnFamily(ColumnFamilyDescriptorBuilder.of(f))
.build(); .build();
HTU.getAdmin().createTable(td); HTU.getAdmin().createTable(td);
Table table = HTU.getConnection().getTable(td.getTableName()); Table table = HTU.getConnection().getTable(td.getTableName());
@ -321,7 +321,7 @@ public class TestReplicaWithCluster {
// Add a CF, it should work. // Add a CF, it should work.
TableDescriptor bHdt = HTU.getAdmin().listTableDescriptor(td.getTableName()); TableDescriptor bHdt = HTU.getAdmin().listTableDescriptor(td.getTableName());
td = TableDescriptorBuilder.newBuilder(td) td = TableDescriptorBuilder.newBuilder(td)
.addColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(row).build()) .addColumnFamily(ColumnFamilyDescriptorBuilder.of(row))
.build(); .build();
HTU.getAdmin().disableTable(td.getTableName()); HTU.getAdmin().disableTable(td.getTableName());
HTU.getAdmin().modifyTable(td); HTU.getAdmin().modifyTable(td);

View File

@ -56,7 +56,7 @@ public class TestResultFromCoprocessor {
TEST_UTIL.startMiniCluster(3); TEST_UTIL.startMiniCluster(3);
TableDescriptor desc = TableDescriptorBuilder.newBuilder(TABLE_NAME) TableDescriptor desc = TableDescriptorBuilder.newBuilder(TABLE_NAME)
.addCoprocessor(MyObserver.class.getName()) .addCoprocessor(MyObserver.class.getName())
.addColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(FAMILY).build()) .addColumnFamily(ColumnFamilyDescriptorBuilder.of(FAMILY))
.build(); .build();
TEST_UTIL.getAdmin().createTable(desc); TEST_UTIL.getAdmin().createTable(desc);
} }

View File

@ -284,7 +284,7 @@ public class TestSnapshotFromMaster {
// snapshot, the call after snapshot will be a no-op and checks will fail // snapshot, the call after snapshot will be a no-op and checks will fail
UTIL.deleteTable(TABLE_NAME); UTIL.deleteTable(TABLE_NAME);
TableDescriptor td = TableDescriptorBuilder.newBuilder(TABLE_NAME) TableDescriptor td = TableDescriptorBuilder.newBuilder(TABLE_NAME)
.addColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(TEST_FAM).build()) .addColumnFamily(ColumnFamilyDescriptorBuilder.of(TEST_FAM))
.setCompactionEnabled(false) .setCompactionEnabled(false)
.build(); .build();
UTIL.getAdmin().createTable(td); UTIL.getAdmin().createTable(td);