下面列出了org.apache.hadoop.hbase.client.HBaseAdmin#close ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@BeforeClass
public static void doBeforeTestSetup() throws Exception {
HBaseAdmin admin = driver.getConnectionQueryServices(getUrl(), TEST_PROPERTIES).getAdmin();
try {
try {
admin.disableTable(HBASE_NATIVE_BYTES);
admin.deleteTable(HBASE_NATIVE_BYTES);
} catch (org.apache.hadoop.hbase.TableNotFoundException e) {
}
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();
}
}
@Override
public void connect() throws IOException
{
super.connect();
HTableDescriptor tdesc = table.getTableDescriptor();
if (!tdesc.hasFamily(columnFamilyBytes)) {
HBaseAdmin admin = new HBaseAdmin(table.getConfiguration());
admin.disableTable(table.getTableName());
try {
HColumnDescriptor cdesc = new HColumnDescriptor(columnFamilyBytes);
admin.addColumn(table.getTableName(), cdesc);
} finally {
admin.enableTable(table.getTableName());
admin.close();
}
}
}
/**
* Test to repro ArrayIndexOutOfBoundException that happens during filtering in BinarySubsetComparator
* only after a flush is performed
* @throws Exception
*/
@Test
public void testFilterOnTrailingKeyColumn() throws Exception {
long ts = nextTimestamp();
String tenantId = getOrganizationId();
Properties props = new Properties(TEST_PROPERTIES);
props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts+1));
Connection conn = DriverManager.getConnection(PHOENIX_JDBC_URL, props);
HBaseAdmin admin = null;
try {
initTableValues(tenantId, getSplits(tenantId), ts);
admin = conn.unwrap(PhoenixConnection.class).getQueryServices().getAdmin();
admin.flush(SchemaUtil.getTableNameAsBytes(PRODUCT_METRICS_SCHEMA_NAME,PRODUCT_METRICS_NAME));
String query = "SELECT SUM(TRANSACTIONS) FROM " + PRODUCT_METRICS_NAME + " WHERE FEATURE=?";
PreparedStatement statement = conn.prepareStatement(query);
statement.setString(1, F1);
ResultSet rs = statement.executeQuery();
assertTrue(rs.next());
assertEquals(1200, rs.getInt(1));
} finally {
if (admin != null) admin.close();
conn.close();
}
}
/**
* @param tableName
* @return
*/
public boolean deleteTable(String tableName) throws IOException {
HBaseAdmin admin = new HBaseAdmin(getConnPool().getConn());
if (admin.tableExists(tableName)) {
try {
if (admin.isTableEnabled(tableName)) {
admin.disableTable(tableName);
}
admin.deleteTable(tableName);
LOGGER.info(">>>> Table {} delete success!", tableName);
} catch (Exception ex) {
LOGGER.error("delete table error:", ex);
return false;
}
} else {
LOGGER.warn(">>>> Table {} delete but not exist.", tableName);
}
admin.close();
return true;
}
public Boolean createTable(String tableName, String familyName) throws Exception {
HBaseAdmin admin = new HBaseAdmin(conn);
if (admin.tableExists(tableName)) {
LOGGER.warn(">>>> Table {} exists!", tableName);
admin.close();
return false;
}
HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf(tableName));
tableDesc.addFamily(new HColumnDescriptor(familyName));
admin.createTable(tableDesc);
LOGGER.info(">>>> Table {} create success!", tableName);
admin.close();
return true;
}
/**
* @param tableName
* @return
*/
public boolean deleteTable(String tableName) throws IOException {
HBaseAdmin admin = new HBaseAdmin(conn);
if (admin.tableExists(tableName)) {
try {
admin.disableTable(tableName);
admin.deleteTable(tableName);
LOGGER.info(">>>> Table {} delete success!", tableName);
} catch (Exception ex) {
LOGGER.error("delete table error:", ex);
return false;
}
} else {
LOGGER.warn(">>>> Table {} delete but not exist.", tableName);
}
admin.close();
return true;
}
@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();
}
}
/**
* 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();
}
}
@BeforeClass
@Shadower(classBeingShadowed = BaseHBaseManagedTimeIT.class)
public static void doSetup() throws Exception {
Map<String,String> props = Maps.newHashMapWithExpectedSize(1);
setUpTestDriver(new ReadOnlyProps(props.entrySet().iterator()));
// Ensures our split points will be used
// TODO: do deletePriorTables before test?
Connection conn = DriverManager.getConnection(getUrl());
HBaseAdmin admin = conn.unwrap(PhoenixConnection.class).getQueryServices().getAdmin();
try {
admin.disableTable(TestUtil.ATABLE_NAME);
admin.deleteTable(TestUtil.ATABLE_NAME);
} catch (TableNotFoundException e) {
} finally {
admin.close();
conn.close();
}
}
public static void executeDeleteTables(Configuration config) throws IOException {
HBaseAdmin admin = new HBaseAdmin(config);
if (admin.tableExists(HBaseTableMetaModel.profileCacheTableName)) {
admin.disableTable(HBaseTableMetaModel.profileCacheTableName);
admin.deleteTable(HBaseTableMetaModel.profileCacheTableName);
}
if (admin.tableExists(HBaseTableMetaModel.validationRulesTableName)) {
admin.disableTable(HBaseTableMetaModel.validationRulesTableName);
admin.deleteTable(HBaseTableMetaModel.validationRulesTableName);
}
admin.close();
}
public static void main(String[] args)
throws MasterNotRunningException, ZooKeeperConnectionException,
IOException {
// tag::CREATE1[]
Configuration conf = HBaseConfiguration.create();
HBaseAdmin admin = new HBaseAdmin(conf);
HTableDescriptor desc =
new HTableDescriptor(TableName.valueOf("testtable_create1"));
HColumnDescriptor family = new HColumnDescriptor("f1");
desc.addFamily(family);
admin.createTable(desc);
// end::CREATE1[]
admin.close();
}
private void ensureTable() throws IOException {
LOGGER.info("Creating table {}", toolITTableName);
HBaseAdmin admin = new HBaseAdmin(new Configuration());
HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(toolITTableName));
hTableDescriptor.addFamily(new HColumnDescriptor("f"));
admin.createTable(hTableDescriptor);
admin.close();
LOGGER.info("Created table {}", toolITTableName);
}
public static void close(HBaseAdmin admin) {
try {
if (null != admin) {
admin.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void compactTable(Connection conn, String tableName) throws IOException, InterruptedException, SQLException {
ConnectionQueryServices services = conn.unwrap(PhoenixConnection.class).getQueryServices();
HBaseAdmin admin = services.getAdmin();
try {
admin.flush(tableName);
admin.majorCompact(tableName);
Thread.sleep(10000); // FIXME: how do we know when compaction is done?
} finally {
admin.close();
}
}
/**
* Computes size of each region for table and given column families.
*
* @deprecated Use {@link #RegionSizeCalculator(RegionLocator, Admin)} instead.
*/
@Deprecated
public RegionSizeCalculator(HTable table) throws IOException {
HBaseAdmin admin = new HBaseAdmin(table.getConfiguration());
try {
init(table, admin);
} finally {
admin.close();
}
}
private static void initTable() throws Exception {
Connection conn = DriverManager.getConnection(getUrl());
conn.createStatement().execute("CREATE TABLE " + TABLE_NAME + "("
+ "a VARCHAR PRIMARY KEY, b VARCHAR) "
+ HTableDescriptor.MAX_FILESIZE + "=" + MAX_FILESIZE + ","
+ " SALT_BUCKETS = 4");
PreparedStatement stmt = conn.prepareStatement("UPSERT INTO s VALUES(?,?)");
int rowCount = 0;
for (int c1 = MIN_CHAR; c1 <= MAX_CHAR; c1++) {
for (int c2 = MIN_CHAR; c2 <= MAX_CHAR; c2++) {
String pk = Character.toString((char)c1) + Character.toString((char)c2);
stmt.setString(1, pk);
stmt.setString(2, PAYLOAD);
stmt.execute();
rowCount++;
if (rowCount % BATCH_SIZE == 0) {
conn.commit();
}
}
}
conn.commit();
ConnectionQueryServices services = conn.unwrap(PhoenixConnection.class).getQueryServices();
HBaseAdmin admin = services.getAdmin();
try {
admin.flush(TABLE_NAME);
} finally {
admin.close();
}
conn.close();
}
public static void main(String[] args) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
Configuration conf = HBaseConfiguration.create();
HBaseAdmin admin = new HBaseAdmin(conf);
// tag::CREATE3[]
HTableDescriptor desc = new HTableDescriptor(TableName.valueOf("crc"));
desc.setMaxFileSize((long)20*1024*1024*1024);
desc.setConfiguration("hbase.hstore.compaction.min", "5");
HColumnDescriptor family = new HColumnDescriptor("c");
family.setInMemory(true);
desc.addFamily(family);
UniformSplit uniformSplit = new UniformSplit();
admin.createTable(desc, uniformSplit.split(64));
// end::CREATE3[]
admin.close();
}
private static void destroyTable() throws Exception {
// Physically delete HBase table so that splits occur as expected for each test
Properties props = new Properties(TEST_PROPERTIES);
ConnectionQueryServices services = DriverManager.getConnection(getUrl(), props).unwrap(PhoenixConnection.class).getQueryServices();
HBaseAdmin admin = services.getAdmin();
try {
try {
admin.disableTable(PRODUCT_METRICS_NAME);
admin.deleteTable(PRODUCT_METRICS_NAME);
} catch (TableNotFoundException e) {
}
} finally {
admin.close();
}
}
public static void main(String[] args) throws Exception{
if (args.length == 0) {
System.out.println("CreateTables {tableName} {columnFamilyName} {RegionCount}");
return;
}
String tableName = args[0];
String columnFamilyName = args[1];
String regionCount = args[2];
long regionMaxSize = 107374182400l;
Configuration config = HBaseConfiguration.addHbaseResources(new Configuration());
HBaseAdmin admin = new HBaseAdmin(config);
createTable(tableName, columnFamilyName, Short.parseShort(regionCount), regionMaxSize, admin);
admin.close();
System.out.println("Done");
}
public static void executeCreateTables(Configuration config) throws IOException {
HBaseAdmin admin = new HBaseAdmin(config);
createProfileCacheTable(admin);
createValidationRuleTable(admin);
admin.close();
}