Search in sources :

Example 86 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 87 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);
    HTableDescriptor htableDescriptor = admin.getTableDescriptor(TableName.valueOf(t2));
    String str = htableDescriptor.getValue(PhoenixTransactionContext.READ_NON_TX_DATA);
    assertEquals(Boolean.TRUE.toString(), str);
    // 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)

Example 88 with PTableKey

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

the class NonTxIndexBuilderTest method getTestIndexMaintainer.

private IndexMaintainer getTestIndexMaintainer() throws Exception {
    Properties props = PropertiesUtil.deepCopy(TestUtil.TEST_PROPERTIES);
    // disable column encoding, makes debugging easier
    props.put(QueryServices.DEFAULT_COLUMN_ENCODED_BYTES_ATRRIB, "0");
    Connection conn = DriverManager.getConnection(getUrl(), props);
    try {
        conn.setAutoCommit(true);
        conn.createStatement().execute(TEST_TABLE_DDL);
        conn.createStatement().execute(TEST_TABLE_INDEX_DDL);
        PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class);
        PTable table = pconn.getTable(new PTableKey(pconn.getTenantId(), TEST_TABLE_STRING));
        ImmutableBytesWritable ptr = new ImmutableBytesWritable();
        table.getIndexMaintainers(ptr, pconn);
        List<IndexMaintainer> indexMaintainerList = IndexMaintainer.deserialize(ptr, GenericKeyValueBuilder.INSTANCE, true);
        assertEquals(1, indexMaintainerList.size());
        IndexMaintainer indexMaintainer = indexMaintainerList.get(0);
        return indexMaintainer;
    } finally {
        conn.close();
    }
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) ImmutableBytesWritable(org.apache.hadoop.hbase.io.ImmutableBytesWritable) IndexMaintainer(org.apache.phoenix.index.IndexMaintainer) 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)

Example 89 with PTableKey

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

the class IndexColumnNamesTest method testGetDynamicColPrecision.

/**
 * Tests that col types with a precision are outputted correctly in the dynamic columns
 * @throws SQLException
 */
@Test
public void testGetDynamicColPrecision() throws SQLException {
    conn.createStatement().execute(DYNAMIC_COL_DDL);
    conn.createStatement().execute(DYNAMIC_COL_IDX_DDL);
    PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class);
    pDataTable = pconn.getTable(new PTableKey(pconn.getTenantId(), "PRECISION_NAME_TEST"));
    pIndexTable = pconn.getTable(new PTableKey(pconn.getTenantId(), "PRECISION_NAME_IDX_TEST"));
    IndexColumnNames indexColumnNames = new IndexColumnNames(pDataTable, pIndexTable);
    assertEquals("[\"CHAR_TEST\" CHAR(15), \"VARCHAR_TEST\" VARCHAR(1), \"DECIMAL_TEST\" DECIMAL(10,2), \"BINARY_TEST\" BINARY(11), \"VARCHAR_UNSPEC\" VARCHAR, \"DEC_UNSPEC\" DECIMAL]", indexColumnNames.getDynamicDataCols().toString());
    assertEquals("[\":CHAR_TEST\" CHAR(15), \"0:VARCHAR_TEST\" VARCHAR(1), \"0:DECIMAL_TEST\" DECIMAL(10,2), \"0:BINARY_TEST\" BINARY(11), \"0:VARCHAR_UNSPEC\" VARCHAR, \"0:DEC_UNSPEC\" DECIMAL]", indexColumnNames.getDynamicIndexCols().toString());
}
Also used : PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection) PTableKey(org.apache.phoenix.schema.PTableKey) Test(org.junit.Test) BaseIndexTest(org.apache.phoenix.mapreduce.index.BaseIndexTest)

Aggregations

PTableKey (org.apache.phoenix.schema.PTableKey)89 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)77 PTable (org.apache.phoenix.schema.PTable)55 Connection (java.sql.Connection)48 Test (org.junit.Test)40 Properties (java.util.Properties)23 ResultSet (java.sql.ResultSet)14 PColumn (org.apache.phoenix.schema.PColumn)14 PreparedStatement (java.sql.PreparedStatement)13 HTableInterface (org.apache.hadoop.hbase.client.HTableInterface)12 SQLException (java.sql.SQLException)11 TableNotFoundException (org.apache.phoenix.schema.TableNotFoundException)11 PMetaData (org.apache.phoenix.schema.PMetaData)10 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)9 PName (org.apache.phoenix.schema.PName)9 BaseTest (org.apache.phoenix.query.BaseTest)8 Result (org.apache.hadoop.hbase.client.Result)7 ResultScanner (org.apache.hadoop.hbase.client.ResultScanner)7 Scan (org.apache.hadoop.hbase.client.Scan)7 MetaDataMutationResult (org.apache.phoenix.coprocessor.MetaDataProtocol.MetaDataMutationResult)6