Java Code Examples for org.apache.hadoop.hbase.client.TableDescriptorBuilder#newBuilder()

The following examples show how to use org.apache.hadoop.hbase.client.TableDescriptorBuilder#newBuilder() . 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: IndexCoprocIT.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateOnExistingHBaseTable() throws Exception {
    String schemaName = generateUniqueName();
    String tableName = generateUniqueName();
    String indexName = generateUniqueName();
    byte[] cf = Bytes.toBytes("f");
    try (PhoenixConnection conn = getConnection()){
        TableName table = TableName.valueOf(SchemaUtil.getPhysicalHBaseTableName(schemaName,
            tableName, isNamespaceMapped).getString());
        TableDescriptorBuilder originalDescBuilder = TableDescriptorBuilder.newBuilder(table);
        ColumnFamilyDescriptorBuilder familyDescBuilder =
            ColumnFamilyDescriptorBuilder.newBuilder(cf);
        originalDescBuilder.setColumnFamily(familyDescBuilder.build());
        Admin admin = conn.getQueryServices().getAdmin();
        admin.createTable(originalDescBuilder.build());
        createBaseTable(schemaName, tableName, isMultiTenant, 0, null);
        TableDescriptor baseDescriptor = admin.getDescriptor(table);
        assertUsingNewCoprocs(baseDescriptor);
        createIndexTable(schemaName, tableName, indexName);
        baseDescriptor = admin.getDescriptor(table);
        TableName indexTable = TableName.valueOf(SchemaUtil.getPhysicalHBaseTableName(schemaName,
            indexName, isNamespaceMapped).getString());
        TableDescriptor indexDescriptor = admin.getDescriptor(indexTable);
        assertUsingNewCoprocs(baseDescriptor, indexDescriptor);
    }
}
 
Example 2
Source File: HBaseTestingUtility.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * Create a table.
 * @param htd table descriptor
 * @param families array of column families
 * @param splitKeys array of split keys
 * @param type Bloom type
 * @param blockSize block size
 * @param c Configuration to use
 * @return A Table instance for the created table.
 * @throws IOException if getAdmin or createTable fails
 */

public Table createTable(TableDescriptor htd, byte[][] families, byte[][] splitKeys,
    BloomType type, int blockSize, Configuration c) throws IOException {
  TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(htd);
  for (byte[] family : families) {
    ColumnFamilyDescriptorBuilder cfdb = ColumnFamilyDescriptorBuilder.newBuilder(family)
      .setBloomFilterType(type)
      .setBlocksize(blockSize);
    if (isNewVersionBehaviorEnabled()) {
        cfdb.setNewVersionBehavior(true);
    }
    builder.setColumnFamily(cfdb.build());
  }
  TableDescriptor td = builder.build();
  if (splitKeys != null) {
    getAdmin().createTable(td, splitKeys);
  } else {
    getAdmin().createTable(td);
  }
  // HBaseAdmin only waits for regions to appear in hbase:meta
  // we should wait until they are assigned
  waitUntilAllRegionsAssigned(td.getTableName());
  return getConnection().getTable(td.getTableName());
}
 
Example 3
Source File: TestRegionSnapshotTask.java    From hbase with Apache License 2.0 6 votes vote down vote up
private Table setupTable(TableName tableName) throws Exception {
  TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(tableName);
  // Flush many files, but do not compact immediately
  // Make sure that the region does not split
  builder
      .setMemStoreFlushSize(5000)
      .setRegionSplitPolicyClassName(ConstantSizeRegionSplitPolicy.class.getName())
      .setMaxFileSize(100 * 1024 * 1024)
      .setValue("hbase.hstore.compactionThreshold", "250");

  TableDescriptor td = builder.build();
  byte[] fam = Bytes.toBytes("fam");
  Table table = TEST_UTIL.createTable(td, new byte[][] {fam},
      TEST_UTIL.getConfiguration());
  TEST_UTIL.loadTable(table, fam);
  return table;
}
 
