Search in sources :

Example 6 with TableAlreadyExistsException

use of org.apache.phoenix.schema.TableAlreadyExistsException in project phoenix by apache.

the class BaseTest method createTestTable.

protected static void createTestTable(String url, String ddl, byte[][] splits, Long ts, boolean swallowTableAlreadyExistsException) throws SQLException {
    assertNotNull(ddl);
    StringBuilder buf = new StringBuilder(ddl);
    if (splits != null) {
        buf.append(" SPLIT ON (");
        for (int i = 0; i < splits.length; i++) {
            buf.append("'").append(Bytes.toString(splits[i])).append("'").append(",");
        }
        buf.setCharAt(buf.length() - 1, ')');
    }
    ddl = buf.toString();
    Properties props = new Properties();
    if (ts != null) {
        props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts));
    }
    Connection conn = DriverManager.getConnection(url, props);
    try {
        conn.createStatement().execute(ddl);
    } catch (TableAlreadyExistsException e) {
        if (!swallowTableAlreadyExistsException) {
            throw e;
        }
    } finally {
        conn.close();
    }
}
Also used : TableAlreadyExistsException(org.apache.phoenix.schema.TableAlreadyExistsException) NewerTableAlreadyExistsException(org.apache.phoenix.schema.NewerTableAlreadyExistsException) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) Connection(java.sql.Connection) Properties(java.util.Properties)

Example 7 with TableAlreadyExistsException

use of org.apache.phoenix.schema.TableAlreadyExistsException in project phoenix by apache.

the class ConnectionlessQueryServicesImpl method init.

// TODO: share this with ConnectionQueryServicesImpl
@Override
public void init(String url, Properties props) throws SQLException {
    if (initialized) {
        if (initializationException != null) {
            throw initializationException;
        }
        return;
    }
    synchronized (this) {
        if (initialized) {
            if (initializationException != null) {
                throw initializationException;
            }
            return;
        }
        SQLException sqlE = null;
        PhoenixConnection metaConnection = null;
        try {
            Properties scnProps = PropertiesUtil.deepCopy(props);
            scnProps.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP));
            scnProps.remove(PhoenixRuntime.TENANT_ID_ATTRIB);
            String globalUrl = JDBCUtil.removeProperty(url, PhoenixRuntime.TENANT_ID_ATTRIB);
            metaConnection = new PhoenixConnection(this, globalUrl, scnProps, newEmptyMetaData());
            try {
                metaConnection.createStatement().executeUpdate(QueryConstants.CREATE_TABLE_METADATA);
            } catch (TableAlreadyExistsException ignore) {
            // Ignore, as this will happen if the SYSTEM.TABLE already exists at this fixed timestamp.
            // A TableAlreadyExistsException is not thrown, since the table only exists *after* this fixed timestamp.
            }
            try {
                int nSaltBuckets = getSequenceSaltBuckets();
                String createTableStatement = Sequence.getCreateTableStatement(nSaltBuckets);
                metaConnection.createStatement().executeUpdate(createTableStatement);
            } catch (NewerTableAlreadyExistsException ignore) {
            // Ignore, as this will happen if the SYSTEM.SEQUENCE already exists at this fixed timestamp.
            // A TableAlreadyExistsException is not thrown, since the table only exists *after* this fixed timestamp.
            }
            try {
                metaConnection.createStatement().executeUpdate(QueryConstants.CREATE_STATS_TABLE_METADATA);
            } catch (NewerTableAlreadyExistsException ignore) {
            // Ignore, as this will happen if the SYSTEM.SEQUENCE already exists at this fixed
            // timestamp.
            // A TableAlreadyExistsException is not thrown, since the table only exists *after* this
            // fixed timestamp.
            }
            try {
                metaConnection.createStatement().executeUpdate(QueryConstants.CREATE_FUNCTION_METADATA);
            } catch (NewerTableAlreadyExistsException ignore) {
            }
        } catch (SQLException e) {
            sqlE = e;
        } finally {
            try {
                if (metaConnection != null)
                    metaConnection.close();
            } catch (SQLException e) {
                if (sqlE != null) {
                    sqlE.setNextException(e);
                } else {
                    sqlE = e;
                }
            } finally {
                try {
                    if (sqlE != null) {
                        initializationException = sqlE;
                        throw sqlE;
                    }
                } finally {
                    initialized = true;
                }
            }
        }
    }
}
Also used : TableAlreadyExistsException(org.apache.phoenix.schema.TableAlreadyExistsException) NewerTableAlreadyExistsException(org.apache.phoenix.schema.NewerTableAlreadyExistsException) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) SQLException(java.sql.SQLException) Properties(java.util.Properties) NewerTableAlreadyExistsException(org.apache.phoenix.schema.NewerTableAlreadyExistsException)

