/** * @param rawAttributeName is the attribute name viewed by applications, it allows multiple * values. For example, secondaryIndex in secondaryIndex$1 and coprocessor in corpcessor$2 * @param indexOfAttribute is of the same raw attribute name, for example 2 in secondary$2 */ static void updateTableAttribute( Configuration conf, byte[] tableName, String rawAttributeName, int indexOfAttribute, boolean ifUpdateorRemove, String value) throws IOException { HBaseAdmin admin = new HBaseAdmin(conf); HTableDescriptor desc = admin.getTableDescriptor(tableName); admin.disableTable(tableName); // System.out.println("TTDEBUG: disable table " + Bytes.toString(tableName)); String coprocessorKey = rawAttributeName + indexOfAttribute; if (!ifUpdateorRemove) { desc.remove(Bytes.toBytes(coprocessorKey)); } else { desc.setValue(coprocessorKey, value); } admin.modifyTable(tableName, desc); // System.out.println("TTDEBUG: modify table " + Bytes.toString(tableName)); admin.enableTable(tableName); // System.out.println("TTDEBUG: enable table " + Bytes.toString(tableName)); HTableDescriptor descNew = admin.getTableDescriptor(tableName); // modify table is asynchronous, has to loop over to check while (!desc.equals(descNew)) { System.err.println( "TTDEBUG: waiting for descriptor to change: from " + descNew + " to " + desc); try { Thread.sleep(500); } catch (InterruptedException ex) { } descNew = admin.getTableDescriptor(tableName); } }
/** Apply column family options such as Bloom filters, compression, and data block encoding. */ protected void applyColumnFamilyOptions(byte[] tableName, byte[][] columnFamilies) throws IOException { HBaseAdmin admin = new HBaseAdmin(conf); HTableDescriptor tableDesc = admin.getTableDescriptor(tableName); LOG.info("Disabling table " + Bytes.toString(tableName)); admin.disableTable(tableName); for (byte[] cf : columnFamilies) { HColumnDescriptor columnDesc = tableDesc.getFamily(cf); boolean isNewCf = columnDesc == null; if (isNewCf) { columnDesc = new HColumnDescriptor(cf); } if (bloomType != null) { columnDesc.setBloomFilterType(bloomType); } if (compressAlgo != null) { columnDesc.setCompressionType(compressAlgo); } if (dataBlockEncodingAlgo != null) { columnDesc.setDataBlockEncoding(dataBlockEncodingAlgo); columnDesc.setEncodeOnDisk(!encodeInCacheOnly); } if (inMemoryCF) { columnDesc.setInMemory(inMemoryCF); } if (isNewCf) { admin.addColumn(tableName, columnDesc); } else { admin.modifyColumn(tableName, columnDesc); } } LOG.info("Enabling table " + Bytes.toString(tableName)); admin.enableTable(tableName); }
public void fixInconsistent() throws IOException { if (ifFix == true) { for (String segFullName : inconsistentHTables) { String[] sepNameList = segFullName.split(","); HTableDescriptor desc = hbaseAdmin.getTableDescriptor(TableName.valueOf(sepNameList[0])); logger.info( "Change the host of htable " + sepNameList[0] + "belonging to cube " + sepNameList[1] + " from " + desc.getValue(IRealizationConstants.HTableTag) + " to " + dstCfg.getMetadataUrlPrefix()); hbaseAdmin.disableTable(sepNameList[0]); desc.setValue(IRealizationConstants.HTableTag, dstCfg.getMetadataUrlPrefix()); hbaseAdmin.modifyTable(sepNameList[0], desc); hbaseAdmin.enableTable(sepNameList[0]); } } else { logger.info("------ Inconsistent HTables Needed To Be Fixed ------"); for (String hTable : inconsistentHTables) { String[] sepNameList = hTable.split(","); logger.info(sepNameList[0] + " belonging to cube " + sepNameList[1]); } logger.info("----------------------------------------------------"); } }
@Test public void testCreateTableToBeTransactional() throws Exception { Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES); Connection conn = DriverManager.getConnection(getUrl(), props); String ddl = "CREATE TABLE TEST_TRANSACTIONAL_TABLE (k varchar primary key) transactional=true"; conn.createStatement().execute(ddl); PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class); PTable table = pconn.getTable(new PTableKey(null, "TEST_TRANSACTIONAL_TABLE")); HTableInterface htable = pconn.getQueryServices().getTable(Bytes.toBytes("TEST_TRANSACTIONAL_TABLE")); assertTrue(table.isTransactional()); assertTrue( htable .getTableDescriptor() .getCoprocessors() .contains(TransactionProcessor.class.getName())); try { ddl = "ALTER TABLE TEST_TRANSACTIONAL_TABLE SET transactional=false"; conn.createStatement().execute(ddl); fail(); } catch (SQLException e) { assertEquals(SQLExceptionCode.TX_MAY_NOT_SWITCH_TO_NON_TX.getErrorCode(), e.getErrorCode()); } HBaseAdmin admin = pconn.getQueryServices().getAdmin(); HTableDescriptor desc = new HTableDescriptor(TableName.valueOf("TXN_TEST_EXISTING")); desc.addFamily(new HColumnDescriptor(QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES)); admin.createTable(desc); ddl = "CREATE TABLE TXN_TEST_EXISTING (k varchar primary key) transactional=true"; conn.createStatement().execute(ddl); assertEquals( Boolean.TRUE.toString(), admin .getTableDescriptor(TableName.valueOf("TXN_TEST_EXISTING")) .getValue(TxConstants.READ_NON_TX_DATA)); // Should be ok, as HBase metadata should match existing metadata. ddl = "CREATE TABLE IF NOT EXISTS TEST_TRANSACTIONAL_TABLE (k varchar primary key)"; try { conn.createStatement().execute(ddl); fail(); } catch (SQLException e) { assertEquals(SQLExceptionCode.TX_MAY_NOT_SWITCH_TO_NON_TX.getErrorCode(), e.getErrorCode()); } ddl += " transactional=true"; conn.createStatement().execute(ddl); table = pconn.getTable(new PTableKey(null, "TEST_TRANSACTIONAL_TABLE")); htable = pconn.getQueryServices().getTable(Bytes.toBytes("TEST_TRANSACTIONAL_TABLE")); assertTrue(table.isTransactional()); assertTrue( htable .getTableDescriptor() .getCoprocessors() .contains(TransactionProcessor.class.getName())); }
private void populateMappingComboAndFamilyStuff() { String tableName = ""; if (!Const.isEmpty(m_existingTableNamesCombo.getText().trim())) { tableName = m_existingTableNamesCombo.getText().trim(); if (tableName.indexOf('@') > 0) { tableName = tableName.substring(0, tableName.indexOf('@')); } } // defaults if we fail to connect, table doesn't exist etc.. m_familyCI.setComboValues(new String[] {""}); m_existingMappingNamesCombo.removeAll(); if (m_admin != null && !Const.isEmpty(tableName)) { try { // first get the existing mapping names (if any) List<String> mappingNames = m_admin.getMappingNames(tableName); for (String m : mappingNames) { m_existingMappingNamesCombo.add(m); } // now get family information for this table Configuration conf = m_admin.getConnection(); HBaseAdmin admin = new HBaseAdmin(conf); if (admin.tableExists(tableName)) { HTableDescriptor descriptor = admin.getTableDescriptor(Bytes.toBytes(tableName)); Collection<HColumnDescriptor> families = descriptor.getFamilies(); String[] familyNames = new String[families.size()]; int i = 0; for (HColumnDescriptor d : families) { familyNames[i++] = d.getNameAsString(); } m_familyCI.setComboValues(familyNames); } else { m_familyCI.setComboValues(new String[] {""}); } m_familiesInvalidated = false; return; } catch (Exception e) { // TODO popup error dialog e.printStackTrace(); } } }
private HTableDescriptor ensureTableExists(String tableName) throws StorageException { HBaseAdmin adm = getAdminInterface(); HTableDescriptor desc; try { // Create our table, if necessary if (adm.tableExists(tableName)) { desc = adm.getTableDescriptor(tableName.getBytes()); } else { desc = new HTableDescriptor(tableName); adm.createTable(desc); } } catch (IOException e) { throw new TemporaryStorageException(e); } return desc; }
public RowLogShardImpl(String id, Configuration configuration, RowLog rowLog, int batchSize) throws IOException { this.id = id; this.rowLog = rowLog; this.batchSize = batchSize; String tableName = rowLog.getId() + "-" + id; HBaseAdmin admin = new HBaseAdmin(configuration); try { admin.getTableDescriptor(Bytes.toBytes(tableName)); } catch (TableNotFoundException e) { HTableDescriptor tableDescriptor = new HTableDescriptor(tableName); tableDescriptor.addFamily(new HColumnDescriptor(MESSAGES_CF)); admin.createTable(tableDescriptor); } table = new LocalHTable(configuration, tableName); }
public void check(List<String> segFullNameList) { issueExistHTables = Lists.newArrayList(); inconsistentHTables = Lists.newArrayList(); for (String segFullName : segFullNameList) { String[] sepNameList = segFullName.split(","); try { HTableDescriptor hTableDescriptor = hbaseAdmin.getTableDescriptor(TableName.valueOf(sepNameList[0])); String host = hTableDescriptor.getValue(IRealizationConstants.HTableTag); if (!dstCfg.getMetadataUrlPrefix().equalsIgnoreCase(host)) { inconsistentHTables.add(segFullName); } } catch (IOException e) { issueExistHTables.add(segFullName); continue; } } }
/** * Tests that when: 1) DDL has only pk columns 2) There is a default column family specified. * * <p>Then: 1)TTL is set for the specified default column family. */ @Test public void testCreateTableColumnFamilyHBaseAttribs7() throws Exception { String ddl = "create table IF NOT EXISTS TEST7 (" + " id char(1) NOT NULL," + " col1 integer NOT NULL," + " CONSTRAINT NAME_PK PRIMARY KEY (id, col1)" + " ) DEFAULT_COLUMN_FAMILY='a', TTL=10000, SALT_BUCKETS = 4"; long ts = nextTimestamp(); Properties props = new Properties(); props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts)); Connection conn = DriverManager.getConnection(getUrl(), props); conn.createStatement().execute(ddl); HBaseAdmin admin = driver.getConnectionQueryServices(getUrl(), props).getAdmin(); HColumnDescriptor[] columnFamilies = admin.getTableDescriptor(Bytes.toBytes("TEST7")).getColumnFamilies(); assertEquals(1, columnFamilies.length); assertEquals("a", columnFamilies[0].getNameAsString()); assertEquals(10000, columnFamilies[0].getTimeToLive()); }
/** * 添加一个列族 * * @param tableName * @param family 列族 * @throws ParamIsNullException 参数为空 * @throws TableNotFoundException 表不存在/获取表连接失败 */ public static void addColumnFamily(String tableName, HColumnDescriptor family) throws ParamIsNullException, TableNotFoundException { if (null == tableName) { throw new ParamIsNullException("tableName不能为空"); } if (null == family) { throw new ParamIsNullException("HColumnDescriptor不能为空"); } Connection conn = null; HBaseAdmin admin = null; try { conn = ConnectionFactory.createConnection(conf); admin = (HBaseAdmin) conn.getAdmin(); /** 表不存在 */ if (!admin.tableExists(Bytes.toBytes(tableName))) { throw new TableNotFoundException(tableName); } HTableDescriptor table = admin.getTableDescriptor(Bytes.toBytes(tableName)); table.addFamily(family); } catch (IOException e) { logger.error("获取Hbase连接发生异常, errMsg:{}", e.getMessage()); } finally { if (null != admin) { try { admin.close(); } catch (IOException e) { logger.error("HBaseAdmin close exception, errMsg:{}", e.getMessage()); } } if (null != conn) { try { conn.close(); } catch (IOException e) { logger.error("Connection close exception, errMsg:{}", e.getMessage()); } } } }
/** * Tests that when: 1) DDL has both pk as well as key value columns 2) Key value columns have * explicit column family names 3) Different REPLICATION_SCOPE specifiers for different column * family names. * * <p>Then: 1)REPLICATION_SCOPE is set. 2)Each explicit column family has the REPLICATION_SCOPE as * specified in DDL. */ @Test public void testCreateTableColumnFamilyHBaseAttribs5() throws Exception { String ddl = "create table IF NOT EXISTS TEST5 (" + " id char(1) NOT NULL," + " col1 integer NOT NULL," + " b.col2 bigint," + " c.col3 bigint, " + " CONSTRAINT NAME_PK PRIMARY KEY (id, col1)" + " ) b.REPLICATION_SCOPE=0, c.REPLICATION_SCOPE=1, SALT_BUCKETS = 4"; long ts = nextTimestamp(); Properties props = new Properties(); props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts)); Connection conn = DriverManager.getConnection(getUrl(), props); conn.createStatement().execute(ddl); HBaseAdmin admin = driver.getConnectionQueryServices(getUrl(), props).getAdmin(); HColumnDescriptor[] columnFamilies = admin.getTableDescriptor(Bytes.toBytes("TEST5")).getColumnFamilies(); assertEquals(2, columnFamilies.length); assertEquals("B", columnFamilies[0].getNameAsString()); assertEquals(0, columnFamilies[0].getScope()); assertEquals("C", columnFamilies[1].getNameAsString()); assertEquals(1, columnFamilies[1].getScope()); }