Example 4
Source File: TestTableScan.java    From hbase with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setUpBeforeClass() throws Exception {
  conf = TEST_UTIL.getConfiguration();
  conf.set(Constants.CUSTOM_FILTERS, "CustomFilter:" + CustomFilter.class.getName());
  TEST_UTIL.startMiniCluster();
  REST_TEST_UTIL.startServletContainer(conf);
  client = new Client(new Cluster().add("localhost",
    REST_TEST_UTIL.getServletPort()));
  Admin admin = TEST_UTIL.getAdmin();
  if (!admin.tableExists(TABLE)) {
    TableDescriptorBuilder tableDescriptorBuilder =
      TableDescriptorBuilder.newBuilder(TABLE);
    ColumnFamilyDescriptor columnFamilyDescriptor =
      ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(CFA)).build();
    tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor);
    columnFamilyDescriptor =
      ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(CFB)).build();
    tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor);
    admin.createTable(tableDescriptorBuilder.build());
    expectedRows1 = TestScannerResource.insertData(conf, TABLE, COLUMN_1, 1.0);
    expectedRows2 = TestScannerResource.insertData(conf, TABLE, COLUMN_2, 0.5);
    expectedRows3 = TestScannerResource.insertData(conf, TABLE, COLUMN_EMPTY, 1.0);
  }
}
 
Example 5
Source File: TestAccessController.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Test
public void testTableModify() throws Exception {
  AccessTestAction modifyTable = new AccessTestAction() {
    @Override
    public Object run() throws Exception {
      TableDescriptorBuilder tableDescriptorBuilder =
        TableDescriptorBuilder.newBuilder(TEST_TABLE);
      ColumnFamilyDescriptor columnFamilyDescriptor =
        ColumnFamilyDescriptorBuilder.newBuilder(TEST_FAMILY).build();
      tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor);
      columnFamilyDescriptor = ColumnFamilyDescriptorBuilder
        .newBuilder(Bytes.toBytes("fam_" + User.getCurrent().getShortName())).build();
      tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor);
      ACCESS_CONTROLLER.preModifyTable(ObserverContextImpl.createAndPrepare(CP_ENV),
          TEST_TABLE,
          null,  // not needed by AccessController
          tableDescriptorBuilder.build());
      return null;
    }
  };

  verifyAllowed(modifyTable, SUPERUSER, USER_ADMIN, USER_CREATE, USER_OWNER, USER_GROUP_CREATE,
    USER_GROUP_ADMIN);
  verifyDenied(modifyTable, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ, USER_GROUP_WRITE);
}
 
Example 6
Source File: TestScannerFromBucketCache.java    From hbase with Apache License 2.0 6 votes vote down vote up
private static HRegion initHRegion(TableName tableName, byte[] startKey, byte[] stopKey,
    String callingMethod, Configuration conf, HBaseTestingUtility testUtil, boolean isReadOnly,
    byte[]... families) throws IOException {
  RegionInfo regionInfo =
      RegionInfoBuilder.newBuilder(tableName).setStartKey(startKey).setEndKey(stopKey).build();
  TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(tableName);
  builder.setReadOnly(isReadOnly).setDurability(Durability.SYNC_WAL);
  for (byte[] family : families) {
    builder.setColumnFamily(
        ColumnFamilyDescriptorBuilder.newBuilder(family).setMaxVersions(Integer.MAX_VALUE)
            .build());
  }
  return HBaseTestingUtility
      .createRegionAndWAL(regionInfo, testUtil.getDataTestDir(callingMethod), conf,
          builder.build(), BlockCacheFactory.createBlockCache(conf));
}
 
Example 7
Source File: TestNamespaceReplication.java    From hbase with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void setUpBeforeClass() throws Exception {
  TestReplicationBase.setUpBeforeClass();

  connection1 = ConnectionFactory.createConnection(CONF1);
  connection2 = ConnectionFactory.createConnection(CONF2);
  admin1 = connection1.getAdmin();
  admin2 = connection2.getAdmin();

  admin1.createNamespace(NamespaceDescriptor.create(ns1).build());
  admin1.createNamespace(NamespaceDescriptor.create(ns2).build());
  admin2.createNamespace(NamespaceDescriptor.create(ns1).build());
  admin2.createNamespace(NamespaceDescriptor.create(ns2).build());

  TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(tabAName);
  builder.setColumnFamily(ColumnFamilyDescriptorBuilder
    .newBuilder(f1Name).setScope(HConstants.REPLICATION_SCOPE_GLOBAL).build());
  builder.setColumnFamily(ColumnFamilyDescriptorBuilder
    .newBuilder(f2Name).setScope(HConstants.REPLICATION_SCOPE_GLOBAL).build());
  TableDescriptor tabA = builder.build();
  admin1.createTable(tabA);
  admin2.createTable(tabA);

  builder = TableDescriptorBuilder.newBuilder(tabBName);
  builder.setColumnFamily(ColumnFamilyDescriptorBuilder
    .newBuilder(f1Name).setScope(HConstants.REPLICATION_SCOPE_GLOBAL).build());
  builder.setColumnFamily(ColumnFamilyDescriptorBuilder
    .newBuilder(f2Name).setScope(HConstants.REPLICATION_SCOPE_GLOBAL).build());
  TableDescriptor tabB = builder.build();
  admin1.createTable(tabB);
  admin2.createTable(tabB);
}
 