Example 8 with TableAlreadyExistsException

use of org.apache.phoenix.schema.TableAlreadyExistsException in project phoenix by apache.

the class AutoPartitionViewsIT method testViewCreationFailure.

@Test
public void testViewCreationFailure() throws SQLException {
    try (Connection conn = DriverManager.getConnection(getUrl());
        Connection viewConn1 = isMultiTenant ? DriverManager.getConnection(TENANT_SPECIFIC_URL1) : DriverManager.getConnection(getUrl());
        Connection viewConn2 = isMultiTenant ? DriverManager.getConnection(TENANT_SPECIFIC_URL2) : DriverManager.getConnection(getUrl())) {
        String tableName = generateUniqueName();
        String autoSeqName = generateUniqueName();
        String ddl = String.format("CREATE TABLE " + tableName + " (%s metricId INTEGER NOT NULL, val1 DOUBLE, val2 DOUBLE CONSTRAINT PK PRIMARY KEY( %s metricId)) %s", isMultiTenant ? "tenantId VARCHAR NOT NULL, " : "", isMultiTenant ? "tenantId, " : "", String.format(tableDDLOptions, autoSeqName));
        conn.createStatement().execute(ddl);
        conn.createStatement().execute("CREATE SEQUENCE " + autoSeqName + " CACHE 1");
        String baseViewName = generateUniqueName();
        String metricView1 = baseViewName + "_VIEW1";
        String metricView2 = baseViewName + "_VIEW2";
        // create a view
        viewConn1.createStatement().execute("CREATE VIEW " + metricView1 + " AS SELECT * FROM " + tableName + " WHERE val2=1.2");
        try {
            // create the same view which should fail
            viewConn1.createStatement().execute("CREATE VIEW " + metricView1 + " AS SELECT * FROM " + tableName);
            fail("view should already exist");
        } catch (TableAlreadyExistsException e) {
        }
        // create a second view (without a where clause)
        viewConn2.createStatement().execute("CREATE VIEW " + metricView2 + " AS SELECT * FROM " + tableName);
        // upsert a row into each view
        viewConn1.createStatement().execute("UPSERT INTO " + metricView1 + "(val1) VALUES(1.1)");
        viewConn1.commit();
        viewConn2.createStatement().execute("UPSERT INTO " + metricView2 + "(val1,val2) VALUES(2.1,2.2)");
        viewConn2.commit();
        // query the base table
        ResultSet rs = conn.createStatement().executeQuery("SELECT * FROM " + tableName);
        assertTrue(rs.next());
        int offset = 0;
        if (isMultiTenant) {
            assertEquals("tenant1", rs.getString(1));
            offset = 1;
        }
        assertEquals(1, rs.getInt(1 + offset));
        assertEquals(1.1, rs.getDouble(2 + offset), 1e-6);
        assertEquals(1.2, rs.getDouble(3 + offset), 1e-6);
        assertTrue(rs.next());
        // validate that the auto partition sequence was not incremented even though view creation failed
        if (isMultiTenant) {
            assertEquals("tenant2", rs.getString(1));
        }
        assertEquals(2, rs.getInt(1 + offset));
        assertEquals(2.1, rs.getDouble(2 + offset), 1e-6);
        assertEquals(2.2, rs.getDouble(3 + offset), 1e-6);
        assertFalse(rs.next());
        // query the first view
        rs = viewConn1.createStatement().executeQuery("SELECT * FROM " + metricView1);
        assertTrue(rs.next());
        assertEquals(1, rs.getInt(1));
        assertEquals(1.1, rs.getDouble(2), 1e-6);
        assertEquals(1.2, rs.getDouble(3), 1e-6);
        assertFalse(rs.next());
        // query the second view
        rs = viewConn2.createStatement().executeQuery("SELECT * FROM " + metricView2);
        assertTrue(rs.next());
        assertEquals(2, rs.getInt(1));
        assertEquals(2.1, rs.getDouble(2), 1e-6);
        assertEquals(2.2, rs.getDouble(3), 1e-6);
        assertFalse(rs.next());
    }
}
Also used : TableAlreadyExistsException(org.apache.phoenix.schema.TableAlreadyExistsException) Connection(java.sql.Connection) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) ResultSet(java.sql.ResultSet) Test(org.junit.Test)

