Java Code Examples for org.apache.hadoop.hbase.client.HBaseAdmin#createTable()

The following examples show how to use org.apache.hadoop.hbase.client.HBaseAdmin#createTable() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HBaseTable.java    From wifi with Apache License 2.0 6 votes vote down vote up
public static void create() throws Exception
{
	HBaseAdmin admin = new HBaseAdmin(cfg);
	if (admin.tableExists(tableName)) {
		System.out.println("[info]table has created!");
	} else {
		TableName table = TableName.valueOf(tableName);
		HTableDescriptor tableDescriptor = new HTableDescriptor(table);
		tableDescriptor.addFamily(new HColumnDescriptor(familyName));
		try{
			admin.createTable(tableDescriptor);
		}catch(TableExistsException e)
		{
			System.out.println("[warning] table exists!");
		}
	}
	System.out.println("[info]create table success!");
}
 
Example 2
Source File: HBaseMiniCluster.java    From yuzhouwan with 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 3
Source File: HBaseSimpleDemo.java    From bigdata-tutorial with Apache License 2.0 6 votes vote down vote up
public Boolean createTable(String tableName, String familyName) throws Exception {
	HBaseAdmin admin = new HBaseAdmin(hconn);
	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;

}
 
Example 4
Source File: HBaseTest.java    From codes-scratch-crawler with Apache License 2.0 6 votes vote down vote up
public static void createTable(String tableName) throws IOException {
  HBaseAdmin admin = new HBaseAdmin(conf);
  if (!admin.tableExists(tableName)) {
    HTableDescriptor tableDesc = new HTableDescriptor(tableName);
    tableDesc.addFamily(new HColumnDescriptor("ip:"));
    tableDesc.addFamily(new HColumnDescriptor("time:"));
    tableDesc.addFamily(new HColumnDescriptor("type:"));
    tableDesc.addFamily(new HColumnDescriptor("cookie:"));
    // 注意这个C列,一下简单以此列来说明列存储
    tableDesc.addFamily(new HColumnDescriptor("c:"));
    admin.createTable(tableDesc);
    System.out.println("table create ok!");
  } else {
    System.out.println("table already exists!");
  }
}
 
Example 5
Source File: Tailer.java    From zerowing with MIT License 6 votes vote down vote up
private HTable ensureTable(String tableName) throws Exception {
  if (_knownTables.containsKey(tableName)) {
    return _knownTables.get(tableName);
  }

  HBaseAdmin admin = getHBaseAdmin();
  if (!admin.tableExists(tableName)) {
    HTableDescriptor tableDesc = _translator.describeHBaseTable(tableName);
    admin.createTable(tableDesc);
  }

  HTable table = new HTable(_conf, tableName);

  if (_bufferWrites) {
    table.setAutoFlush(false, true);
  }

  _knownTables.put(tableName, table);
  return table;
}
 
Example 6
Source File: TestEndToEndCoveredIndexing.java    From phoenix with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Create the primary table (to which you should write), setup properly for indexing the given
 * {@link ColumnGroup}s. Also creates the necessary index tables to match the passes groups.
 * @param groups {@link ColumnGroup}s to index, creating one index table per column group.
 * @return reference to the primary table
 * @throws IOException if there is an issue communicating with HBase
 */
private HTable createSetupTables(ColumnGroup... groups) throws IOException {
  HBaseAdmin admin = UTIL.getHBaseAdmin();
  // setup the index
  CoveredColumnIndexSpecifierBuilder builder = new CoveredColumnIndexSpecifierBuilder();
  for (ColumnGroup group : groups) {
    builder.addIndexGroup(group);
    // create the index tables
    CoveredColumnIndexer.createIndexTable(admin, group.getTable());
  }

  // setup the primary table
  String indexedTableName = Bytes.toString(TestTable.getTableName());
  HTableDescriptor pTable = new HTableDescriptor(indexedTableName);
  pTable.addFamily(new HColumnDescriptor(FAM));
  pTable.addFamily(new HColumnDescriptor(FAM2));
  builder.build(pTable);

  // create the primary table
  admin.createTable(pTable);
  HTable primary = new HTable(UTIL.getConfiguration(), indexedTableName);
  primary.setAutoFlush(false);
  return primary;
}
 
