Search in sources :

Example 61 with PTableKey

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

the class AlterTableIT method testSetForSomePhoenixTablePropertiesOnViewsAllowed.

@Test
public void testSetForSomePhoenixTablePropertiesOnViewsAllowed() throws Exception {
    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
    String ddl = "CREATE TABLE " + dataTableFullName + " (\n" + "ID1 VARCHAR(15) NOT NULL,\n" + "ID2 VARCHAR(15) NOT NULL,\n" + "CREATED_DATE DATE,\n" + "CREATION_TIME BIGINT,\n" + "LAST_USED DATE,\n" + "CONSTRAINT PK PRIMARY KEY (ID1, ID2)) " + generateDDLOptions("SALT_BUCKETS = 8");
    Connection conn1 = DriverManager.getConnection(getUrl(), props);
    conn1.createStatement().execute(ddl);
    String viewFullName = SchemaUtil.getTableName(schemaName, generateUniqueName());
    ddl = "CREATE VIEW " + viewFullName + " AS SELECT * FROM " + dataTableFullName + " WHERE CREATION_TIME = 1";
    conn1.createStatement().execute(ddl);
    ddl = "ALTER VIEW " + viewFullName + " SET UPDATE_CACHE_FREQUENCY = 10";
    conn1.createStatement().execute(ddl);
    conn1.createStatement().execute("SELECT * FROM " + viewFullName);
    PhoenixConnection pconn = conn1.unwrap(PhoenixConnection.class);
    assertEquals(10, pconn.getTable(new PTableKey(pconn.getTenantId(), viewFullName)).getUpdateCacheFrequency());
    ddl = "ALTER VIEW " + viewFullName + " SET UPDATE_CACHE_FREQUENCY = 20";
    conn1.createStatement().execute(ddl);
    conn1.createStatement().execute("SELECT * FROM " + viewFullName);
    pconn = conn1.unwrap(PhoenixConnection.class);
    assertEquals(20, pconn.getTable(new PTableKey(pconn.getTenantId(), viewFullName)).getUpdateCacheFrequency());
    assertImmutableRows(conn1, viewFullName, false);
    ddl = "ALTER VIEW " + viewFullName + " SET DISABLE_WAL = TRUE";
    try {
        conn1.createStatement().execute(ddl);
        fail();
    } catch (SQLException e) {
        assertEquals(SQLExceptionCode.VIEW_WITH_PROPERTIES.getErrorCode(), e.getErrorCode());
    }
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) TestUtil.closeConnection(org.apache.phoenix.util.TestUtil.closeConnection) Properties(java.util.Properties) PTableKey(org.apache.phoenix.schema.PTableKey) BaseTest(org.apache.phoenix.query.BaseTest) Test(org.junit.Test)

Example 62 with PTableKey

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

the class AlterTableIT method asssertIsWALDisabled.

private void asssertIsWALDisabled(Connection conn, String fullTableName, boolean expectedValue) throws SQLException {
    PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class);
    assertEquals(expectedValue, pconn.getTable(new PTableKey(pconn.getTenantId(), fullTableName)).isWALDisabled());
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) PTableKey(org.apache.phoenix.schema.PTableKey)

Example 63 with PTableKey

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

the class AppendOnlySchemaIT method testValidateAttributes.

@Test
public void testValidateAttributes() throws Exception {
    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
    try (Connection conn = DriverManager.getConnection(getUrl(), props)) {
        String tableName = generateUniqueName();
        String viewName = generateUniqueName();
        try {
            conn.createStatement().execute("create table IF NOT EXISTS " + tableName + " ( id char(1) NOT NULL," + " col1 integer NOT NULL," + " CONSTRAINT NAME_PK PRIMARY KEY (id, col1))" + " APPEND_ONLY_SCHEMA = true");
            fail("UPDATE_CACHE_FREQUENCY attribute must not be set to ALWAYS if APPEND_ONLY_SCHEMA is true");
        } catch (SQLException e) {
            assertEquals(SQLExceptionCode.UPDATE_CACHE_FREQUENCY_INVALID.getErrorCode(), e.getErrorCode());
        }
        conn.createStatement().execute("create table IF NOT EXISTS " + tableName + " ( id char(1) NOT NULL," + " col1 integer NOT NULL" + " CONSTRAINT NAME_PK PRIMARY KEY (id, col1))" + " APPEND_ONLY_SCHEMA = true, UPDATE_CACHE_FREQUENCY=1000");
        conn.createStatement().execute("create view IF NOT EXISTS " + viewName + " (val1 integer) AS SELECT * FROM " + tableName);
        PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class);
        PTable view = pconn.getTable(new PTableKey(pconn.getTenantId(), viewName));
        assertEquals(true, view.isAppendOnlySchema());
        assertEquals(1000, view.getUpdateCacheFrequency());
    }
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) Properties(java.util.Properties) PTableKey(org.apache.phoenix.schema.PTableKey) PTable(org.apache.phoenix.schema.PTable) Test(org.junit.Test)

Example 64 with PTableKey

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

the class ParameterizedTransactionIT method testRowConflicts.