Example 9 with TableAlreadyExistsException

use of org.apache.phoenix.schema.TableAlreadyExistsException in project phoenix by apache.

the class ConnectionQueryServicesImpl method ensureViewIndexTableCreated.

private void ensureViewIndexTableCreated(byte[] physicalTableName, Map<String, Object> tableProps, List<Pair<byte[], Map<String, Object>>> families, byte[][] splits, long timestamp, boolean isNamespaceMapped) throws SQLException {
    byte[] physicalIndexName = MetaDataUtil.getViewIndexPhysicalName(physicalTableName);
    tableProps.put(MetaDataUtil.IS_VIEW_INDEX_TABLE_PROP_NAME, TRUE_BYTES_AS_STRING);
    HTableDescriptor desc = ensureTableCreated(physicalIndexName, PTableType.TABLE, tableProps, families, splits, false, isNamespaceMapped);
    if (desc != null) {
        if (!Boolean.TRUE.equals(PBoolean.INSTANCE.toObject(desc.getValue(MetaDataUtil.IS_VIEW_INDEX_TABLE_PROP_BYTES)))) {
            String fullTableName = Bytes.toString(physicalIndexName);
            throw new TableAlreadyExistsException("Unable to create shared physical table for indexes on views.", SchemaUtil.getSchemaNameFromFullName(fullTableName), SchemaUtil.getTableNameFromFullName(fullTableName));
        }
    }
}
Also used : TableAlreadyExistsException(org.apache.phoenix.schema.TableAlreadyExistsException) NewerTableAlreadyExistsException(org.apache.phoenix.schema.NewerTableAlreadyExistsException) HTableDescriptor(org.apache.hadoop.hbase.HTableDescriptor)

Example 10 with TableAlreadyExistsException

use of org.apache.phoenix.schema.TableAlreadyExistsException in project phoenix by apache.

the class CreateTableIT method testCreateTable.