Example 7
Source File: HbaseServiceImpl.java    From searchanalytics-bigdata with MIT License 6 votes vote down vote up
@Override
public void setupSearchEventsTable() {
	LOG.debug("Setting up searchclicks table!");
	String tableName = "searchclicks";
	TableName name = TableName.valueOf(tableName);
	HTableDescriptor desc = new HTableDescriptor(name);
	desc.addFamily(new HColumnDescriptor(
			HbaseJsonEventSerializer.COLUMFAMILY_CLIENT_BYTES));
	desc.addFamily(new HColumnDescriptor(
			HbaseJsonEventSerializer.COLUMFAMILY_CLIENT_BYTES));
	desc.addFamily(new HColumnDescriptor(
			HbaseJsonEventSerializer.COLUMFAMILY_SEARCH_BYTES));
	desc.addFamily(new HColumnDescriptor(
			HbaseJsonEventSerializer.COLUMFAMILY_FILTERS_BYTES));
	try {
		HBaseAdmin hBaseAdmin = new HBaseAdmin(miniHBaseCluster.getConf());
		hBaseAdmin.createTable(desc);
		hBaseAdmin.close();
	} catch (IOException e) {
		throw new RuntimeException(e);
	}
	LOG.debug("Setting up searchclicks table done!");
}
 
Example 8
Source File: OmidTestBase.java    From phoenix-omid with Apache License 2.0 5 votes vote down vote up
protected 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);
}
 
Example 9
Source File: CoprocessorToolITSuite.java    From eagle with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 10
Source File: GridTableHBaseBenchmark.java    From Kylin with Apache License 2.0 5 votes vote down vote up
private static void createHTableIfNeeded(HConnection conn, String tableName) throws IOException {
    HBaseAdmin hbase = new HBaseAdmin(conn);

    try {
        boolean tableExist = false;
        try {
            hbase.getTableDescriptor(TableName.valueOf(tableName));
            tableExist = true;
        } catch (TableNotFoundException e) {
        }

        if (tableExist) {
            System.out.println("HTable '" + tableName + "' already exists");
            return;
        }

        System.out.println("Creating HTable '" + tableName + "'");

        HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tableName));

        HColumnDescriptor fd = new HColumnDescriptor(CF);
        fd.setBlocksize(CELL_SIZE);
        desc.addFamily(fd);
        hbase.createTable(desc);

        System.out.println("HTable '" + tableName + "' created");
    } finally {
        hbase.close();
    }
}
 
Example 11
Source File: CoveredColumnIndexer.java    From phoenix with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * @param admin to create the table
 * @param index descriptor to update before creating table
 */
public static void createIndexTable(HBaseAdmin admin, HTableDescriptor index) throws IOException {
  HColumnDescriptor col =
      new HColumnDescriptor(CoveredColumnIndexCodec.INDEX_ROW_COLUMN_FAMILY);
  // ensure that we can 'see past' delete markers when doing scans
  col.setKeepDeletedCells(true);
  index.addFamily(col);
  admin.createTable(index);
}
 
Example 12
Source File: CoveredColumnIndexer.java    From phoenix with Apache License 2.0 5 votes vote down vote up
/**
 * @param admin to create the table
 * @param index descriptor to update before creating table
 */
public static void createIndexTable(HBaseAdmin admin, HTableDescriptor index) throws IOException {
  HColumnDescriptor col =
      new HColumnDescriptor(CoveredColumnIndexCodec.INDEX_ROW_COLUMN_FAMILY);
  // ensure that we can 'see past' delete markers when doing scans
  col.setKeepDeletedCells(true);
  index.addFamily(col);
  admin.createTable(index);
}
 
