use of org.apache.phoenix.jdbc.PhoenixConnection in project phoenix by apache.
the class AlterTableWithViewsIT method testAlterAppendOnlySchema.
@Test
public void testAlterAppendOnlySchema() throws Exception {
try (Connection conn = DriverManager.getConnection(getUrl());
Connection viewConn = isMultiTenant ? DriverManager.getConnection(TENANT_SPECIFIC_URL1) : conn) {
String baseTableName = "NONTXNTBL_" + generateUniqueName() + (isMultiTenant ? "0" : "1");
String viewOfTable = baseTableName + "_VIEW";
String ddl = "CREATE TABLE " + baseTableName + " (\n" + "%s ID VARCHAR(15) NOT NULL,\n" + " COL1 integer NOT NULL," + "CREATED_DATE DATE,\n" + "CONSTRAINT PK PRIMARY KEY (%s ID, COL1)) %s";
conn.createStatement().execute(generateDDL(ddl));
ddl = "CREATE VIEW " + viewOfTable + " AS SELECT * FROM " + baseTableName;
viewConn.createStatement().execute(ddl);
PhoenixConnection phoenixConn = conn.unwrap(PhoenixConnection.class);
PTable table = phoenixConn.getTable(new PTableKey(null, baseTableName));
PName tenantId = isMultiTenant ? PNameFactory.newName("tenant1") : null;
assertFalse(table.isAppendOnlySchema());
PTable viewTable = viewConn.unwrap(PhoenixConnection.class).getTable(new PTableKey(tenantId, viewOfTable));
assertFalse(viewTable.isAppendOnlySchema());
try {
viewConn.createStatement().execute("ALTER VIEW " + viewOfTable + " SET APPEND_ONLY_SCHEMA = true");
fail();
} catch (SQLException e) {
assertEquals(SQLExceptionCode.CANNOT_ALTER_TABLE_PROPERTY_ON_VIEW.getErrorCode(), e.getErrorCode());
}
conn.createStatement().execute("ALTER TABLE " + baseTableName + " SET APPEND_ONLY_SCHEMA = true");
viewConn.createStatement().execute("SELECT * FROM " + viewOfTable);
phoenixConn = conn.unwrap(PhoenixConnection.class);
table = phoenixConn.getTable(new PTableKey(null, baseTableName));
assertTrue(table.isAppendOnlySchema());
viewTable = viewConn.unwrap(PhoenixConnection.class).getTable(new PTableKey(tenantId, viewOfTable));
assertTrue(viewTable.isAppendOnlySchema());
}
}
use of org.apache.phoenix.jdbc.PhoenixConnection in project phoenix by apache.
the class CreateTableIT method assertColumnEncodingMetadata.
private void assertColumnEncodingMetadata(QualifierEncodingScheme expectedEncodingScheme, ImmutableStorageScheme expectedStorageScheme, String tableName, Connection conn) throws Exception {
PhoenixConnection phxConn = conn.unwrap(PhoenixConnection.class);
PTable table = phxConn.getTable(new PTableKey(null, tableName));
assertEquals(expectedEncodingScheme, table.getEncodingScheme());
assertEquals(expectedStorageScheme, table.getImmutableStorageScheme());
}
use of org.apache.phoenix.jdbc.PhoenixConnection in project phoenix by apache.
the class ColumnEncodedBytesPropIT method testValidateProperty.
@Test
public void testValidateProperty() throws SQLException {
Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
String dataTableFullName1 = SchemaUtil.getTableName("", generateUniqueName());
String dataTableFullName2 = SchemaUtil.getTableName("", generateUniqueName());
try (Connection conn = DriverManager.getConnection(getUrl(), props)) {
Statement stmt = conn.createStatement();
String ddl = "CREATE TABLE " + dataTableFullName1 + " (id varchar not null, val varchar " + " CONSTRAINT pk PRIMARY KEY (id)) COLUMN_ENCODED_BYTES=4";
stmt.execute(ddl);
ddl = "CREATE TABLE " + dataTableFullName2 + " (id varchar not null, val varchar " + " CONSTRAINT pk PRIMARY KEY (id)) COLUMN_ENCODED_BYTES=NONE";
stmt.execute(ddl);
PhoenixConnection phxConn = conn.unwrap(PhoenixConnection.class);
PTable dataTable1 = phxConn.getTable(new PTableKey(null, dataTableFullName1));
assertEquals("Encoding scheme set incorrectly", QualifierEncodingScheme.FOUR_BYTE_QUALIFIERS, dataTable1.getEncodingScheme());
PTable dataTable2 = phxConn.getTable(new PTableKey(null, dataTableFullName2));
assertEquals("Encoding scheme set incorrectly", QualifierEncodingScheme.NON_ENCODED_QUALIFIERS, dataTable2.getEncodingScheme());
}
}
use of org.apache.phoenix.jdbc.PhoenixConnection in project phoenix by apache.
the class NamespaceSchemaMappingIT method testBackWardCompatibility.
/**
* Tests that when: There is a table created with older version of phoenix and a table created with newer version
* having {@code QueryServices#IS_NAMESPACE_MAPPING_ENABLED} true, then there is only a flag
* {@code PhoenixDatabaseMetaData#IS_NAMESPACE_MAPPED} differentiates that whether schema of the table is mapped to
* namespace or not
*/
@Test
@SuppressWarnings("deprecation")
public void testBackWardCompatibility() throws Exception {
String namespace = "TEST_SCHEMA";
String schemaName = namespace;
String tableName = generateUniqueName();
String phoenixFullTableName = schemaName + "." + tableName;
String hbaseFullTableName = schemaName + ":" + tableName;
HBaseAdmin admin = driver.getConnectionQueryServices(getUrl(), TestUtil.TEST_PROPERTIES).getAdmin();
admin.createNamespace(NamespaceDescriptor.create(namespace).build());
admin.createTable(new HTableDescriptor(TableName.valueOf(namespace, tableName)).addFamily(new HColumnDescriptor(QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES)));
admin.createTable(new HTableDescriptor(TableName.valueOf(phoenixFullTableName)).addFamily(new HColumnDescriptor(QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES)));
Put put = new Put(PVarchar.INSTANCE.toBytes(phoenixFullTableName));
put.addColumn(QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES, QueryConstants.EMPTY_COLUMN_BYTES, QueryConstants.EMPTY_COLUMN_VALUE_BYTES);
HTable phoenixSchematable = new HTable(admin.getConfiguration(), phoenixFullTableName);
phoenixSchematable.put(put);
phoenixSchematable.close();
put = new Put(PVarchar.INSTANCE.toBytes(hbaseFullTableName));
put.addColumn(QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES, QueryConstants.EMPTY_COLUMN_BYTES, QueryConstants.EMPTY_COLUMN_VALUE_BYTES);
phoenixSchematable.close();
HTable namespaceMappedtable = new HTable(admin.getConfiguration(), hbaseFullTableName);
namespaceMappedtable.put(put);
namespaceMappedtable.close();
Properties props = new Properties();
props.setProperty(QueryServices.DROP_METADATA_ATTRIB, Boolean.TRUE.toString());
Connection conn = DriverManager.getConnection(getUrl(), props);
String ddl = "create table " + phoenixFullTableName + "(tableName varchar primary key)";
conn.createStatement().execute(ddl);
String query = "select tableName from " + phoenixFullTableName;
ResultSet rs = conn.createStatement().executeQuery(query);
assertTrue(rs.next());
assertEquals(phoenixFullTableName, rs.getString(1));
HTable metatable = new HTable(admin.getConfiguration(), SchemaUtil.getPhysicalName(PhoenixDatabaseMetaData.SYSTEM_CATALOG_NAME_BYTES, (conn.unwrap(PhoenixConnection.class).getQueryServices().getProps())));
Put p = new Put(SchemaUtil.getTableKey(null, schemaName, tableName));
p.addColumn(QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES, PhoenixDatabaseMetaData.IS_NAMESPACE_MAPPED_BYTES, PBoolean.INSTANCE.toBytes(true));
metatable.put(p);
metatable.close();
PhoenixConnection phxConn = (conn.unwrap(PhoenixConnection.class));
phxConn.getQueryServices().clearCache();
rs = conn.createStatement().executeQuery(query);
assertTrue(rs.next());
assertEquals(hbaseFullTableName, rs.getString(1));
admin.disableTable(phoenixFullTableName);
admin.deleteTable(phoenixFullTableName);
conn.createStatement().execute("DROP TABLE " + phoenixFullTableName);
admin.close();
conn.close();
}
use of org.apache.phoenix.jdbc.PhoenixConnection in project phoenix by apache.
the class StatisticsCollectionRunTrackerIT method createTableAndGetRegion.
private HRegionInfo createTableAndGetRegion(String tableName) throws Exception {
byte[] tableNameBytes = Bytes.toBytes(tableName);
String ddl = "CREATE TABLE " + tableName + " (PK1 VARCHAR PRIMARY KEY, KV1 VARCHAR)";
try (Connection conn = DriverManager.getConnection(getUrl())) {
conn.createStatement().execute(ddl);
PhoenixConnection phxConn = conn.unwrap(PhoenixConnection.class);
try (HBaseAdmin admin = phxConn.getQueryServices().getAdmin()) {
List<HRegionInfo> tableRegions = admin.getTableRegions(tableNameBytes);
return tableRegions.get(0);
}
}
}
Aggregations