diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/Admin.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/Admin.java index f5da0aa0bde7..7500dd145f0e 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/Admin.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/Admin.java @@ -158,6 +158,13 @@ default List listTableDescriptors(Pattern pattern) throws IOExc List listTableDescriptors(Pattern pattern, boolean includeSysTables) throws IOException; + /** + * List all enabled or disabled tables + * @param isEnabled is true means return enabled tables, false means return disabled tables + * @return a list of enabled or disabled tables + */ + List listTableDescriptorsByState(boolean isEnabled) throws IOException; + /** * List all of the names of userspace tables. * @return TableName[] table names @@ -184,6 +191,14 @@ default TableName[] listTableNames(Pattern pattern) throws IOException { */ TableName[] listTableNames(Pattern pattern, boolean includeSysTables) throws IOException; + /** + * List all enabled or disabled table names + * @param isEnabled is true means return enabled table names, false means return disabled table + * names + * @return a list of enabled or disabled table names + */ + List listTableNamesByState(boolean isEnabled) throws IOException; + /** * Get a table descriptor. * @param tableName as a {@link TableName} diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/AdminOverAsyncAdmin.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/AdminOverAsyncAdmin.java index 9e2b990d91c1..67b0a9f06827 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/AdminOverAsyncAdmin.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/AdminOverAsyncAdmin.java @@ -146,6 +146,11 @@ public List listTableDescriptors(Pattern pattern, boolean inclu return get(admin.listTableDescriptors(pattern, includeSysTables)); } + @Override + public List listTableDescriptorsByState(boolean isEnabled) throws IOException { + return get(admin.listTableDescriptorsByState(isEnabled)); + } + @Override public TableName[] listTableNames() throws IOException { return get(admin.listTableNames()).toArray(new TableName[0]); @@ -156,6 +161,11 @@ public TableName[] listTableNames(Pattern pattern, boolean includeSysTables) thr return get(admin.listTableNames(pattern, includeSysTables)).toArray(new TableName[0]); } + @Override + public List listTableNamesByState(boolean isEnabled) throws IOException { + return get(admin.listTableNamesByState(isEnabled)); + } + @Override public TableDescriptor getDescriptor(TableName tableName) throws TableNotFoundException, IOException { diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncAdmin.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncAdmin.java index 6070c553f5e1..3d0f9c083ebd 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncAdmin.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncAdmin.java @@ -111,6 +111,14 @@ CompletableFuture> listTableDescriptors(Pattern pattern, */ CompletableFuture> listTableDescriptorsByNamespace(String name); + /** + * List all enabled or disabled table descriptors + * @param isEnabled is true means return enabled table descriptors, false means return disabled + * table descriptors + * @return a list of table names wrapped by a {@link CompletableFuture}. + */ + CompletableFuture> listTableDescriptorsByState(boolean isEnabled); + /** * List all of the names of userspace tables. * @return a list of table names wrapped by a {@link CompletableFuture}. @@ -135,6 +143,14 @@ default CompletableFuture> listTableNames() { */ CompletableFuture> listTableNames(Pattern pattern, boolean includeSysTables); + /** + * List all enabled or disabled table names + * @param isEnabled is true means return enabled table names, false means return disabled table + * names + * @return a list of table names wrapped by a {@link CompletableFuture}. + */ + CompletableFuture> listTableNamesByState(boolean isEnabled); + /** * Get list of table names by namespace. * @param name namespace name diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncHBaseAdmin.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncHBaseAdmin.java index a8f93dd506d4..6e914240ba26 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncHBaseAdmin.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncHBaseAdmin.java @@ -104,6 +104,11 @@ public CompletableFuture> listTableDescriptorsByNamespace( return wrap(rawAdmin.listTableDescriptorsByNamespace(name)); } + @Override + public CompletableFuture> listTableDescriptorsByState(boolean isEnabled) { + return wrap(rawAdmin.listTableDescriptorsByState(isEnabled)); + } + @Override public CompletableFuture> listTableNames(boolean includeSysTables) { return wrap(rawAdmin.listTableNames(includeSysTables)); @@ -115,6 +120,11 @@ public CompletableFuture> listTableNames(Pattern pattern, return wrap(rawAdmin.listTableNames(pattern, includeSysTables)); } + @Override + public CompletableFuture> listTableNamesByState(boolean isEnabled) { + return wrap(rawAdmin.listTableNamesByState(isEnabled)); + } + @Override public CompletableFuture> listTableNamesByNamespace(String name) { return wrap(rawAdmin.listTableNamesByNamespace(name)); diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/RawAsyncHBaseAdmin.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/RawAsyncHBaseAdmin.java index 4d614907326e..369f93b0d7bc 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/RawAsyncHBaseAdmin.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/RawAsyncHBaseAdmin.java @@ -223,8 +223,12 @@ import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.ListNamespacesResponse; import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.ListTableDescriptorsByNamespaceRequest; import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.ListTableDescriptorsByNamespaceResponse; +import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.ListTableDescriptorsByStateRequest; +import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.ListTableDescriptorsByStateResponse; import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.ListTableNamesByNamespaceRequest; import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.ListTableNamesByNamespaceResponse; +import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.ListTableNamesByStateRequest; +import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.ListTableNamesByStateResponse; import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.MajorCompactionTimestampForRegionRequest; import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.MajorCompactionTimestampRequest; import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.MajorCompactionTimestampResponse; @@ -561,6 +565,17 @@ public CompletableFuture> listTableNames(Pattern pattern, return getTableNames(RequestConverter.buildGetTableNamesRequest(pattern, includeSysTables)); } + @Override + public CompletableFuture> listTableNamesByState(boolean isEnabled) { + return this.> newMasterCaller() + .action((controller, stub) -> this.> call(controller, stub, + ListTableNamesByStateRequest.newBuilder().setIsEnabled(isEnabled).build(), + (s, c, req, done) -> s.listTableNamesByState(c, req, done), + (resp) -> ProtobufUtil.toTableNameList(resp.getTableNamesList()))) + .call(); + } + private CompletableFuture> getTableNames(GetTableNamesRequest request) { return this.> newMasterCaller() .action((controller, stub) -> this.> call(controller, .call(); } + @Override + public CompletableFuture> listTableDescriptorsByState(boolean isEnabled) { + return this.> newMasterCaller() + .action((controller, stub) -> this.> call(controller, stub, + ListTableDescriptorsByStateRequest.newBuilder().setIsEnabled(isEnabled).build(), + (s, c, req, done) -> s.listTableDescriptorsByState(c, req, done), + (resp) -> ProtobufUtil.toTableDescriptorList(resp))) + .call(); + } + @Override public CompletableFuture> listTableNamesByNamespace(String name) { return this.> newMasterCaller() diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/shaded/protobuf/ProtobufUtil.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/shaded/protobuf/ProtobufUtil.java index 079ddbb4218e..86e15fe3be16 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/shaded/protobuf/ProtobufUtil.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/shaded/protobuf/ProtobufUtil.java @@ -198,6 +198,7 @@ import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.GetTableDescriptorsResponse; import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.ListNamespaceDescriptorsResponse; import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.ListTableDescriptorsByNamespaceResponse; +import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.ListTableDescriptorsByStateResponse; import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.MajorCompactionTimestampResponse; import org.apache.hadoop.hbase.shaded.protobuf.generated.ProcedureProtos; import org.apache.hadoop.hbase.shaded.protobuf.generated.QuotaProtos; @@ -506,6 +507,18 @@ public static List toTableDescriptorList(GetTableDescriptorsRes .collect(Collectors.toList()); } + /** + * Get a list of TableDescriptor from ListTableDescriptorsByNamespaceResponse protobuf + * @param proto the ListTableDescriptorsByNamespaceResponse + * @return a list of TableDescriptor + */ + public static List + toTableDescriptorList(ListTableDescriptorsByStateResponse proto) { + if (proto == null) return new ArrayList<>(); + return proto.getTableSchemaList().stream().map(ProtobufUtil::toTableDescriptor) + .collect(Collectors.toList()); + } + /** * get the split keys in form "byte [][]" from a CreateTableRequest proto * @param proto the CreateTableRequest diff --git a/hbase-protocol-shaded/src/main/protobuf/server/master/Master.proto b/hbase-protocol-shaded/src/main/protobuf/server/master/Master.proto index 257abe8f11ca..1d802712959e 100644 --- a/hbase-protocol-shaded/src/main/protobuf/server/master/Master.proto +++ b/hbase-protocol-shaded/src/main/protobuf/server/master/Master.proto @@ -512,6 +512,14 @@ message GetTableDescriptorsResponse { repeated TableSchema table_schema = 1; } +message ListTableDescriptorsByStateRequest { + required bool is_enabled = 1; +} + +message ListTableDescriptorsByStateResponse { + repeated TableSchema table_schema = 1; +} + message GetTableNamesRequest { optional string regex = 1; optional bool include_sys_tables = 2 [default=false]; @@ -522,6 +530,14 @@ message GetTableNamesResponse { repeated TableName table_names = 1; } +message ListTableNamesByStateRequest { + required bool is_enabled = 1; +} + +message ListTableNamesByStateResponse { + repeated TableName table_names = 1; +} + message GetTableStateRequest { required TableName table_name = 1; } @@ -770,10 +786,18 @@ service MasterService { rpc GetTableDescriptors(GetTableDescriptorsRequest) returns(GetTableDescriptorsResponse); + /** List all enabled or disabled table descriptors. */ + rpc ListTableDescriptorsByState(ListTableDescriptorsByStateRequest) + returns(ListTableDescriptorsByStateResponse); + /** Get the list of table names. */ rpc GetTableNames(GetTableNamesRequest) returns(GetTableNamesResponse); + /** List all enabled or disabled table names. */ + rpc ListTableNamesByState(ListTableNamesByStateRequest) + returns(ListTableNamesByStateResponse); + /** Return cluster status. */ rpc GetClusterStatus(GetClusterStatusRequest) returns(GetClusterStatusResponse); diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/master/MasterRpcServices.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/master/MasterRpcServices.java index 4a490b1e127c..797238062d14 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/master/MasterRpcServices.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/master/MasterRpcServices.java @@ -283,8 +283,12 @@ import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.ListNamespacesResponse; import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.ListTableDescriptorsByNamespaceRequest; import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.ListTableDescriptorsByNamespaceResponse; +import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.ListTableDescriptorsByStateRequest; +import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.ListTableDescriptorsByStateResponse; import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.ListTableNamesByNamespaceRequest; import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.ListTableNamesByNamespaceResponse; +import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.ListTableNamesByStateRequest; +import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.ListTableNamesByStateResponse; import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.MajorCompactionTimestampForRegionRequest; import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.MajorCompactionTimestampRequest; import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos.MajorCompactionTimestampResponse; @@ -1171,6 +1175,31 @@ public GetTableDescriptorsResponse getTableDescriptors(RpcController c, } } + @Override + public ListTableDescriptorsByStateResponse listTableDescriptorsByState(RpcController controller, + ListTableDescriptorsByStateRequest request) throws ServiceException { + try { + server.checkInitialized(); + List descriptors = server.listTableDescriptors(null, null, null, false); + + ListTableDescriptorsByStateResponse.Builder builder = + ListTableDescriptorsByStateResponse.newBuilder(); + if (descriptors != null && descriptors.size() > 0) { + // Add the table descriptors to the response + TableState.State state = + request.getIsEnabled() ? TableState.State.ENABLED : TableState.State.DISABLED; + for (TableDescriptor htd : descriptors) { + if (server.getTableStateManager().isTableState(htd.getTableName(), state)) { + builder.addTableSchema(ProtobufUtil.toTableSchema(htd)); + } + } + } + return builder.build(); + } catch (IOException ioe) { + throw new ServiceException(ioe); + } + } + /** * Get list of userspace table names * @param controller Unused (set to null). @@ -1200,6 +1229,29 @@ public GetTableNamesResponse getTableNames(RpcController controller, GetTableNam } } + @Override + public ListTableNamesByStateResponse listTableNamesByState(RpcController controller, + ListTableNamesByStateRequest request) throws ServiceException { + try { + server.checkServiceStarted(); + List tableNames = server.listTableNames(null, null, false); + ListTableNamesByStateResponse.Builder builder = ListTableNamesByStateResponse.newBuilder(); + if (tableNames != null && tableNames.size() > 0) { + // Add the disabled table names to the response + TableState.State state = + request.getIsEnabled() ? TableState.State.ENABLED : TableState.State.DISABLED; + for (TableName table : tableNames) { + if (server.getTableStateManager().isTableState(table, state)) { + builder.addTableNames(ProtobufUtil.toProtoTableName(table)); + } + } + } + return builder.build(); + } catch (IOException e) { + throw new ServiceException(e); + } + } + @Override public GetTableStateResponse getTableState(RpcController controller, GetTableStateRequest request) throws ServiceException { diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/master/TestListTablesByState.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/master/TestListTablesByState.java new file mode 100644 index 000000000000..91524ddd6b61 --- /dev/null +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/master/TestListTablesByState.java @@ -0,0 +1,83 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.hbase.master; + +import org.apache.hadoop.hbase.HBaseClassTestRule; +import org.apache.hadoop.hbase.HBaseTestingUtil; +import org.apache.hadoop.hbase.TableName; +import org.apache.hadoop.hbase.client.Admin; +import org.apache.hadoop.hbase.client.TableDescriptor; +import org.apache.hadoop.hbase.client.TableDescriptorBuilder; +import org.apache.hadoop.hbase.testclassification.MasterTests; +import org.apache.hadoop.hbase.testclassification.MediumTests; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +@Category({ MasterTests.class, MediumTests.class }) +public class TestListTablesByState { + @ClassRule + public static final HBaseClassTestRule CLASS_RULE = + HBaseClassTestRule.forClass(TestListTablesByState.class); + + private static HBaseTestingUtil UTIL; + private static Admin ADMIN; + private final static int SLAVES = 1; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + UTIL = new HBaseTestingUtil(); + UTIL.startMiniCluster(SLAVES); + ADMIN = UTIL.getAdmin(); + } + + @Test + public void testListTableNamesByState() throws Exception { + TableName testTableName = TableName.valueOf("test"); + TableDescriptor testTableDesc = TableDescriptorBuilder.newBuilder(testTableName).build(); + ADMIN.createTable(testTableDesc); + ADMIN.disableTable(testTableName); + Assert.assertEquals(ADMIN.listTableNamesByState(false).get(0), testTableName); + ADMIN.enableTable(testTableName); + Assert.assertEquals(ADMIN.listTableNamesByState(true).get(0), testTableName); + } + + @Test + public void testListTableDescriptorByState() throws Exception { + TableName testTableName = TableName.valueOf("test"); + TableDescriptor testTableDesc = TableDescriptorBuilder.newBuilder(testTableName).build(); + ADMIN.createTable(testTableDesc); + ADMIN.disableTable(testTableName); + Assert.assertEquals(ADMIN.listTableDescriptorsByState(false).get(0), testTableDesc); + ADMIN.enableTable(testTableName); + Assert.assertEquals(ADMIN.listTableDescriptorsByState(true).get(0), testTableDesc); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + if (ADMIN != null) { + ADMIN.close(); + } + if (UTIL != null) { + UTIL.shutdownMiniCluster(); + } + } +} diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/rsgroup/VerifyingRSGroupAdmin.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/rsgroup/VerifyingRSGroupAdmin.java index 3c0658455f3a..edf5143bdeb6 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/rsgroup/VerifyingRSGroupAdmin.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/rsgroup/VerifyingRSGroupAdmin.java @@ -144,6 +144,11 @@ public List listTableDescriptors(Pattern pattern, boolean inclu return admin.listTableDescriptors(pattern, includeSysTables); } + @Override + public List listTableDescriptorsByState(boolean isEnabled) throws IOException { + return admin.listTableDescriptorsByState(isEnabled); + } + public TableName[] listTableNames() throws IOException { return admin.listTableNames(); } @@ -152,6 +157,11 @@ public TableName[] listTableNames(Pattern pattern, boolean includeSysTables) thr return admin.listTableNames(pattern, includeSysTables); } + @Override + public List listTableNamesByState(boolean isEnabled) throws IOException { + return admin.listTableNamesByState(isEnabled); + } + public TableDescriptor getDescriptor(TableName tableName) throws TableNotFoundException, IOException { return admin.getDescriptor(tableName); diff --git a/hbase-shell/src/main/ruby/hbase/admin.rb b/hbase-shell/src/main/ruby/hbase/admin.rb index 7d6906b71dcf..7477b8ec164f 100644 --- a/hbase-shell/src/main/ruby/hbase/admin.rb +++ b/hbase-shell/src/main/ruby/hbase/admin.rb @@ -1885,6 +1885,12 @@ def modify_table_family_sft(tableName, family_bytes, sft) def flush_master_store() @admin.flushMasterStore() end + + #---------------------------------------------------------------------------------------------- + # Returns a list of enable or disabled tables in hbase + def list_tables_by_state(isEnabled) + @admin.listTableNamesByState(isEnabled).map(&:getNameAsString) + end end # rubocop:enable Metrics/ClassLength end diff --git a/hbase-shell/src/main/ruby/shell.rb b/hbase-shell/src/main/ruby/shell.rb index 5bd6b451ec2b..eeac0ba95f7e 100644 --- a/hbase-shell/src/main/ruby/shell.rb +++ b/hbase-shell/src/main/ruby/shell.rb @@ -390,6 +390,8 @@ def self.exception_handler(hide_traceback) locate_region list_regions clone_table_schema + list_enabled_tables + list_disabled_tables ], aliases: { 'describe' => ['desc'] diff --git a/hbase-shell/src/main/ruby/shell/commands/list_disabled_tables.rb b/hbase-shell/src/main/ruby/shell/commands/list_disabled_tables.rb new file mode 100644 index 000000000000..81176861550f --- /dev/null +++ b/hbase-shell/src/main/ruby/shell/commands/list_disabled_tables.rb @@ -0,0 +1,44 @@ +# +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +module Shell + module Commands + class ListDisabledTables < Command + def help + <<~EOF + List all disabled tables + Examples: + hbase> list_disabled_tables + EOF + end + + def command() + formatter.header(['TABLE']) + + list = admin.list_tables_by_state(false) + list.each do |table| + formatter.row([table]) + end + + formatter.footer(list.size) + list + end + end + end +end diff --git a/hbase-shell/src/main/ruby/shell/commands/list_enabled_tables.rb b/hbase-shell/src/main/ruby/shell/commands/list_enabled_tables.rb new file mode 100644 index 000000000000..dbb659c3f7a9 --- /dev/null +++ b/hbase-shell/src/main/ruby/shell/commands/list_enabled_tables.rb @@ -0,0 +1,44 @@ +# +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +module Shell + module Commands + class ListEnabledTables < Command + def help + <<~EOF + List all enabled tables + Examples: + hbase> list_enabled_tables + EOF + end + + def command() + formatter.header(['TABLE']) + + list = admin.list_tables_by_state(true) + list.each do |table| + formatter.row([table]) + end + + formatter.footer(list.size) + list + end + end + end +end diff --git a/hbase-shell/src/test/java/org/apache/hadoop/hbase/client/TestListTablesShell.java b/hbase-shell/src/test/java/org/apache/hadoop/hbase/client/TestListTablesShell.java new file mode 100644 index 000000000000..b823b6fc3aa7 --- /dev/null +++ b/hbase-shell/src/test/java/org/apache/hadoop/hbase/client/TestListTablesShell.java @@ -0,0 +1,46 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.hbase.client; + +import org.apache.hadoop.hbase.HBaseClassTestRule; +import org.apache.hadoop.hbase.testclassification.ClientTests; +import org.apache.hadoop.hbase.testclassification.LargeTests; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.experimental.categories.Category; + +@Category({ ClientTests.class, LargeTests.class }) +public class TestListTablesShell extends AbstractTestShell { + @ClassRule + public static final HBaseClassTestRule CLASS_RULE = + HBaseClassTestRule.forClass(TestListTablesShell.class); + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + setUpConfig(); + + TEST_UTIL.startMiniCluster(3); + + setUpJRubyRuntime(); + } + + @Override + protected String getIncludeList() { + return "list_tables_test.rb"; + } +} diff --git a/hbase-shell/src/test/java/org/apache/hadoop/hbase/client/TestShell.java b/hbase-shell/src/test/java/org/apache/hadoop/hbase/client/TestShell.java index b97f7b516b57..47918f68019f 100644 --- a/hbase-shell/src/test/java/org/apache/hadoop/hbase/client/TestShell.java +++ b/hbase-shell/src/test/java/org/apache/hadoop/hbase/client/TestShell.java @@ -32,6 +32,6 @@ public class TestShell extends AbstractTestShell { @Override protected String getExcludeList() { return "replication_admin_test.rb,rsgroup_shell_test.rb,admin_test.rb,table_test.rb," - + "quotas_test.rb,admin2_test.rb"; + + "quotas_test.rb,admin2_test.rb,list_tables_test.rb"; } } diff --git a/hbase-shell/src/test/ruby/shell/list_tables_test.rb b/hbase-shell/src/test/ruby/shell/list_tables_test.rb new file mode 100644 index 000000000000..d3599bf8155f --- /dev/null +++ b/hbase-shell/src/test/ruby/shell/list_tables_test.rb @@ -0,0 +1,48 @@ +# +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'hbase_constants' +require 'hbase_shell' + +class ListTablesTest < Test::Unit::TestCase + def setup + @hbase = ::Hbase::Hbase.new($TEST_CLUSTER.getConfiguration) + @shell = Shell::Shell.new(@hbase) + connection = $TEST_CLUSTER.getConnection + @admin = connection.getAdmin + end + + define_test "List enabled tables" do + table = 'test_table1' + family = 'f1' + @shell.command('create', table, family) + assert_equal [table], @shell.command('list_enabled_tables') + @shell.command(:disable, table) + @shell.command(:drop, table) + end + + define_test "List disabled tables" do + table = 'test_table2' + family = 'f1' + @shell.command('create', table, family) + @shell.command(:disable, table) + assert_equal [table], @shell.command('list_disabled_tables') + @shell.command(:drop, table) + end +end diff --git a/hbase-thrift/src/main/java/org/apache/hadoop/hbase/thrift2/client/ThriftAdmin.java b/hbase-thrift/src/main/java/org/apache/hadoop/hbase/thrift2/client/ThriftAdmin.java index 13a1b9920ecf..398d77dd9005 100644 --- a/hbase-thrift/src/main/java/org/apache/hadoop/hbase/thrift2/client/ThriftAdmin.java +++ b/hbase-thrift/src/main/java/org/apache/hadoop/hbase/thrift2/client/ThriftAdmin.java @@ -174,6 +174,11 @@ public List listTableDescriptors(Pattern pattern, boolean inclu } } + @Override + public List listTableDescriptorsByState(boolean isEnabled) throws IOException { + throw new NotImplementedException("listTableDescriptorsByState not supported in ThriftAdmin"); + } + @Override public TableName[] listTableNames() throws IOException { return listTableNames(null); @@ -195,6 +200,11 @@ public TableName[] listTableNames(Pattern pattern, boolean includeSysTables) thr } } + @Override + public List listTableNamesByState(boolean isEnabled) throws IOException { + throw new NotImplementedException("listTableNamesByState not supported in ThriftAdmin"); + } + @Override public TableDescriptor getDescriptor(TableName tableName) throws TableNotFoundException, IOException {