Example 13
Source File: TestEndToEndCoveredColumnsIndexBuilder.java    From phoenix with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * @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 14
Source File: IndexHandlerIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() throws Exception {
    HTableDescriptor desc =
            new HTableDescriptor(org.apache.hadoop.hbase.TableName.valueOf(TestTable
                    .getTableNameString()));
    desc.addFamily(FAM1);

    // create the table
    HBaseAdmin admin = UTIL.getHBaseAdmin();
    admin.createTable(desc);
}
 
Example 15
Source File: HBaseCreateTable.java    From SparkOnALog with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws IOException {
	if (args.length == 0) {
		System.out.println("CreateTable {tableName} {columnFamilyName}");
		return;
	}

	String tableName = args[0];
	String columnFamilyName = args[1];

	HBaseAdmin admin = new HBaseAdmin(new Configuration());

	HTableDescriptor tableDescriptor = new HTableDescriptor(); 
	tableDescriptor.setName(Bytes.toBytes(tableName));

	HColumnDescriptor columnDescriptor = new HColumnDescriptor(columnFamilyName);

	columnDescriptor.setCompressionType(Compression.Algorithm.SNAPPY);
	columnDescriptor.setBlocksize(64 * 1024);
	columnDescriptor.setBloomFilterType(BloomType.ROW);

	tableDescriptor.addFamily(columnDescriptor);

	//tableDescriptor.setValue(tableDescriptor.SPLIT_POLICY, ConstantSizeRegionSplitPolicy.class.getName());

	System.out.println("-Creating Table");
	admin.createTable(tableDescriptor);

	admin.close();
	System.out.println("-Done");
}
 
Example 16
Source File: BulkImportRunner.java    From zerowing with MIT License 5 votes vote down vote up
private boolean createTables() throws Exception {
  Translator translator = getTranslator();
  HBaseAdmin admin = getHBaseAdmin();
  HashSet<String> createdTables = new HashSet<String>();

  for (BulkImportJob job : _jobs) {
    String tableName = job.getOutputTableName();

    if (!createdTables.contains(tableName)) {
      if (admin.tableExists(tableName)) {
        if(ConfigUtil.getMergeExistingTable(_conf)) {
          continue;
        } else {
          log.error("Table already exists: " + tableName + ". If you want to merge with an existing table, use the --merge option");
          return false;
        }
      }

      HTableDescriptor tableDesc = translator.describeHBaseTable(tableName);

      try {
        if(ConfigUtil.getPresplitTable(_conf)) {
          byte[][] splits = calculateRegionSplits(job.getMongoURI(), tableName);
          admin.createTable(tableDesc, splits);
        } else {
          admin.createTable(tableDesc);
        }
      } catch (IOException e) {
        log.error("Failed to create table: " + tableName, e);
        return false;
      }

      createdTables.add(tableName);
    }
  }

  return true;
}
 
Example 17
Source File: Create1.java    From examples with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 18
Source File: CreateTables.java    From hadoop-arch-book with Apache License 2.0 4 votes vote down vote up
private static void createProfileCacheTable(HBaseAdmin admin) throws IOException {
  HTableDescriptor tableDescriptor = new HTableDescriptor(HBaseTableMetaModel.profileCacheTableName);

  HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(HBaseTableMetaModel.profileCacheColumnFamily);
  hColumnDescriptor.setMaxVersions(1);


  tableDescriptor.addFamily(hColumnDescriptor);
  tableDescriptor.setValue(tableDescriptor.SPLIT_POLICY, DisabledRegionSplitPolicy.class.getName());

  byte[][] splitKeys = new byte[HBaseTableMetaModel.profileCacheNumberOfProfileCacheSalts][];

  for (int i = 0; i < HBaseTableMetaModel.profileCacheNumberOfProfileCacheSalts; i++) {
    char salt = (char)('A' + i);
    splitKeys[i] = Bytes.toBytes(salt);
  }

  admin.createTable(tableDescriptor, splitKeys);
}
 