Example 8
Source File: TestMultiRowResource.java    From hbase with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void setUpBeforeClass() throws Exception {
  conf = TEST_UTIL.getConfiguration();
  conf.setBoolean(RESTServer.REST_CSRF_ENABLED_KEY, csrfEnabled);
  if (csrfEnabled) {
    conf.set(RESTServer.REST_CSRF_BROWSER_USERAGENTS_REGEX_KEY, ".*");
  }
  extraHdr = new BasicHeader(RESTServer.REST_CSRF_CUSTOM_HEADER_DEFAULT, "");
  TEST_UTIL.startMiniCluster();
  REST_TEST_UTIL.startServletContainer(conf);
  context = JAXBContext.newInstance(
          CellModel.class,
          CellSetModel.class,
          RowModel.class);
  marshaller = context.createMarshaller();
  unmarshaller = context.createUnmarshaller();
  client = new Client(new Cluster().add("localhost", REST_TEST_UTIL.getServletPort()));
  Admin admin = TEST_UTIL.getAdmin();
  if (admin.tableExists(TABLE)) {
    return;
  }
  TableDescriptorBuilder tableDescriptorBuilder =
    TableDescriptorBuilder.newBuilder(TABLE);
  ColumnFamilyDescriptor columnFamilyDescriptor =
    ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(CFA)).build();
  tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor);
  columnFamilyDescriptor =
    ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(CFB)).build();
  tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor);
  admin.createTable(tableDescriptorBuilder.build());
}
 
Example 9
Source File: TestCleanupCompactedFileAfterFailover.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Before
public void before() throws Exception {
  TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(TABLE_NAME);
  builder.setColumnFamily(ColumnFamilyDescriptorBuilder.of(FAMILY));
  admin.createTable(builder.build());
  TEST_UTIL.waitTableAvailable(TABLE_NAME);
  table = TEST_UTIL.getConnection().getTable(TABLE_NAME);
}
 
Example 10
Source File: TestNamespaceAuditor.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test
public void testCloneSnapshot() throws Exception {
  String nsp = prefix + "_testCloneSnapshot";
  NamespaceDescriptor nspDesc =
      NamespaceDescriptor.create(nsp).addConfiguration(TableNamespaceManager.KEY_MAX_TABLES, "2")
          .addConfiguration(TableNamespaceManager.KEY_MAX_REGIONS, "20").build();
  ADMIN.createNamespace(nspDesc);
  assertNotNull("Namespace descriptor found null.", ADMIN.getNamespaceDescriptor(nsp));
  TableName tableName = TableName.valueOf(nsp + TableName.NAMESPACE_DELIM + "table1");
  TableName cloneTableName = TableName.valueOf(nsp + TableName.NAMESPACE_DELIM + "table2");

  ColumnFamilyDescriptor columnFamilyDescriptor =
    ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("fam1")).build();
  TableDescriptorBuilder tableDescOne = TableDescriptorBuilder
    .newBuilder(tableName);
  tableDescOne.setColumnFamily(columnFamilyDescriptor);

  ADMIN.createTable(tableDescOne.build(), Bytes.toBytes("AAA"), Bytes.toBytes("ZZZ"), 4);
  String snapshot = "snapshot_testCloneSnapshot";
  ADMIN.snapshot(snapshot, tableName);
  ADMIN.cloneSnapshot(snapshot, cloneTableName);

  int tableLength;
  try (RegionLocator locator = ADMIN.getConnection().getRegionLocator(tableName)) {
    tableLength = locator.getStartKeys().length;
  }
  assertEquals(tableName.getNameAsString() + " should have four regions.", 4, tableLength);

  try (RegionLocator locator = ADMIN.getConnection().getRegionLocator(cloneTableName)) {
    tableLength = locator.getStartKeys().length;
  }
  assertEquals(cloneTableName.getNameAsString() + " should have four regions.", 4, tableLength);

  NamespaceTableAndRegionInfo nstate = getNamespaceState(nsp);
  assertEquals("Total tables count should be 2.", 2, nstate.getTables().size());
  assertEquals("Total regions count should be.", 8, nstate.getRegionCount());

  ADMIN.deleteSnapshot(snapshot);
}
 