private void testRowConflicts(String fullTableName) throws Exception {
    try (Connection conn1 = DriverManager.getConnection(getUrl());
        Connection conn2 = DriverManager.getConnection(getUrl())) {
        conn1.setAutoCommit(false);
        conn2.setAutoCommit(false);
        String selectSql = "SELECT * FROM " + fullTableName;
        conn1.setAutoCommit(false);
        ResultSet rs = conn1.createStatement().executeQuery(selectSql);
        boolean immutableRows = conn1.unwrap(PhoenixConnection.class).getTable(new PTableKey(null, fullTableName)).isImmutableRows();
        assertFalse(rs.next());
        // upsert row using conn1
        String upsertSql = "UPSERT INTO " + fullTableName + "(varchar_pk, char_pk, int_pk, long_pk, decimal_pk, date_pk, a.int_col1) VALUES(?, ?, ?, ?, ?, ?, ?)";
        PreparedStatement stmt = conn1.prepareStatement(upsertSql);
        TestUtil.setRowKeyColumns(stmt, 1);
        stmt.setInt(7, 10);
        stmt.execute();
        // upsert row using conn2
        upsertSql = "UPSERT INTO " + fullTableName + "(varchar_pk, char_pk, int_pk, long_pk, decimal_pk, date_pk, b.int_col2) VALUES(?, ?, ?, ?, ?, ?, ?)";
        stmt = conn2.prepareStatement(upsertSql);
        TestUtil.setRowKeyColumns(stmt, 1);
        stmt.setInt(7, 11);
        stmt.execute();
        conn1.commit();
        //second commit should fail
        try {
            conn2.commit();
            if (!immutableRows)
                fail();
        } catch (SQLException e) {
            if (immutableRows)
                fail();
            assertEquals(e.getErrorCode(), SQLExceptionCode.TRANSACTION_CONFLICT_EXCEPTION.getErrorCode());
        }
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) PTableKey(org.apache.phoenix.schema.PTableKey)

Example 65 with PTableKey

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

the class ParameterizedTransactionIT method testCreateTableToBeTransactional.

@Test
public void testCreateTableToBeTransactional() throws Exception {
    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
    Connection conn = DriverManager.getConnection(getUrl(), props);
    String t1 = generateUniqueName();
    String t2 = generateUniqueName();
    String ddl = "CREATE TABLE " + t1 + " (k varchar primary key) " + tableDDLOptions;
    conn.createStatement().execute(ddl);
    PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class);
    PTable table = pconn.getTable(new PTableKey(null, t1));
    HTableInterface htable = pconn.getQueryServices().getTable(Bytes.toBytes(t1));
    assertTrue(table.isTransactional());
    assertTrue(htable.getTableDescriptor().getCoprocessors().contains(PhoenixTransactionalProcessor.class.getName()));
    try {
        ddl = "ALTER TABLE " + t1 + " 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(t2));
    desc.addFamily(new HColumnDescriptor(QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES));
    admin.createTable(desc);
    ddl = "CREATE TABLE " + t2 + " (k varchar primary key) transactional=true";
    conn.createStatement().execute(ddl);
    assertEquals(Boolean.TRUE.toString(), admin.getTableDescriptor(TableName.valueOf(t2)).getValue(TxConstants.READ_NON_TX_DATA));
    // Should be ok, as HBase metadata should match existing metadata.
    ddl = "CREATE TABLE IF NOT EXISTS " + t1 + " (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, t1));
    htable = pconn.getQueryServices().getTable(Bytes.toBytes(t1));
    assertTrue(table.isTransactional());
    assertTrue(htable.getTableDescriptor().getCoprocessors().contains(PhoenixTransactionalProcessor.class.getName()));
}
Also used : HBaseAdmin(org.apache.hadoop.hbase.client.HBaseAdmin) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) SQLException(java.sql.SQLException) HColumnDescriptor(org.apache.hadoop.hbase.HColumnDescriptor) Connection(java.sql.Connection) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) Properties(java.util.Properties) PTableKey(org.apache.phoenix.schema.PTableKey) HTableInterface(org.apache.hadoop.hbase.client.HTableInterface) PTable(org.apache.phoenix.schema.PTable) HTableDescriptor(org.apache.hadoop.hbase.HTableDescriptor) Test(org.junit.Test)

Aggregations

PTableKey (org.apache.phoenix.schema.PTableKey)69 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)58 PTable (org.apache.phoenix.schema.PTable)48 Connection (java.sql.Connection)37 Test (org.junit.Test)29 Properties (java.util.Properties)18 PColumn (org.apache.phoenix.schema.PColumn)15 SQLException (java.sql.SQLException)11 PreparedStatement (java.sql.PreparedStatement)10 ResultSet (java.sql.ResultSet)10 TableNotFoundException (org.apache.phoenix.schema.TableNotFoundException)10 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)9 PName (org.apache.phoenix.schema.PName)9 Scan (org.apache.hadoop.hbase.client.Scan)8 MetaDataMutationResult (org.apache.phoenix.coprocessor.MetaDataProtocol.MetaDataMutationResult)7 BaseTest (org.apache.phoenix.query.BaseTest)7 Statement (java.sql.Statement)5 HTableInterface (org.apache.hadoop.hbase.client.HTableInterface)5 Result (org.apache.hadoop.hbase.client.Result)5 ResultScanner (org.apache.hadoop.hbase.client.ResultScanner)5