@Test
public void testCreateTable() throws Exception {
    long ts = nextTimestamp();
    String schemaName = "TEST";
    String tableName = schemaName + ".M_INTERFACE_JOB";
    Properties props = new Properties();
    props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts));
    String ddl = "CREATE TABLE " + tableName + "(                data.addtime VARCHAR ,\n" + "                data.dir VARCHAR ,\n" + "                data.end_time VARCHAR ,\n" + "                data.file VARCHAR ,\n" + "                data.fk_log VARCHAR ,\n" + "                data.host VARCHAR ,\n" + "                data.r VARCHAR ,\n" + "                data.size VARCHAR ,\n" + "                data.start_time VARCHAR ,\n" + "                data.stat_date DATE ,\n" + "                data.stat_hour VARCHAR ,\n" + "                data.stat_minute VARCHAR ,\n" + "                data.state VARCHAR ,\n" + "                data.title VARCHAR ,\n" + "                data.\"user\" VARCHAR ,\n" + "                data.inrow VARCHAR ,\n" + "                data.jobid VARCHAR ,\n" + "                data.jobtype VARCHAR ,\n" + "                data.level VARCHAR ,\n" + "                data.msg VARCHAR ,\n" + "                data.outrow VARCHAR ,\n" + "                data.pass_time VARCHAR ,\n" + "                data.type VARCHAR ,\n" + "                id INTEGER not null primary key desc\n" + "                ) ";
    try (Connection conn = DriverManager.getConnection(getUrl(), props)) {
        conn.createStatement().execute(ddl);
    }
    HBaseAdmin admin = driver.getConnectionQueryServices(getUrl(), props).getAdmin();
    assertNotNull(admin.getTableDescriptor(Bytes.toBytes(tableName)));
    HColumnDescriptor[] columnFamilies = admin.getTableDescriptor(Bytes.toBytes(tableName)).getColumnFamilies();
    assertEquals(BloomType.NONE, columnFamilies[0].getBloomFilterType());
    props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 10));
    try (Connection conn = DriverManager.getConnection(getUrl(), props)) {
        conn.createStatement().execute(ddl);
        fail();
    } catch (TableAlreadyExistsException e) {
    // expected
    }
    props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 20));
    try (Connection conn = DriverManager.getConnection(getUrl(), props)) {
        conn.createStatement().execute("DROP TABLE " + tableName);
    }
    props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 30));
    props.setProperty(QueryServices.IS_NAMESPACE_MAPPING_ENABLED, Boolean.TRUE.toString());
    try (Connection conn = DriverManager.getConnection(getUrl(), props)) {
        conn.createStatement().execute("CREATE SCHEMA " + schemaName);
    }
    props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 40));
    try (Connection conn = DriverManager.getConnection(getUrl(), props)) {
        conn.createStatement().execute(ddl);
        assertNotEquals(null, admin.getTableDescriptor(SchemaUtil.getPhysicalTableName(tableName.getBytes(), true).getName()));
    } finally {
        admin.close();
    }
    props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 50));
    props.setProperty(QueryServices.DROP_METADATA_ATTRIB, Boolean.TRUE.toString());
    try (Connection conn = DriverManager.getConnection(getUrl(), props)) {
        conn.createStatement().execute("DROP TABLE " + tableName);
    }
}
Also used : HBaseAdmin(org.apache.hadoop.hbase.client.HBaseAdmin) TableAlreadyExistsException(org.apache.phoenix.schema.TableAlreadyExistsException) NewerTableAlreadyExistsException(org.apache.phoenix.schema.NewerTableAlreadyExistsException) HColumnDescriptor(org.apache.hadoop.hbase.HColumnDescriptor) Connection(java.sql.Connection) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) Properties(java.util.Properties) Test(org.junit.Test)

Aggregations

TableAlreadyExistsException (org.apache.phoenix.schema.TableAlreadyExistsException)11 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)9 Properties (java.util.Properties)8 Connection (java.sql.Connection)7 NewerTableAlreadyExistsException (org.apache.phoenix.schema.NewerTableAlreadyExistsException)7 Test (org.junit.Test)5 SQLException (java.sql.SQLException)4 ResultSet (java.sql.ResultSet)3 HBaseAdmin (org.apache.hadoop.hbase.client.HBaseAdmin)3 ColumnAlreadyExistsException (org.apache.phoenix.schema.ColumnAlreadyExistsException)3 IOException (java.io.IOException)2 ExecutionException (java.util.concurrent.ExecutionException)2 TimeoutException (java.util.concurrent.TimeoutException)2 HTableDescriptor (org.apache.hadoop.hbase.HTableDescriptor)2 TableExistsException (org.apache.hadoop.hbase.TableExistsException)2 AccessDeniedException (org.apache.hadoop.hbase.security.AccessDeniedException)2 PhoenixIOException (org.apache.phoenix.exception.PhoenixIOException)2 RetriableUpgradeException (org.apache.phoenix.exception.RetriableUpgradeException)2 UpgradeInProgressException (org.apache.phoenix.exception.UpgradeInProgressException)2 UpgradeNotRequiredException (org.apache.phoenix.exception.UpgradeNotRequiredException)2