Example 11
Source File: OfflineMetaRebuildTestCore.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Setup a clean table before we start mucking with it.
 *
 * @throws IOException
 * @throws InterruptedException
 * @throws KeeperException
 */
private Table setupTable(TableName tablename) throws Exception {
  TableDescriptorBuilder tableDescriptorBuilder =
    TableDescriptorBuilder.newBuilder(tablename);
  ColumnFamilyDescriptor columnFamilyDescriptor =
    ColumnFamilyDescriptorBuilder.newBuilder(FAM).build();
  // If a table has no CF's it doesn't get checked
  tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor);
  TEST_UTIL.getAdmin().createTable(tableDescriptorBuilder.build(), splits);
  return this.connection.getTable(tablename);
}
 
Example 12
Source File: DropMetadataIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Test
public void testDropViewKeepsHTable() throws Exception {
    Connection conn = getConnection();
    Admin admin = conn.unwrap(PhoenixConnection.class).getQueryServices().getAdmin();
    String hbaseNativeViewName = generateUniqueName();

    byte[] hbaseNativeBytes = SchemaUtil.getTableNameAsBytes(HBASE_NATIVE_SCHEMA_NAME, hbaseNativeViewName);
    try {
         TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(TableName.valueOf(hbaseNativeBytes));
        ColumnFamilyDescriptor columnDescriptor = ColumnFamilyDescriptorBuilder.newBuilder(FAMILY_NAME)
                .setKeepDeletedCells(KeepDeletedCells.TRUE).build();
        builder.addColumnFamily(columnDescriptor);
        admin.createTable(builder.build());
    } finally {
        admin.close();
    }
    
    conn.createStatement().execute("create view " + hbaseNativeViewName+
            "   (uint_key unsigned_int not null," +
            "    ulong_key unsigned_long not null," +
            "    string_key varchar not null,\n" +
            "    \"1\".uint_col unsigned_int," +
            "    \"1\".ulong_col unsigned_long" +
            "    CONSTRAINT pk PRIMARY KEY (uint_key, ulong_key, string_key))\n" +
            ColumnFamilyDescriptorBuilder.DATA_BLOCK_ENCODING + "='" + DataBlockEncoding.NONE + "'");
    conn.createStatement().execute("drop view " + hbaseNativeViewName);
    conn.close();
}
 
Example 13
Source File: CoveredColumnIndexSpecifierBuilder.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public TableDescriptor build(TableDescriptor desc, Class<? extends IndexCodec> clazz) throws IOException {
  // add the codec for the index to the map of options
  Map<String, String> opts = this.convertToMap();
  opts.put(NonTxIndexBuilder.CODEC_CLASS_NAME_KEY, clazz.getName());
      TableDescriptorBuilder newBuilder = TableDescriptorBuilder.newBuilder(desc);
      Indexer.enableIndexing(newBuilder, NonTxIndexBuilder.class, opts, Coprocessor.PRIORITY_USER);
      return newBuilder.build();
}
 