Example 19
Source File: HBaseTap.java    From SpyGlass with Apache License 2.0 4 votes vote down vote up
@Override
public boolean createResource(JobConf jobConf) throws IOException {
  HBaseAdmin hBaseAdmin = getHBaseAdmin(jobConf);

  if (hBaseAdmin.tableExists(tableName)) {
    return true;
  }

  LOG.info("Creating HBase Table: {}", tableName);

  HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);

  String[] familyNames = ((HBaseScheme) getScheme()).getFamilyNames();

  for (String familyName : familyNames) {
    tableDescriptor.addFamily(new HColumnDescriptor(familyName));
  }

  hBaseAdmin.createTable(tableDescriptor);

  return true;
}
 
Example 20
Source File: FailForUnsupportedHBaseVersionsIT.java    From phoenix with Apache License 2.0 4 votes vote down vote up
/**
 * Test that we correctly abort a RegionServer when we run tests with an unsupported HBase
 * version. The 'completeness' of this test requires that we run the test with both a version of
 * HBase that wouldn't be supported with WAL Compression. Currently, this is the default version
 * (0.94.4) so just running 'mvn test' will run the full test. However, this test will not fail
 * when running against a version of HBase with WALCompression enabled. Therefore, to fully test
 * this functionality, we need to run the test against both a supported and an unsupported version
 * of HBase (as long as we want to support an version of HBase that doesn't support custom WAL
 * Codecs).
 * @throws Exception on failure
 */
@Test(timeout = 300000 /* 5 mins */)
public void testDoesNotStartRegionServerForUnsupportedCompressionAndVersion() throws Exception {
    Configuration conf = HBaseConfiguration.create();
    setUpConfigForMiniCluster(conf);
    IndexTestingUtils.setupConfig(conf);
    // enable WAL Compression
    conf.setBoolean(HConstants.ENABLE_WAL_COMPRESSION, true);

    // check the version to see if it isn't supported
    String version = VersionInfo.getVersion();
    boolean supported = false;
    if (Indexer.validateVersion(version, conf) == null) {
        supported = true;
    }

    // start the minicluster
    HBaseTestingUtility util = new HBaseTestingUtility(conf);
    // set replication required parameter
    ConfigUtil.setReplicationConfigIfAbsent(conf);
    try {
        util.startMiniCluster();

        // setup the primary table
        @SuppressWarnings("deprecation")
        HTableDescriptor desc = new HTableDescriptor(
                "testDoesNotStartRegionServerForUnsupportedCompressionAndVersion");
        byte[] family = Bytes.toBytes("f");
        desc.addFamily(new HColumnDescriptor(family));

        // enable indexing to a non-existant index table
        String indexTableName = "INDEX_TABLE";
        ColumnGroup fam1 = new ColumnGroup(indexTableName);
        fam1.add(new CoveredColumn(family, CoveredColumn.ALL_QUALIFIERS));
        CoveredColumnIndexSpecifierBuilder builder = new CoveredColumnIndexSpecifierBuilder();
        builder.addIndexGroup(fam1);
        builder.build(desc);

        // get a reference to the regionserver, so we can ensure it aborts
        HRegionServer server = util.getMiniHBaseCluster().getRegionServer(0);

        // create the primary table
        HBaseAdmin admin = util.getHBaseAdmin();
        if (supported) {
            admin.createTable(desc);
            assertFalse("Hosting regeion server failed, even the HBase version (" + version
                    + ") supports WAL Compression.", server.isAborted());
        } else {
            admin.createTableAsync(desc, null);

            // wait for the regionserver to abort - if this doesn't occur in the timeout, assume its
            // broken.
            while (!server.isAborted()) {
                LOG.debug("Waiting on regionserver to abort..");
            }
        }
    } finally {
        // cleanup
        util.shutdownMiniCluster();
    }
}