Java Code Examples for org.apache.hadoop.hbase.client.HBaseAdmin

The following examples show how to use org.apache.hadoop.hbase.client.HBaseAdmin. These examples are extracted from open source projects.
Example 1
Project: yuzhouwan   File: HBaseMiniCluster.java    License: Apache License 2.0 6 votes vote down vote up
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;
    }
 
Example 2
Project: phoenix   File: AlterTableIT.java    License: Apache License 2.0 6 votes vote down vote up
@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());
    }
}
 
Example 3
Project: kylin-on-parquet-v2   File: StorageCleanUtil.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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);
        }
    });
}
 
Example 4
Project: phoenix   File: NativeHBaseTypesIT.java    License: Apache License 2.0 6 votes vote down vote up
@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();
    }
}
 
Example 5
Project: hbase-tools   File: ExportKeys.java    License: Apache License 2.0 6 votes vote down vote up
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");
    }
}
 
Example 6
Project: Kylin   File: DeployCoprocessorCLI.java    License: Apache License 2.0 6 votes vote down vote up
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);
}
 
Example 7
Project: hbase-tools   File: Util.java    License: Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 8
Project: phoenix-omid   File: TestEndToEndScenariosWithHA.java    License: Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example 9
Project: kafka-connect-hbase   File: HbaseTestUtil.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example 10
Project: phoenix   File: BaseTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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();
    }
}
 
Example 11
Project: phoenix   File: BaseStatsCollectorIT.java    License: Apache License 2.0 6 votes vote down vote up
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());
    }
}
 
Example 12
Project: examples   File: Merge.java    License: Apache License 2.0 6 votes vote down vote up
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[]
}
 
Example 13
Project: hbase-tools   File: Snapshot.java    License: Apache License 2.0 6 votes vote down vote up
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();
}
 
Example 14
Project: hbase-tools   File: ExportKeys.java    License: Apache License 2.0 6 votes vote down vote up
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");
    }
}
 
Example 15
Project: hbase-tools   File: Snapshot.java    License: Apache License 2.0 6 votes vote down vote up
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");
        }
    }
}
 
Example 16
Project: hiped2   File: HBaseWriter.java    License: Apache License 2.0 6 votes vote down vote up
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);
}
 
Example 17
Project: hbase-tools   File: SnapshotArgs.java    License: Apache License 2.0 6 votes vote down vote up
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());
        }
    }
}
 
Example 18
Project: hbase-tools   File: RegionLoadAdapter.java    License: Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 19
Project: hbase-tools   File: Manager.java    License: Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 20
Project: hbase-tools   File: TableInfo.java    License: Apache License 2.0 5 votes vote down vote up
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));
}
 
Example 21
Project: hbase-tools   File: Balance.java    License: Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 22
Project: hbase-tools   File: RegionLoadAdapter.java    License: Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 23
Project: canal-1.1.3   File: HbaseTemplate.java    License: Apache License 2.0 5 votes vote down vote up
public boolean tableExists(String tableName) {
    try (HBaseAdmin admin = (HBaseAdmin) getConnection().getAdmin()) {

        return admin.tableExists(TableName.valueOf(tableName));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 24
Project: hbase-tools   File: Assign.java    License: Apache License 2.0 5 votes vote down vote up
public Assign(HBaseAdmin admin, Args args) {
    if (args.getOptionSet().nonOptionArguments().size() < 2) {
        throw new IllegalArgumentException(Args.INVALID_ARGUMENTS);
    }

    this.admin = admin;
    this.args = args;
}
 
Example 25
Project: canal-1.1.3   File: HbaseTemplate.java    License: Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 26
Project: datacollector   File: HBaseProducer2_0.java    License: Apache License 2.0 5 votes vote down vote up
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));
  }
}
 
Example 27
Project: phoenix   File: CoprocessorHConnectionTableFactoryIT.java    License: Apache License 2.0 5 votes vote down vote up
@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);

}
 
