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 b34fa69..55d60a8 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 @@ -718,11 +718,7 @@ class ConnectionManager { @Override public RegionLocator getRegionLocator(TableName tableName) throws IOException { - if (managed) { - throw new IOException("The connection has to be unmanaged."); - } - return new HTable( - tableName, this, tableConfig, rpcCallerFactory, rpcControllerFactory, getBatchPool()); + return new HRegionLocator(tableName, this, getConfiguration()); } @Override diff --git hbase-client/src/main/java/org/apache/hadoop/hbase/client/HRegionLocator.java hbase-client/src/main/java/org/apache/hadoop/hbase/client/HRegionLocator.java new file mode 100644 index 0000000..587966c --- /dev/null +++ hbase-client/src/main/java/org/apache/hadoop/hbase/client/HRegionLocator.java @@ -0,0 +1,135 @@ +package org.apache.hadoop.hbase.client; + +/** +* +* 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. +*/ +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.NavigableMap; +import java.util.Map.Entry; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hbase.HRegionInfo; +import org.apache.hadoop.hbase.HRegionLocation; +import org.apache.hadoop.hbase.RegionLocations; +import org.apache.hadoop.hbase.ServerName; +import org.apache.hadoop.hbase.TableName; +import org.apache.hadoop.hbase.classification.InterfaceAudience; +import org.apache.hadoop.hbase.classification.InterfaceStability; +import org.apache.hadoop.hbase.util.Pair; + +import com.google.common.annotations.VisibleForTesting; + +@InterfaceAudience.Private +@InterfaceStability.Stable +public class HRegionLocator implements RegionLocator { + + private final TableName tableName; + private final Configuration configuration; + private final ClusterConnection connection; + + public HRegionLocator(TableName tableName, ClusterConnection connection, Configuration configuration) { + this.connection = connection; + this.tableName = tableName; + this.configuration = configuration; + } + + @Override + public void close() throws IOException { + // This is a no-op. + } + + /** + * {@inheritDoc} + */ + @Override + public HRegionLocation getRegionLocation(final byte [] row) + throws IOException { + return connection.getRegionLocation(tableName, row, false); + } + + /** + * {@inheritDoc} + */ + @Override + public HRegionLocation getRegionLocation(final byte [] row, boolean reload) + throws IOException { + return connection.getRegionLocation(tableName, row, reload); + } + + @Override + public List getAllRegionLocations() throws IOException { + NavigableMap locations = + MetaScanner.allTableRegions(this.connection, getName()); + ArrayList regions = new ArrayList<>(locations.size()); + for (Entry entry : locations.entrySet()) { + regions.add(new HRegionLocation(entry.getKey(), entry.getValue())); + } + return regions; + } + + /** + * {@inheritDoc} + */ + @Override + public byte[][] getStartKeys() throws IOException { + return getStartEndKeys().getFirst(); + } + + /** + * {@inheritDoc} + */ + @Override + public byte[][] getEndKeys() throws IOException { + return getStartEndKeys().getSecond(); + } + + /** + * {@inheritDoc} + */ + @Override + public Pair getStartEndKeys() throws IOException { + List regions = listRegionLocations(); + final List startKeyList = new ArrayList(regions.size()); + final List endKeyList = new ArrayList(regions.size()); + + for (RegionLocations locations : regions) { + HRegionInfo region = locations.getRegionLocation().getRegionInfo(); + startKeyList.add(region.getStartKey()); + endKeyList.add(region.getEndKey()); + } + + return new Pair(startKeyList.toArray(new byte[startKeyList.size()][]), + endKeyList.toArray(new byte[endKeyList.size()][])); + } + + @Override + public TableName getName() { + return this.tableName; + } + + @VisibleForTesting + List listRegionLocations() throws IOException { + return MetaScanner.listTableRegionLocations(getConfiguration(), this.connection, getName()); + } + + public Configuration getConfiguration() { + return configuration; + } +}