Example 14
Source File: TestHFileOutputFormat2.java    From hbase with Apache License 2.0 5 votes vote down vote up
private void setupMockColumnFamiliesForBlockSize(Table table,
    Map<String, Integer> familyToDataBlockEncoding) throws IOException {
  TableDescriptorBuilder mockTableDescriptor =
    TableDescriptorBuilder.newBuilder(TABLE_NAMES[0]);
  for (Entry<String, Integer> entry : familyToDataBlockEncoding.entrySet()) {
    ColumnFamilyDescriptor columnFamilyDescriptor = ColumnFamilyDescriptorBuilder
      .newBuilder(Bytes.toBytes(entry.getKey()))
      .setMaxVersions(1)
      .setBlocksize(entry.getValue())
      .setBlockCacheEnabled(false)
      .setTimeToLive(0).build();
    mockTableDescriptor.setColumnFamily(columnFamilyDescriptor);
  }
  Mockito.doReturn(mockTableDescriptor).when(table).getDescriptor();
}
 
Example 15
Source File: TestCoprocessorMetrics.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test
public void testWALObserver() throws IOException {
  // Find out the MetricRegistry used by the CP using the global registries
  MetricRegistryInfo info = MetricsCoprocessor.createRegistryInfoForWALCoprocessor(
      CustomWALObserver.class.getName());

  Optional<MetricRegistry> registry =  MetricRegistries.global().get(info);
  assertTrue(registry.isPresent());

  Optional<Metric> metric = registry.get().get("walEditsCount");
  assertTrue(metric.isPresent());

  try (Connection connection = ConnectionFactory.createConnection(UTIL.getConfiguration());
       Admin admin = connection.getAdmin()) {
    TableDescriptorBuilder tableDescriptorBuilder =
      TableDescriptorBuilder.newBuilder(TableName.valueOf(name.getMethodName()));
    ColumnFamilyDescriptor columnFamilyDescriptor =
      ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("foo")).build();
    tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor);
    admin.createTable(tableDescriptorBuilder.build());

    Counter rollWalRequests = (Counter)metric.get();
    long prevCount = rollWalRequests.getCount();
    assertTrue(prevCount > 0);

    try (Table table = connection.getTable(TableName.valueOf(name.getMethodName()))) {
      table.put(new Put(foo).addColumn(foo, foo, foo));
    }

    assertEquals(1, rollWalRequests.getCount() - prevCount);
  }
}
 
Example 16
Source File: TestFSTableDescriptors.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test
public void testTableDescriptorsNoCache()
  throws IOException, InterruptedException {
  final String name = this.name.getMethodName();
  FileSystem fs = FileSystem.get(UTIL.getConfiguration());
  // Cleanup old tests if any debris laying around.
  Path rootdir = new Path(UTIL.getDataTestDir(), name);
  FSTableDescriptors htds = new FSTableDescriptorsTest(fs, rootdir, false);
  final int count = 10;
  // Write out table infos.
  for (int i = 0; i < count; i++) {
    htds.createTableDescriptor(TableDescriptorBuilder.newBuilder(TableName.valueOf(name + i)).build());
  }

  for (int i = 0; i < 2 * count; i++) {
    assertNotNull("Expected HTD, got null instead", htds.get(TableName.valueOf(name + i % 2)));
  }
  // Update the table infos
  for (int i = 0; i < count; i++) {
    TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(TableName.valueOf(name + i));
    builder.setColumnFamily(ColumnFamilyDescriptorBuilder.of("" + i));
    htds.updateTableDescriptor(builder.build());
  }
  for (int i = 0; i < count; i++) {
    assertNotNull("Expected HTD, got null instead", htds.get(TableName.valueOf(name + i)));
    assertTrue("Column Family " + i + " missing",
               htds.get(TableName.valueOf(name + i)).hasColumnFamily(Bytes.toBytes("" + i)));
  }
  assertEquals(count * 4, htds.invocations);
  assertEquals("expected=0, actual=" + htds.cachehits, 0, htds.cachehits);
}
 
