下面列出了org.apache.hadoop.hbase.client.HBaseAdmin#listTables ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@BeforeMethod
public void setUp() throws Exception {
HBaseAdmin admin = testutil.getHBaseAdmin();
if (!admin.tableExists(TableName.valueOf(DEFAULT_TIMESTAMP_STORAGE_TABLE_NAME))) {
HTableDescriptor desc = new HTableDescriptor(TABLE_NAME);
HColumnDescriptor datafam = new HColumnDescriptor(DEFAULT_TIMESTAMP_STORAGE_CF_NAME);
datafam.setMaxVersions(Integer.MAX_VALUE);
desc.addFamily(datafam);
admin.createTable(desc);
}
if (admin.isTableDisabled(TableName.valueOf(DEFAULT_TIMESTAMP_STORAGE_TABLE_NAME))) {
admin.enableTable(TableName.valueOf(DEFAULT_TIMESTAMP_STORAGE_TABLE_NAME));
}
HTableDescriptor[] tables = admin.listTables();
for (HTableDescriptor t : tables) {
LOG.info(t.getNameAsString());
}
}
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());
}
}
}
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());
}
}
}
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());
}
}
}
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());
}
}
}
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());
}
}
}
/**
* 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();
}
}
public static boolean existsFamilyName(HBaseAdmin hba, String tableName, String columnName) {
HTableDescriptor[] list;
try {
list = hba.listTables();
for (int i = 0; i < list.length; i++) {
if (list[i].getNameAsString().equals(tableName))
for (HColumnDescriptor hc : list[i].getColumnFamilies()) {
if (hc.getNameAsString().equals(columnName))
return true;
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
@Test
public void testRetriveHtableHost() throws IOException {
Configuration conf = HBaseConfiguration.create();
HBaseAdmin hbaseAdmin = new HBaseAdmin(conf);
HTableDescriptor[] tableDescriptors = hbaseAdmin.listTables();
for (HTableDescriptor table : tableDescriptors) {
String value = table.getValue("KYLIN_HOST");
if (value != null) {
System.out.println(table.getTableName());
System.out.println("host is " + value);
hbaseAdmin.disableTable(table.getTableName());
table.setValue("KYLIN_HOST_ANOTHER", "dev02");
hbaseAdmin.modifyTable(table.getTableName(), table);
hbaseAdmin.enableTable(table.getTableName());
}
}
hbaseAdmin.close();
}
public static void validateTable(HBaseAdmin admin, String tableName) throws IOException, InterruptedException {
if (tableName.equals(Args.ALL_TABLES)) return;
boolean tableExists = false;
try {
if (tableName.contains(Constant.TABLE_DELIMITER)) {
String[] tables = tableName.split(Constant.TABLE_DELIMITER);
for (String table : tables) {
tableExists = admin.tableExists(table);
}
} else {
tableExists = admin.listTables(tableName).length > 0;
}
} catch (Exception e) {
Thread.sleep(1000);
System.out.println();
System.out.println(admin.getConfiguration().get("hbase.zookeeper.quorum") + " is invalid zookeeper quorum");
System.exit(1);
}
if (tableExists) {
try {
if (!admin.isTableEnabled(tableName)) {
throw new InvalidTableException("Table is not enabled.");
}
} catch (Exception ignore) {
}
} else {
throw new InvalidTableException("Table does not exist.");
}
}
public static void validateTable(HBaseAdmin admin, String tableName) throws IOException, InterruptedException {
if (tableName.equals(Args.ALL_TABLES)) return;
boolean tableExists = false;
try {
if (tableName.contains(Constant.TABLE_DELIMITER)) {
String[] tables = tableName.split(Constant.TABLE_DELIMITER);
for (String table : tables) {
tableExists = admin.tableExists(table);
}
} else {
tableExists = admin.listTables(tableName).length > 0;
}
} catch (Exception e) {
Thread.sleep(1000);
System.out.println();
System.out.println(admin.getConfiguration().get("hbase.zookeeper.quorum") + " is invalid zookeeper quorum");
System.exit(1);
}
if (tableExists) {
try {
if (!admin.isTableEnabled(tableName)) {
throw new InvalidTableException("Table is not enabled.");
}
} catch (Exception ignore) {
}
} else {
throw new InvalidTableException("Table does not exist.");
}
}
public static void validateTable(HBaseAdmin admin, String tableName) throws IOException, InterruptedException {
if (tableName.equals(Args.ALL_TABLES)) return;
boolean tableExists = false;
try {
if (tableName.contains(Constant.TABLE_DELIMITER)) {
String[] tables = tableName.split(Constant.TABLE_DELIMITER);
for (String table : tables) {
tableExists = admin.tableExists(table);
}
} else {
tableExists = admin.listTables(tableName).length > 0;
}
} catch (Exception e) {
Thread.sleep(1000);
System.out.println();
System.out.println(admin.getConfiguration().get("hbase.zookeeper.quorum") + " is invalid zookeeper quorum");
System.exit(1);
}
if (tableExists) {
try {
if (!admin.isTableEnabled(tableName)) {
throw new InvalidTableException("Table is not enabled.");
}
} catch (Exception ignore) {
}
} else {
throw new InvalidTableException("Table does not exist.");
}
}
public static void validateTable(HBaseAdmin admin, String tableName) throws IOException, InterruptedException {
if (tableName.equals(Args.ALL_TABLES)) return;
boolean tableExists = false;
try {
if (tableName.contains(Constant.TABLE_DELIMITER)) {
String[] tables = tableName.split(Constant.TABLE_DELIMITER);
for (String table : tables) {
tableExists = admin.tableExists(table);
}
} else {
tableExists = admin.listTables(tableName).length > 0;
}
} catch (Exception e) {
Thread.sleep(1000);
System.out.println();
System.out.println(admin.getConfiguration().get("hbase.zookeeper.quorum") + " is invalid zookeeper quorum");
System.exit(1);
}
if (tableExists) {
try {
if (!admin.isTableEnabled(tableName)) {
throw new InvalidTableException("Table is not enabled.");
}
} catch (Exception ignore) {
}
} else {
throw new InvalidTableException("Table does not exist.");
}
}
public static void validateTable(HBaseAdmin admin, String tableName) throws IOException, InterruptedException {
if (tableName.equals(Args.ALL_TABLES)) return;
boolean tableExists = false;
try {
if (tableName.contains(Constant.TABLE_DELIMITER)) {
String[] tables = tableName.split(Constant.TABLE_DELIMITER);
for (String table : tables) {
tableExists = admin.tableExists(table);
}
} else {
tableExists = admin.listTables(tableName).length > 0;
}
} catch (Exception e) {
Thread.sleep(1000);
System.out.println();
System.out.println(admin.getConfiguration().get("hbase.zookeeper.quorum") + " is invalid zookeeper quorum");
System.exit(1);
}
if (tableExists) {
try {
if (!admin.isTableEnabled(tableName)) {
throw new InvalidTableException("Table is not enabled.");
}
} catch (Exception ignore) {
}
} else {
throw new InvalidTableException("Table does not exist.");
}
}
private void clean() throws IOException {
Configuration conf = HBaseConfiguration.create();
HBaseAdmin hbaseAdmin = new HBaseAdmin(conf);
for (HTableDescriptor descriptor : hbaseAdmin.listTables()) {
String name = descriptor.getNameAsString().toLowerCase();
if (name.startsWith("kylin") || name.startsWith("_kylin")) {
String x = descriptor.getValue("KYLIN_HOST");
System.out.println("table name " + descriptor.getNameAsString() + " host: " + x);
System.out.println(descriptor);
System.out.println();
}
}
hbaseAdmin.close();
}
private void setup() throws IOException {
long currentTIME = System.currentTimeMillis();
exportFolder = "/tmp/hbase-export/" + currentTIME + "/";
backupArchive = "/tmp/kylin_" + currentTIME + ".tar.gz";
KylinConfig.destoryInstance();
System.setProperty(KylinConfig.KYLIN_CONF, AbstractKylinTestCase.SANDBOX_TEST_DATA);
kylinConfig = KylinConfig.getInstanceFromEnv();
cli = kylinConfig.getCliCommandExecutor();
String metadataUrl = kylinConfig.getMetadataUrl();
// split [email protected]_URL
int cut = metadataUrl.indexOf('@');
tableNameBase = metadataUrl.substring(0, cut);
String hbaseUrl = cut < 0 ? metadataUrl : metadataUrl.substring(cut + 1);
HConnection conn = HBaseConnection.get(hbaseUrl);
try {
hbase = new HBaseAdmin(conn);
config = hbase.getConfiguration();
allTables = hbase.listTables();
} catch (IOException e) {
e.printStackTrace();
throw e;
}
}