Example 28
/**
 * @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);
}
 
Example 29
Project: DataLink   File: HbaseUtil.java    License: Apache License 2.0 5 votes vote down vote up
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 管理员.");
	}
}
 
Example 30
Project: hbase-tools   File: Manager.java    License: Apache License 2.0 5 votes vote down vote up
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;
    }
}
 
Example 31
Project: phoenix   File: UpgradeUtil.java    License: Apache License 2.0 5 votes vote down vote up
private static void deleteSequenceSnapshot(HBaseAdmin admin) throws SQLException {
    byte[] tableName = getSequenceSnapshotName();
    try {
        admin.disableTable(tableName);;
        admin.deleteTable(tableName);
    } catch (IOException e) {
        throw ServerUtil.parseServerException(e);
    }
}
 
Example 32
Project: pxf   File: HBase.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void init() throws Exception {

    super.init();

    ReportUtils.startLevel(report, getClass(), "Init");

    config = new Configuration();

    // if hbaseRoot root exists in the SUT file, load configuration from it
    if (StringUtils.isNotEmpty(hbaseRoot)) {
        config.addResource(new Path(getHbaseRoot() + "/conf/hbase-site.xml"));
    } else {
        config.set("hbase.rootdir", "hdfs://" + host + ":8020/hbase");
    }

    HBaseAdmin.checkHBaseAvailable(config);
    connection = ConnectionFactory.createConnection(config);
    admin = connection.getAdmin();
    if (admin.getClusterStatus().getServersSize() == 0) {
        ReportUtils.report(report, getClass(),
                "No HBase region servers running", Reporter.FAIL);
    }

    ReportUtils.report(report, getClass(), "HBase Admin created");

    ReportUtils.stopLevel(report);
}
 
Example 33
Project: Transwarp-Sample-Code   File: Connector.java    License: MIT License 5 votes vote down vote up
public Connector() {
        Configuration HBASE_CONFIG = new Configuration();
        Constant constant = new Constant();
        HBASE_CONFIG.addResource("hbase-site.xml");
        HBASE_CONFIG.addResource("core-site.xml");
        HBASE_CONFIG.addResource("hdfs-site.xml");
        configuration = HBaseConfiguration.create(HBASE_CONFIG);

        try {
            if (constant.MODE.equals("kerberos")) {
                UserGroupInformation.setConfiguration(configuration);
//                UserGroupInformation.loginUserFromPassword(constant.KERBEROS_USER,constant.KERBEROS_PASSWD);
                UserGroupInformation.loginUserFromKeytab(constant.KERBEROS_WITH_KEYTAB_USER,constant.KERBEROS_KEYTAB);
            }
            hBaseAdmin = new HBaseAdmin(configuration);
            hyperbaseAdmin = new HyperbaseAdmin(configuration);

            Configuration hdfsConf = getHDFSConf();
            String rootPath = "hdfs://nameservice1";
            Path p = new Path(rootPath + constant.HDFS_LARGE_FILE_DIR);
            FileSystem fs = p.getFileSystem(hdfsConf);
            boolean b = fs.mkdirs(p);
            System.out.println(b);
            fs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
Example 34
Project: phoenix   File: PhoenixSinkIT.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateTable () throws Exception {
    
    String ddl = "CREATE TABLE flume_test " +
            "  (flume_time timestamp not null, col1 varchar , col2 varchar" +
            "  CONSTRAINT pk PRIMARY KEY (flume_time))\n";

    final String fullTableName = "FLUME_TEST";
    sinkContext = new Context ();
    sinkContext.put(FlumeConstants.CONFIG_TABLE, fullTableName);
    sinkContext.put(FlumeConstants.CONFIG_JDBC_URL, getUrl());
    sinkContext.put(FlumeConstants.CONFIG_SERIALIZER,EventSerializers.REGEX.name());
    sinkContext.put(FlumeConstants.CONFIG_TABLE_DDL, ddl);
    sinkContext.put(FlumeConstants.CONFIG_SERIALIZER_PREFIX + FlumeConstants.CONFIG_REGULAR_EXPRESSION,"^([^\t]+)\t([^\t]+)$");
    sinkContext.put(FlumeConstants.CONFIG_SERIALIZER_PREFIX + FlumeConstants.CONFIG_COLUMN_NAMES,"col1,col2");
    sinkContext.put(FlumeConstants.CONFIG_SERIALIZER_PREFIX + FlumeConstants.CONFIG_ROWKEY_TYPE_GENERATOR,DefaultKeyGenerator.TIMESTAMP.name());

    
    sink = new PhoenixSink();
    Configurables.configure(sink, sinkContext);
    Assert.assertEquals(LifecycleState.IDLE, sink.getLifecycleState());
  
    final Channel channel = this.initChannel();
    sink.setChannel(channel);
    
    sink.start();
    HBaseAdmin admin = driver.getConnectionQueryServices(getUrl(), TestUtil.TEST_PROPERTIES).getAdmin();
    try {
        boolean exists = admin.tableExists(fullTableName);
        Assert.assertTrue(exists);
    }finally {
        admin.close();
    }
}
 
Example 35
Project: phoenix   File: ProductMetricsTest.java    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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();
    }
}
 
Example 36
Project: wifi   File: HbaseTestCase.java    License: Apache License 2.0 5 votes vote down vote up
public static void creat(String tablename,String columnFamily) throws Exception {
    HBaseAdmin admin = new HBaseAdmin(cfg);
    if (admin.tableExists(tablename)) {
        System.out.println("table Exists!");
        System.exit(0);
    }
    else{
        HTableDescriptor tableDesc = new HTableDescriptor(tablename);
        tableDesc.addFamily(new HColumnDescriptor(columnFamily));
        admin.createTable(tableDesc);
        System.out.println("create table success!");
    }
}
 
Example 37
Project: hbase-tools   File: Manager.java    License: Apache License 2.0 5 votes vote down vote up
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;
    }
}
 
Example 38
Project: Kylin   File: CleanHtableCLI.java    License: Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 39
Project: phoenix-omid   File: TestHBaseTimestampStorage.java    License: Apache License 2.0 5 votes vote down vote up
@AfterMethod
public void tearDown() {
    try {
        LOG.info("tearing Down");
        HBaseAdmin admin = testutil.getHBaseAdmin();
        admin.disableTable(TableName.valueOf(DEFAULT_TIMESTAMP_STORAGE_TABLE_NAME));
        admin.deleteTable(TableName.valueOf(DEFAULT_TIMESTAMP_STORAGE_TABLE_NAME));

    } catch (IOException e) {
        LOG.error("Error tearing down", e);
    }
}
 
Example 40
Project: phoenix-omid   File: TestOmidLLRaces.java    License: Apache License 2.0 5 votes vote down vote up
private void createTestTable() throws IOException {
    HBaseAdmin admin = hBaseUtils.getHBaseAdmin();
    HTableDescriptor test_table_desc = new HTableDescriptor(TableName.valueOf(TEST_TABLE));
    HColumnDescriptor datafam = new HColumnDescriptor(TEST_FAMILY);
    HColumnDescriptor datafam2 = new HColumnDescriptor(TEST_FAMILY2);
    datafam.setMaxVersions(Integer.MAX_VALUE);
    datafam2.setMaxVersions(Integer.MAX_VALUE);
    test_table_desc.addFamily(datafam);
    test_table_desc.addFamily(datafam2);
    admin.createTable(test_table_desc);
}