下面列出了怎么用org.apache.hadoop.hbase.client.HBaseAdmin的API类实例代码及写法,或者点击链接到github查看源代码。
private HBaseTestingUtility hbaseOperation() throws Exception {
HBaseTestingUtility hbaseTestingUtility = new HBaseTestingUtility();
/**
* # fsOwner's name is Benedict Jin, will throw exception: Illegal character in path at index 42
* hbaseTestingUtility.getTestFileSystem().setOwner(new Path(BASE_PATH.concat("/owner")), "Benedict Jin", "supergroup");
*/
MiniHBaseCluster hbaseCluster = hbaseTestingUtility.startMiniCluster();
hbaseTestingUtility.createTable(Bytes.toBytes(TABLE_NAME), Bytes.toBytes("context"));
hbaseTestingUtility.deleteTable(Bytes.toBytes(TABLE_NAME));
Configuration config = hbaseCluster.getConf();
Connection conn = ConnectionFactory.createConnection(config);
HBaseAdmin hbaseAdmin = new HBaseAdmin(conn);
HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(TABLE_NAME));
hbaseAdmin.createTable(desc);
return hbaseTestingUtility;
}
@Test
public void testSetHColumnProperties() throws Exception {
Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
String ddl = "CREATE TABLE T1 (\n"
+"ID1 VARCHAR(15) NOT NULL,\n"
+"ID2 VARCHAR(15) NOT NULL,\n"
+"CREATED_DATE DATE,\n"
+"CREATION_TIME BIGINT,\n"
+"LAST_USED DATE,\n"
+"CONSTRAINT PK PRIMARY KEY (ID1, ID2)) SALT_BUCKETS = 8";
Connection conn1 = DriverManager.getConnection(getUrl(), props);
conn1.createStatement().execute(ddl);
ddl = "ALTER TABLE T1 SET REPLICATION_SCOPE=1";
conn1.createStatement().execute(ddl);
try (HBaseAdmin admin = conn1.unwrap(PhoenixConnection.class).getQueryServices().getAdmin()) {
HColumnDescriptor[] columnFamilies = admin.getTableDescriptor(Bytes.toBytes("T1")).getColumnFamilies();
assertEquals(1, columnFamilies.length);
assertEquals("0", columnFamilies[0].getNameAsString());
assertEquals(1, columnFamilies[0].getScope());
}
}
/**
* this method will close hbaseAdmin after finishing the work.
*/
public static void dropHTables(final HBaseAdmin hbaseAdmin, List<String> hTables) {
runSingleThreadTaskQuietly(() -> {
try {
for (String htable : hTables) {
logger.info("Deleting HBase table {}", htable);
if (hbaseAdmin.tableExists(htable)) {
if (hbaseAdmin.isTableEnabled(htable)) {
hbaseAdmin.disableTable(htable);
}
hbaseAdmin.deleteTable(htable);
logger.info("Deleted HBase table {}", htable);
} else {
logger.info("HBase table {} does not exist.", htable);
}
}
} catch (Exception e) {
// storage cleanup job will delete it
logger.error("Deleting HBase table failed");
} finally {
IOUtils.closeQuietly(hbaseAdmin);
}
});
}
@BeforeClass
public static void doBeforeTestSetup() throws Exception {
HBaseAdmin admin = driver.getConnectionQueryServices(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).getAdmin();
try {
try {
admin.disableTable(HBASE_NATIVE_BYTES);
admin.deleteTable(HBASE_NATIVE_BYTES);
} catch (org.apache.hadoop.hbase.TableNotFoundException e) {
}
@SuppressWarnings("deprecation")
HTableDescriptor descriptor = new HTableDescriptor(HBASE_NATIVE_BYTES);
HColumnDescriptor columnDescriptor = new HColumnDescriptor(FAMILY_NAME);
columnDescriptor.setKeepDeletedCells(true);
descriptor.addFamily(columnDescriptor);
admin.createTable(descriptor, SPLITS);
initTableValues();
} finally {
admin.close();
}
}
public ExportKeys(HBaseAdmin admin, Args args) {
this.admin = admin;
this.args = args;
if (args.getTableName().equals(Args.ALL_TABLES))
throw new IllegalArgumentException(Args.INVALID_ARGUMENTS);
if (args.getOptionSet().nonOptionArguments().size() != 3)
throw new IllegalArgumentException(Args.INVALID_ARGUMENTS);
outputFileName = (String) args.getOptionSet().nonOptionArguments().get(2);
if (args.has(Args.OPTION_OPTIMIZE)) {
exportThreshold = parseExportThreshold();
System.out.println("exporting threshold: " + exportThreshold + " MB");
} else {
exportThreshold = 0;
System.out.println("exporting threshold is not set");
}
}
public static void resetCoprocessor(String tableName, HBaseAdmin hbaseAdmin, Path hdfsCoprocessorJar) throws IOException {
logger.info("Disable " + tableName);
hbaseAdmin.disableTable(tableName);
logger.info("Unset coprocessor on " + tableName);
HTableDescriptor desc = hbaseAdmin.getTableDescriptor(TableName.valueOf(tableName));
while (desc.hasCoprocessor(OBSERVER_CLS_NAME)) {
desc.removeCoprocessor(OBSERVER_CLS_NAME);
}
while (desc.hasCoprocessor(ENDPOINT_CLS_NAMAE)) {
desc.removeCoprocessor(ENDPOINT_CLS_NAMAE);
}
addCoprocessorOnHTable(desc, hdfsCoprocessorJar);
hbaseAdmin.modifyTable(tableName, desc);
logger.info("Enable " + tableName);
hbaseAdmin.enableTable(tableName);
}
public static boolean isMoved(HBaseAdmin admin, String tableName, String regionName, String serverNameTarget) {
try (HTable table = new HTable(admin.getConfiguration(), tableName)) {
NavigableMap<HRegionInfo, ServerName> regionLocations = table.getRegionLocations();
for (Map.Entry<HRegionInfo, ServerName> regionLocation : regionLocations.entrySet()) {
if (regionLocation.getKey().getEncodedName().equals(regionName)) {
return regionLocation.getValue().getServerName().equals(serverNameTarget);
}
}
if (!existsRegion(regionName, regionLocations.keySet()))
return true; // skip moving
} catch (IOException e) {
return false;
}
return false;
}
@AfterMethod(alwaysRun = true, timeOut = 60_000)
public void cleanup() throws Exception {
LOG.info("Cleanup");
HBaseAdmin admin = hBaseUtils.getHBaseAdmin();
deleteTable(admin, TableName.valueOf(DEFAULT_TIMESTAMP_STORAGE_TABLE_NAME));
hBaseUtils.createTable(TableName.valueOf((DEFAULT_TIMESTAMP_STORAGE_TABLE_NAME)),
new byte[][]{DEFAULT_TIMESTAMP_STORAGE_CF_NAME.getBytes()},
Integer.MAX_VALUE);
tso1.stopAndWait();
TestUtils.waitForSocketNotListening("localhost", TSO1_PORT, 100);
tso2.stopAndWait();
TestUtils.waitForSocketNotListening("localhost", TSO2_PORT, 100);
zkClient.delete().forPath(TSO_LEASE_PATH);
LOG.info("ZKPath {} deleted", TSO_LEASE_PATH);
zkClient.delete().forPath(CURRENT_TSO_PATH);
LOG.info("ZKPaths {} deleted", CURRENT_TSO_PATH);
zkClient.close();
}
/**
* Creates the table with the column families
*
* @param tableName
* @param columnFamilies
* @return
*/
public static void createTable(String tableName, String... columnFamilies) {
HBaseTestingUtility testingUtility = getUtility();
if (!status.get()) {
throw new RuntimeException("The mini cluster hasn't started yet. " +
" Call HBaseTestUtil#startMiniCluster() before creating a table");
}
final TableName name = TableName.valueOf(tableName);
try (HBaseAdmin hBaseAdmin = testingUtility.getHBaseAdmin()) {
final HTableDescriptor hTableDescriptor = new HTableDescriptor(name);
for (String family : columnFamilies) {
final HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(Bytes.toBytes(family));
hTableDescriptor.addFamily(hColumnDescriptor);
}
hBaseAdmin.createTable(hTableDescriptor);
testingUtility.waitUntilAllRegionsAssigned(name);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* Disable and drop all the tables except SYSTEM.CATALOG and SYSTEM.SEQUENCE
*/
private static void disableAndDropNonSystemTables() throws Exception {
HBaseAdmin admin = driver.getConnectionQueryServices(null, null).getAdmin();
try {
HTableDescriptor[] tables = admin.listTables();
for (HTableDescriptor table : tables) {
String schemaName = SchemaUtil.getSchemaNameFromFullName(table.getName());
if (!QueryConstants.SYSTEM_SCHEMA_NAME.equals(schemaName)) {
admin.disableTable(table.getName());
admin.deleteTable(table.getName());
}
}
} finally {
admin.close();
}
}
private void runUpdateStatisticsTool(String fullTableName) {
UpdateStatisticsTool tool = new UpdateStatisticsTool();
tool.setConf(utility.getConfiguration());
String randomDir = getUtility().getRandomDir().toString();
final String[] cmdArgs = getArgValues(fullTableName, randomDir);
try {
int status = tool.run(cmdArgs);
assertEquals("MR Job should complete successfully", 0, status);
HBaseAdmin hBaseAdmin = utility.getHBaseAdmin();
assertEquals("Snapshot should be automatically deleted when UpdateStatisticsTool has completed",
0, hBaseAdmin.listSnapshots(tool.getSnapshotName()).size());
} catch (Exception e) {
fail("Exception when running UpdateStatisticsTool for " + tableName + " Exception: " + e);
} finally {
Job job = tool.getJob();
assertEquals("MR Job should have been configured with UPDATE_STATS job type",
job.getConfiguration().get(MAPREDUCE_JOB_TYPE), UPDATE_STATS.name());
}
}
public static void main(String[] args) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
// tag::MERGE1[]
Configuration conf = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(conf);
HBaseAdmin admin = (HBaseAdmin)connection.getAdmin();
List<HRegionInfo> regions = admin.getTableRegions(TableName.valueOf("t1")); //<1>
LOG.info("testtable contains " + regions.size() + " regions.");
for (int index = 0; index < regions.size() / 2; index++) {
HRegionInfo region1 = regions.get(index*2);
HRegionInfo region2 = regions.get(index*2+1);
LOG.info("Merging regions " + region1 + " and " + region2);
admin.mergeRegions(region1.getEncodedNameAsBytes(),
region2.getEncodedNameAsBytes(), false); //<2>
}
admin.close();
// end::MERGE1[]
}
public static void main(String[] argsParam) throws Exception {
setLoggingThreshold("ERROR");
SnapshotArgs args;
try {
args = new SnapshotArgs(argsParam);
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
System.out.println();
System.out.println(usage());
System.exit(1);
throw e;
}
HBaseAdmin admin = HBaseClient.getAdmin(args);
Snapshot app = new Snapshot(admin, args);
app.run();
}
public ExportKeys(HBaseAdmin admin, Args args) {
this.admin = admin;
this.args = args;
if (args.getTableName().equals(Args.ALL_TABLES))
throw new IllegalArgumentException(Args.INVALID_ARGUMENTS);
if (args.getOptionSet().nonOptionArguments().size() != 3)
throw new IllegalArgumentException(Args.INVALID_ARGUMENTS);
outputFileName = (String) args.getOptionSet().nonOptionArguments().get(2);
if (args.has(Args.OPTION_OPTIMIZE)) {
exportThreshold = parseExportThreshold();
System.out.println("exporting threshold: " + exportThreshold + " MB");
} else {
exportThreshold = 0;
System.out.println("exporting threshold is not set");
}
}
private void deleteOldSnapshots(HBaseAdmin admin, String tableName) throws IOException {
if (args.keepCount(tableName) == SnapshotArgs.KEEP_UNLIMITED
|| args.has(Args.OPTION_TEST) && !tableName.startsWith("UNIT_TEST_")) {
System.out.println(timestamp(TimestampFormat.log)
+ " - Table \"" + tableName + "\" - Delete Snapshot - Keep Unlimited - SKIPPED");
return;
}
List<SnapshotDescription> sd = SnapshotAdapter.getSnapshotDescriptions(admin, getPrefix(tableName) + ".*");
int snapshotCounter = sd.size();
tableSnapshotCountMaxMap.put(tableName, snapshotCounter);
for (SnapshotDescription d : sd) {
if (snapshotCounter-- > args.keepCount(tableName)) {
String snapshotName = d.getName();
System.out.print(timestamp(TimestampFormat.log)
+ " - Table \"" + tableName + "\" - Delete Snapshot - Keep "
+ args.keepCount(tableName) + " - \"" + snapshotName + "\" - ");
admin.deleteSnapshot(snapshotName);
System.out.println("OK");
}
}
}
public static void createTableAndColumn(Configuration conf,
String table,
byte[] columnFamily)
throws IOException {
HBaseAdmin hbase = new HBaseAdmin(conf);
HTableDescriptor desc = new HTableDescriptor(table);
HColumnDescriptor meta = new HColumnDescriptor(columnFamily);
desc.addFamily(meta);
if (hbase.tableExists(table)) {
if(hbase.isTableEnabled(table)) {
hbase.disableTable(table);
}
hbase.deleteTable(table);
}
hbase.createTable(desc);
}
private void parseTables(HBaseAdmin admin, String[] tables, Set<String> tableSet) throws IOException {
for (String table : tables) {
final String tableName;
if (table.contains(ENTRY_DELIMITER)) {
String[] parts = table.split(ENTRY_DELIMITER);
tableName = parts[0];
tableKeepMap.put(tableName, Integer.valueOf(parts[1]));
tableFlushMap.put(tableName, Boolean.valueOf(parts[2]));
} else {
tableName = table;
}
for (HTableDescriptor hTableDescriptor : admin.listTables(tableName)) {
tableSet.add(hTableDescriptor.getNameAsString());
}
}
}
public RegionLoadAdapter(HBaseAdmin admin, Map<byte[], HRegionInfo> regionMap, Args args) throws IOException {
long timestamp = System.currentTimeMillis();
ClusterStatus clusterStatus = admin.getClusterStatus();
Collection<ServerName> serverNames = clusterStatus.getServers();
for (ServerName serverName : serverNames) {
HServerLoad serverLoad = clusterStatus.getLoad(serverName);
for (Map.Entry<byte[], HServerLoad.RegionLoad> entry : serverLoad.getRegionsLoad().entrySet()) {
if (regionMap.get(entry.getKey()) != null)
regionLoadMap.put(regionMap.get(entry.getKey()), new RegionLoadDelegator(entry.getValue()));
}
}
Util.printVerboseMessage(args, "RegionLoadAdapter", timestamp);
}
private Command createCommand(String commandName, HBaseAdmin admin, Args args) throws Exception {
for (Class<? extends Command> c : commandSet) {
if (c.getSimpleName().toLowerCase().equals(commandName.toLowerCase())) {
Constructor constructor = c.getDeclaredConstructor(HBaseAdmin.class, Args.class);
constructor.setAccessible(true);
return (Command) constructor.newInstance(admin, args);
}
}
throw new IllegalArgumentException(INVALID_COMMAND);
}
public TableInfo(HBaseAdmin admin, String tableName, Args args) throws Exception {
super();
this.admin = admin;
this.tableName = tableName;
this.args = args;
load = new Load(new LevelClass(isMultiTable(), args));
}
public Balance(HBaseAdmin admin, Args args) throws IOException {
if (args.getOptionSet().nonOptionArguments().size() != 3) {
throw new RuntimeException(Args.INVALID_ARGUMENTS);
}
this.admin = admin;
this.args = args;
ruleParam = (String) args.getOptionSet().nonOptionArguments().get(2);
tableNameSet = Util.parseTableSet(admin, args);
reset();
}
public RegionLoadAdapter(HBaseAdmin admin, Map<byte[], HRegionInfo> regionMap, Args args) throws IOException {
long timestamp = System.currentTimeMillis();
ClusterStatus clusterStatus = admin.getClusterStatus();
Collection<ServerName> serverNames = clusterStatus.getServers();
for (ServerName serverName : serverNames) {
ServerLoad serverLoad = clusterStatus.getLoad(serverName);
for (Map.Entry<byte[], RegionLoad> entry : serverLoad.getRegionsLoad().entrySet()) {
if (regionMap.get(entry.getKey()) != null)
regionLoadMap.put(regionMap.get(entry.getKey()), new RegionLoadDelegator(entry.getValue()));
}
}
Util.printVerboseMessage(args, "RegionLoadAdapter", timestamp);
}
public boolean tableExists(String tableName) {
try (HBaseAdmin admin = (HBaseAdmin) getConnection().getAdmin()) {
return admin.tableExists(TableName.valueOf(tableName));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public Assign(HBaseAdmin admin, Args args) {
if (args.getOptionSet().nonOptionArguments().size() < 2) {
throw new IllegalArgumentException(Args.INVALID_ARGUMENTS);
}
this.admin = admin;
this.args = args;
}
public void disableTable(String tableName) {
try (HBaseAdmin admin = (HBaseAdmin) getConnection().getAdmin()) {
admin.disableTable(tableName);
} catch (IOException e) {
logger.error(e.getMessage(), e);
throw new RuntimeException(e);
}
}
public void checkHBaseAvailable(List<Stage.ConfigIssue> issues) {
try {
HBaseAdmin.available(hbaseConnectionHelper.getHBaseConfiguration());
} catch (Exception ex) {
LOG.warn("Received exception while connecting to cluster: ", ex);
issues.add(getContext().createConfigIssue(Groups.HBASE.name(), null, Errors.HBASE_06, ex.toString(), ex));
}
}
@Test
public void testCachedConnections() throws Exception {
final String tableName = generateUniqueName();
final String index1Name = generateUniqueName();
final Connection conn = DriverManager.getConnection(getUrl());
final HBaseAdmin admin = getUtility().getHBaseAdmin();
final MiniHBaseCluster cluster = getUtility().getHBaseCluster();
final HRegionServer regionServer = cluster.getRegionServer(0);
Configuration conf = admin.getConfiguration();
final int noOfOrgs = 20;
final AtomicBoolean flag = new AtomicBoolean();
flag.set(false);
// create table and indices
String createTableSql = "CREATE TABLE " + tableName
+ "(org_id VARCHAR NOT NULL PRIMARY KEY, v1 INTEGER, v2 INTEGER, v3 INTEGER) VERSIONS=1 SPLIT ON ('"
+ ORG_PREFIX + "-" + noOfOrgs / 2 + "')";
conn.createStatement().execute(createTableSql);
conn.createStatement().execute("CREATE INDEX " + index1Name + " ON " + tableName + "(v1)");
List<HRegionInfo> regions = admin.getTableRegions(TableName.valueOf(tableName));
final HRegionInfo regionInfo = regions.get(0);
writeToTable(tableName, conn, noOfOrgs);
int beforeRegionCloseCount = getActiveConnections(regionServer, conf);
int regionsCount = admin.getOnlineRegions(regionServer.getServerName()).size();
admin.unassign(regionInfo.getEncodedNameAsBytes(), true);
while(!(admin.getOnlineRegions(regionServer.getServerName()).size() < regionsCount));
int afterRegionCloseCount = getActiveConnections(regionServer, conf);
assertTrue("Cached connections not closed when region closes: ",
afterRegionCloseCount == beforeRegionCloseCount && afterRegionCloseCount > 0);
}
/**
* @param tableName name of the table to create for the test
* @return the supporting state for the test
*/
private TestState setupTest(String tableName) throws IOException {
byte[] tableNameBytes = Bytes.toBytes(tableName);
HTableDescriptor desc = new HTableDescriptor(tableNameBytes);
desc.addFamily(FAM1);
// add the necessary simple options to create the builder
Map<String, String> indexerOpts = new HashMap<String, String>();
// just need to set the codec - we are going to set it later, but we need something here or the
// initializer blows up.
indexerOpts.put(CoveredColumnsIndexBuilder.CODEC_CLASS_NAME_KEY,
CoveredIndexCodecForTesting.class.getName());
Indexer.enableIndexing(desc, CoveredColumnsIndexBuilder.class, indexerOpts);
// create the table
HBaseAdmin admin = UTIL.getHBaseAdmin();
admin.createTable(desc);
HTable primary = new HTable(UTIL.getConfiguration(), tableNameBytes);
// overwrite the codec so we can verify the current state
HRegion region = UTIL.getMiniHBaseCluster().getRegions(tableNameBytes).get(0);
Indexer indexer =
(Indexer) region.getCoprocessorHost().findCoprocessor(Indexer.class.getName());
CoveredColumnsIndexBuilder builder =
(CoveredColumnsIndexBuilder) indexer.getBuilderForTesting();
VerifyingIndexCodec codec = new VerifyingIndexCodec();
builder.setIndexCodecForTesting(codec);
// setup the Puts we want to write
final long ts = System.currentTimeMillis();
EnvironmentEdge edge = new EnvironmentEdge() {
@Override
public long currentTimeMillis() {
return ts;
}
};
EnvironmentEdgeManager.injectEdge(edge);
return new TestState(primary, codec, ts);
}
private static void check(HBaseAdmin admin, HTable htable) throws DataXException, IOException {
if (!admin.isMasterRunning()) {
throw new IllegalStateException("HBase master 没有运行, 请检查您的配置 或者 联系 Hbase 管理员.");
}
if (!admin.tableExists(htable.getTableName())) {
throw new IllegalStateException("HBase源头表" + Bytes.toString(htable.getTableName()) + "不存在, 请检查您的配置 或者 联系 Hbase 管理员.");
}
if (!admin.isTableAvailable(htable.getTableName()) || !admin.isTableEnabled(htable.getTableName())) {
throw new IllegalStateException("HBase源头表" + Bytes.toString(htable.getTableName()) + " 不可用, 请检查您的配置 或者 联系 Hbase 管理员.");
}
}
public void run() throws Exception {
try (HBaseAdmin admin = HBaseClient.getAdmin(args)) {
Command command = createCommand(commandName, admin, args);
command.run();
Util.sendAlertAfterSuccess(args, this.getClass());
} catch (Throwable e) {
Util.sendAlertAfterFailed(args, this.getClass(), e.getMessage());
throw e;
}
}