diff --git hbase-client/src/main/java/org/apache/hadoop/hbase/client/ConnectionAdapter.java hbase-client/src/main/java/org/apache/hadoop/hbase/client/ConnectionAdapter.java index c57064b..2e1bd64 100644 --- hbase-client/src/main/java/org/apache/hadoop/hbase/client/ConnectionAdapter.java +++ hbase-client/src/main/java/org/apache/hadoop/hbase/client/ConnectionAdapter.java @@ -112,6 +112,11 @@ class ConnectionAdapter implements ClusterConnection { } @Override + public RegionLocator getRegionLocation(TableName tableName) throws IOException { + return wrappedConnection.getRegionLocation(tableName); + } + + @Override public Admin getAdmin() throws IOException { return wrappedConnection.getAdmin(); } diff --git hbase-client/src/main/java/org/apache/hadoop/hbase/client/ConnectionManager.java hbase-client/src/main/java/org/apache/hadoop/hbase/client/ConnectionManager.java index ad18e23..706b3e4 100644 --- hbase-client/src/main/java/org/apache/hadoop/hbase/client/ConnectionManager.java +++ hbase-client/src/main/java/org/apache/hadoop/hbase/client/ConnectionManager.java @@ -714,6 +714,14 @@ class ConnectionManager { } @Override + public RegionLocator getRegionLocation(TableName tableName) throws IOException { + if (managed) { + throw new IOException("The connection has to be unmanaged."); + } + return new HTable(tableName, this, getBatchPool()); + } + + @Override public Admin getAdmin() throws IOException { if (managed) { throw new IOException("The connection has to be unmanaged."); diff --git hbase-client/src/main/java/org/apache/hadoop/hbase/client/HConnection.java hbase-client/src/main/java/org/apache/hadoop/hbase/client/HConnection.java index 77e9a5e..321f649 100644 --- hbase-client/src/main/java/org/apache/hadoop/hbase/client/HConnection.java +++ hbase-client/src/main/java/org/apache/hadoop/hbase/client/HConnection.java @@ -154,11 +154,25 @@ public interface HConnection extends Abortable, Closeable { public HTableInterface getTable(TableName tableName, ExecutorService pool) throws IOException; /** + * Retrieve a RegionLocator implementation to inspect region information on a table. The returned + * RegionLocator is not thread-safe, so a new instance should be created for each using thread. + * + * This is a lightweight operation. Pooling or caching of teh returned RegionLocator is neither + * required nor desired. + * + * RegionLocator needs to be unmanaged + * (created with {@link HConnectionManager#createConnection(Configuration)}). + * + * @param tableName Name of the table who's region is to be examined + * @return A RegionLocator instance + */ + public RegionLocator getRegionLocation(TableName tableName) throws IOException; + + /** * Retrieve an Admin implementation to administer an HBase cluster. * The returned Admin is not guaranteed to be thread-safe. A new instance should be created for * each using thread. This is a lightweight operation. Pooling or caching of the returned * Admin is not recommended. Note that HConnection needs to be unmanaged - * (created with {@link HConnectionManager#createConnection(Configuration)}). * * @return an Admin instance for cluster administration */ diff --git hbase-client/src/main/java/org/apache/hadoop/hbase/client/HTable.java hbase-client/src/main/java/org/apache/hadoop/hbase/client/HTable.java index 1265a5f..ad53480 100644 --- hbase-client/src/main/java/org/apache/hadoop/hbase/client/HTable.java +++ hbase-client/src/main/java/org/apache/hadoop/hbase/client/HTable.java @@ -123,7 +123,7 @@ import com.google.protobuf.ServiceException; */ @InterfaceAudience.Public @InterfaceStability.Stable -public class HTable implements HTableInterface { +public class HTable implements HTableInterface, RegionLocator { private static final Log LOG = LogFactory.getLog(HTable.class); protected ClusterConnection connection; private final TableName tableName; @@ -498,30 +498,27 @@ public class HTable implements HTableInterface { * @param row Row to find. * @return The location of the given row. * @throws IOException if a remote or network exception occurs + * @deprecated Use {@link RegionLocator#getRegionLocation(byte[])} */ + @Deprecated public HRegionLocation getRegionLocation(final String row) throws IOException { return connection.getRegionLocation(tableName, Bytes.toBytes(row), false); } /** - * Finds the region on which the given row is being served. Does not reload the cache. - * @param row Row to find. - * @return Location of the row. - * @throws IOException if a remote or network exception occurs + * {@inheritDoc} */ + @Override public HRegionLocation getRegionLocation(final byte [] row) throws IOException { return connection.getRegionLocation(tableName, row, false); } /** - * Finds the region on which the given row is being served. - * @param row Row to find. - * @param reload true to reload information or false to use cached information - * @return Location of the row. - * @throws IOException if a remote or network exception occurs + * {@inheritDoc} */ + @Override public HRegionLocation getRegionLocation(final byte [] row, boolean reload) throws IOException { return connection.getRegionLocation(tableName, row, reload); @@ -599,36 +596,25 @@ public class HTable implements HTableInterface { } /** - * Gets the starting row key for every region in the currently open table. - *

- * This is mainly useful for the MapReduce integration. - * @return Array of region starting row keys - * @throws IOException if a remote or network exception occurs + * {@inheritDoc} */ + @Override public byte [][] getStartKeys() throws IOException { return getStartEndKeys().getFirst(); } /** - * Gets the ending row key for every region in the currently open table. - *

- * This is mainly useful for the MapReduce integration. - * @return Array of region ending row keys - * @throws IOException if a remote or network exception occurs + * {@inheritDoc} */ + @Override public byte[][] getEndKeys() throws IOException { return getStartEndKeys().getSecond(); } /** - * Gets the starting and ending row keys for every region in the currently - * open table. - *

- * This is mainly useful for the MapReduce integration. - * @return Pair of arrays of region starting and ending row keys - * @throws IOException if a remote or network exception occurs + * {@inheritDoc} */ - // TODO: these are not in HTableInterface. Should we add them there or move these to HBaseAdmin? + @Override public Pair getStartEndKeys() throws IOException { List regions = listRegionLocations(); @@ -1667,9 +1653,9 @@ public class HTable implements HTableInterface { } /** - * Explicitly clears the region cache to fetch the latest value from META. - * This is a power user function: avoid unless you know the ramifications. + * {@inheritDoc} */ + @Override public void clearRegionCache() { this.connection.clearRegionCache(); } diff --git hbase-client/src/main/java/org/apache/hadoop/hbase/client/RegionLocator.java hbase-client/src/main/java/org/apache/hadoop/hbase/client/RegionLocator.java new file mode 100644 index 0000000..5b62ac7 --- /dev/null +++ hbase-client/src/main/java/org/apache/hadoop/hbase/client/RegionLocator.java @@ -0,0 +1,88 @@ +/** + * + * 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.classification.InterfaceAudience; +import org.apache.hadoop.classification.InterfaceStability; +import org.apache.hadoop.hbase.HRegionLocation; +import org.apache.hadoop.hbase.util.Pair; + +import java.io.IOException; + +/** + * Used to view region location information for a single HBase table. + * Obtain an instance from an {@link HConnection}. + * + * @since 0.99.0 + */ +@InterfaceAudience.Public +@InterfaceStability.Evolving +public interface RegionLocator { + /** + * Finds the region on which the given row is being served. Does not reload the cache. + * @param row Row to find. + * @return Location of the row. + * @throws IOException if a remote or network exception occurs + */ + public HRegionLocation getRegionLocation(final byte [] row) throws IOException; + + /** + * Finds the region on which the given row is being served. + * @param row Row to find. + * @param reload true to reload information or false to use cached information + * @return Location of the row. + * @throws IOException if a remote or network exception occurs + */ + public HRegionLocation getRegionLocation(final byte [] row, boolean reload) + throws IOException; + + /** + * Gets the starting row key for every region in the currently open table. + *

+ * This is mainly useful for the MapReduce integration. + * @return Array of region starting row keys + * @throws IOException if a remote or network exception occurs + */ + public byte [][] getStartKeys() throws IOException; + + /** + * Gets the ending row key for every region in the currently open table. + *

+ * This is mainly useful for the MapReduce integration. + * @return Array of region ending row keys + * @throws IOException if a remote or network exception occurs + */ + public byte[][] getEndKeys() throws IOException; + + /** + * Gets the starting and ending row keys for every region in the currently + * open table. + *

+ * This is mainly useful for the MapReduce integration. + * @return Pair of arrays of region starting and ending row keys + * @throws IOException if a remote or network exception occurs + */ + public Pair getStartEndKeys() throws IOException; + + /** + * Explicitly clears the region cache to fetch the latest value from META. + * This is a power user function: avoid unless you know the ramifications. + */ + public void clearRegionCache(); +} diff --git hbase-server/src/main/java/org/apache/hadoop/hbase/client/CoprocessorHConnection.java hbase-server/src/main/java/org/apache/hadoop/hbase/client/CoprocessorHConnection.java index a512f83..e6c4a80 100644 --- hbase-server/src/main/java/org/apache/hadoop/hbase/client/CoprocessorHConnection.java +++ hbase-server/src/main/java/org/apache/hadoop/hbase/client/CoprocessorHConnection.java @@ -141,6 +141,11 @@ class CoprocessorHConnection implements ClusterConnection { } @Override + public RegionLocator getRegionLocation(TableName tableName) throws IOException { + return delegate.getRegionLocation(tableName); + } + + @Override public Admin getAdmin() throws IOException { return delegate.getAdmin(); } @Override