Example 17
Source File: TestFSTableDescriptors.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Test public void testTableDescriptors()
throws IOException, InterruptedException {
  final String name = this.name.getMethodName();
  FileSystem fs = FileSystem.get(UTIL.getConfiguration());
  // Cleanup old tests if any debris laying around.
  Path rootdir = new Path(UTIL.getDataTestDir(), name);
  FSTableDescriptors htds = new FSTableDescriptors(fs, rootdir) {
    @Override
    public TableDescriptor get(TableName tablename)
        throws TableExistsException, FileNotFoundException, IOException {
      LOG.info(tablename + ", cachehits=" + this.cachehits);
      return super.get(tablename);
    }
  };
  final int count = 10;
  // Write out table infos.
  for (int i = 0; i < count; i++) {
    htds.createTableDescriptor(TableDescriptorBuilder.newBuilder(TableName.valueOf(name + i)).build());
  }

  for (int i = 0; i < count; i++) {
    assertTrue(htds.get(TableName.valueOf(name + i)) !=  null);
  }
  for (int i = 0; i < count; i++) {
    assertTrue(htds.get(TableName.valueOf(name + i)) !=  null);
  }
  // Update the table infos
  for (int i = 0; i < count; i++) {
    TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(TableName.valueOf(name + i));
    builder.setColumnFamily(ColumnFamilyDescriptorBuilder.of("" + i));
    htds.updateTableDescriptor(builder.build());
  }
  // Wait a while so mod time we write is for sure different.
  Thread.sleep(100);
  for (int i = 0; i < count; i++) {
    assertTrue(htds.get(TableName.valueOf(name + i)) !=  null);
  }
  for (int i = 0; i < count; i++) {
    assertTrue(htds.get(TableName.valueOf(name + i)) !=  null);
  }
  assertEquals(count * 4, htds.invocations);
  assertTrue("expected=" + (count * 2) + ", actual=" + htds.cachehits,
    htds.cachehits >= (count * 2));
}
 
Example 18
Source File: TestModifyTableProcedure.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Test
public void testRecoveryAndDoubleExecutionOnline() throws Exception {
  final TableName tableName = TableName.valueOf(name.getMethodName());
  final String cf2 = "cf2";
  final String cf3 = "cf3";
  final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();

  // create the table
  RegionInfo[] regions = MasterProcedureTestingUtility.createTable(
    procExec, tableName, null, "cf1", cf3);

  ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, true);

  // Modify multiple properties of the table.
  TableDescriptorBuilder tableDescriptorBuilder =
    TableDescriptorBuilder.newBuilder(UTIL.getAdmin().getDescriptor(tableName));
  ColumnFamilyDescriptor columnFamilyDescriptor =
    ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(cf2)).build();
  boolean newCompactionEnableOption = !tableDescriptorBuilder.build().isCompactionEnabled();
  tableDescriptorBuilder.setCompactionEnabled(newCompactionEnableOption);
  tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor);
  tableDescriptorBuilder.removeColumnFamily(Bytes.toBytes(cf3));

  // Start the Modify procedure && kill the executor
  long procId = procExec.submitProcedure(
    new ModifyTableProcedure(procExec.getEnvironment(), tableDescriptorBuilder.build()));

  // Restart the executor and execute the step twice
  MasterProcedureTestingUtility.testRecoveryAndDoubleExecution(procExec, procId);

  // Validate descriptor
  HTableDescriptor currentHtd = new HTableDescriptor(UTIL.getAdmin().getDescriptor(tableName));
  assertEquals(newCompactionEnableOption, currentHtd.isCompactionEnabled());
  assertEquals(2, currentHtd.getFamiliesKeys().size());
  assertTrue(currentHtd.hasFamily(Bytes.toBytes(cf2)));
  assertFalse(currentHtd.hasFamily(Bytes.toBytes(cf3)));

  // cf2 should be added cf3 should be removed
  MasterProcedureTestingUtility.validateTableCreation(UTIL.getHBaseCluster().getMaster(),
    tableName, regions, "cf1", cf2);
}
 
Example 19
Source File: MappingTableDataTypeIT.java    From phoenix with Apache License 2.0 4 votes vote down vote up
@Test
public void testMappingHbaseTableToPhoenixTable() throws Exception {
    String mtest = generateUniqueName();
    final TableName tableName = TableName.valueOf(mtest);
    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
    PhoenixConnection conn = DriverManager.getConnection(getUrl(), props).unwrap(PhoenixConnection.class);
    
    Admin admin = conn.getQueryServices().getAdmin();
    try {
        // Create table then get the single region for our new table.
        TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(tableName);
        builder.addColumnFamily(ColumnFamilyDescriptorBuilder.of(Bytes.toBytes("cf1")))
                .addColumnFamily(ColumnFamilyDescriptorBuilder.of(Bytes.toBytes("cf2")));
        admin.createTable(builder.build());
        Table t = conn.getQueryServices().getTable(Bytes.toBytes(mtest));
        insertData(tableName.getName(), admin, t);
        t.close();
        // create phoenix table that maps to existing HBase table
        createPhoenixTable(mtest);
        
        String selectSql = "SELECT * FROM " + mtest;
        ResultSet rs = conn.createStatement().executeQuery(selectSql);
        ResultSetMetaData rsMetaData = rs.getMetaData();
        assertTrue("Expected single row", rs.next());
        // verify values from cf2 is not returned
        assertEquals("Number of columns", 2, rsMetaData.getColumnCount());
        assertEquals("Column Value", "value1", rs.getString(2));
        assertFalse("Expected single row ", rs.next());
        
        // delete the row
        String deleteSql = "DELETE FROM " + mtest + " WHERE id = 'row'";
        conn.createStatement().executeUpdate(deleteSql);
        conn.commit();
        
        // verify that no rows are returned when querying through phoenix
        rs = conn.createStatement().executeQuery(selectSql);
        assertFalse("Expected no row` ", rs.next());
        
        // verify that row with value for cf2 still exists when using hbase apis
        Scan scan = new Scan();
        ResultScanner results = t.getScanner(scan);
        Result result = results.next();
        assertNotNull("Expected single row", result);
        List<Cell> kvs = result.getColumnCells(Bytes.toBytes("cf2"), Bytes.toBytes("q2"));
        assertEquals("Expected single value ", 1, kvs.size());
        assertEquals("Column Value", "value2", Bytes.toString(kvs.get(0).getValueArray(), kvs.get(0).getValueOffset(), kvs.get(0).getValueLength()));
        assertNull("Expected single row", results.next());
    } finally {
        admin.close();
    }
}
 
Example 20
Source File: TestMultipleColumnPrefixFilter.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Test
public void testMultipleColumnPrefixFilterWithColumnPrefixFilter() throws IOException {
  String family = "Family";
  TableDescriptorBuilder tableDescriptorBuilder =
    TableDescriptorBuilder.newBuilder(TableName.valueOf(name.getMethodName()));
  ColumnFamilyDescriptor columnFamilyDescriptor =
    ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(family)).build();
  tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor);
  TableDescriptor tableDescriptor = tableDescriptorBuilder.build();
  RegionInfo info = RegionInfoBuilder.newBuilder(tableDescriptor.getTableName()).build();
  HRegion region = HBaseTestingUtility.createRegionAndWAL(info, TEST_UTIL.
    getDataTestDir(), TEST_UTIL.getConfiguration(), tableDescriptor);

  List<String> rows = generateRandomWords(100, "row");
  List<String> columns = generateRandomWords(10000, "column");
  long maxTimestamp = 2;

  String valueString = "ValueString";

  for (String row: rows) {
    Put p = new Put(Bytes.toBytes(row));
    p.setDurability(Durability.SKIP_WAL);
    for (String column: columns) {
      for (long timestamp = 1; timestamp <= maxTimestamp; timestamp++) {
        KeyValue kv = KeyValueTestUtil.create(row, family, column, timestamp,
            valueString);
        p.add(kv);
      }
    }
    region.put(p);
  }

  MultipleColumnPrefixFilter multiplePrefixFilter;
  Scan scan1 = new Scan();
  scan1.readAllVersions();
  byte [][] filter_prefix = new byte [1][];
  filter_prefix[0] = new byte [] {'p'};

  multiplePrefixFilter = new MultipleColumnPrefixFilter(filter_prefix);
  scan1.setFilter(multiplePrefixFilter);
  List<Cell> results1 = new ArrayList<>();
  InternalScanner scanner1 = region.getScanner(scan1);
  while (scanner1.next(results1))
    ;

  ColumnPrefixFilter singlePrefixFilter;
  Scan scan2 = new Scan();
  scan2.readAllVersions();
  singlePrefixFilter = new ColumnPrefixFilter(Bytes.toBytes("p"));

  scan2.setFilter(singlePrefixFilter);
  List<Cell> results2 = new ArrayList<>();
  InternalScanner scanner2 = region.getScanner(scan1);
  while (scanner2.next(results2))
    ;

  assertEquals(results1.size(), results2.size());

  HBaseTestingUtility.closeRegionAndWAL(region);
}