Java Code Examples for org.apache.phoenix.jdbc.PhoenixConnection#getTable()

The following examples show how to use org.apache.phoenix.jdbc.PhoenixConnection#getTable() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ImmutableTablePropertiesIT.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Test
public void testImmutableProperty() throws Exception {
    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
    String immutableDataTableFullName = SchemaUtil.getTableName("", generateUniqueName());
    String mutableDataTableFullName = SchemaUtil.getTableName("", generateUniqueName());
    try (Connection conn = DriverManager.getConnection(getUrl(), props);) {
        Statement stmt = conn.createStatement();
        // create table with immutable table property set to true
        String ddl = "CREATE TABLE  " + immutableDataTableFullName +
                "  (a_string varchar not null, col1 integer" +
                "  CONSTRAINT pk PRIMARY KEY (a_string)) IMMUTABLE_ROWS=true";
        stmt.execute(ddl);
        
        // create table with immutable table property set to false
        ddl = "CREATE TABLE  " + mutableDataTableFullName +
                "  (a_string varchar not null, col1 integer" +
                "  CONSTRAINT pk PRIMARY KEY (a_string))  IMMUTABLE_ROWS=false";
        stmt.execute(ddl);
        
        PhoenixConnection phxConn = conn.unwrap(PhoenixConnection.class);
        PTable immutableTable = phxConn.getTable(new PTableKey(null, immutableDataTableFullName));
        assertTrue("IMMUTABLE_ROWS should be set to true", immutableTable.isImmutableRows());
        PTable mutableTable = phxConn.getTable(new PTableKey(null, mutableDataTableFullName));
        assertFalse("IMMUTABLE_ROWS should be set to false", mutableTable.isImmutableRows());
    } 
}
 
Example 2
Source File: ParallelIteratorsSplitTest.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetSplitsWithSkipScanFilter() throws Exception {
    byte[][] splits = new byte[][] {Ka1A, Ka1B, Ka1E, Ka1G, Ka1I, Ka2A};
    createTestTable(getUrl(),DDL,splits, null);
    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
    Connection conn = DriverManager.getConnection(getUrl(), props);
    PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class);
    
    PTable table = pconn.getTable(new PTableKey(pconn.getTenantId(), TABLE_NAME));
    TableRef tableRef = new TableRef(table);
    List<HRegionLocation> regions = pconn.getQueryServices().getAllTableRegions(tableRef.getTable().getPhysicalName().getBytes());
    List<KeyRange> ranges = getSplits(tableRef, scan, regions, scanRanges);
    assertEquals("Unexpected number of splits: " + ranges.size(), expectedSplits.size(), ranges.size());
    for (int i=0; i<expectedSplits.size(); i++) {
        assertEquals(expectedSplits.get(i), ranges.get(i));
    }
}
 
Example 3
Source File: BaseIndexIT.java    From phoenix with Apache License 2.0 6 votes vote down vote up
private void assertNoIndexDeletes(Connection conn, long minTimestamp, String fullIndexName) throws IOException, SQLException {
    if (!this.mutable) {
        PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class);
        PTable index = pconn.getTable(new PTableKey(null, fullIndexName));
        byte[] physicalIndexTable = index.getPhysicalName().getBytes();
        try (Table hIndex = pconn.getQueryServices().getTable(physicalIndexTable)) {
            Scan scan = new Scan();
            scan.setRaw(true);
            if (this.transactional) {
                minTimestamp = TransactionUtil.convertToNanoseconds(minTimestamp);
            }
            scan.setTimeRange(minTimestamp, HConstants.LATEST_TIMESTAMP);
            ResultScanner scanner = hIndex.getScanner(scan);
            Result result;
            while ((result = scanner.next()) != null) {
                CellScanner cellScanner = result.cellScanner();
                while (cellScanner.advance()) {
                    Cell current = cellScanner.current();
                    assertTrue(CellUtil.isPut(current));
                }
            }
        };
    }
}
 
Example 4
Source File: ViewCompilerTest.java    From phoenix with Apache License 2.0 6 votes vote down vote up
public void assertViewType(String[] viewNames, String[] viewDDLs, ViewType viewType) throws Exception {
    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
    PhoenixConnection conn = DriverManager.getConnection(getUrl(), props).unwrap(PhoenixConnection.class);
    String ct = "CREATE TABLE t (k1 INTEGER NOT NULL, k2 VARCHAR, v VARCHAR, CONSTRAINT pk PRIMARY KEY (k1,k2))";
    conn.createStatement().execute(ct);
    
    for (String viewDDL : viewDDLs) {
        conn.createStatement().execute(viewDDL);
    }
    
    StringBuilder buf = new StringBuilder();
    int count = 0;
    for (String view : viewNames) {
    	PTable table = conn.getTable(new PTableKey(null, view));
        assertEquals(viewType, table.getViewType());
        conn.createStatement().execute("DROP VIEW " + table.getName().getString());
        buf.append(' ');
        buf.append(table.getName().getString());
        count++;
    }
    assertEquals("Expected " + viewDDLs.length + ", but got " + count + ":"+ buf.toString(), viewDDLs.length, count);
}
 
Example 5
Source File: NonTxIndexBuilderTest.java    From phoenix with Apache License 2.0 6 votes vote down vote up
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();
    }
}
 
Example 6
Source File: ColumnEncodedBytesPropIT.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@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());
       } 
}
 
Example 7
Source File: PostIndexDDLCompilerTest.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Test
public void testCompile() throws Exception {
    try (Connection conn = DriverManager.getConnection(getUrl())) {
        setupTables(conn);
        PhoenixConnection pConn = conn.unwrap(PhoenixConnection.class);
        PTable pDataTable = pConn.getTable(new PTableKey(null, "T"));
        PostIndexDDLCompiler compiler = new PostIndexDDLCompiler(pConn, new TableRef(pDataTable));
        MutationPlan plan = compiler.compile(pConn.getTable(new PTableKey(null, "IDX")));
        assertEquals("T", plan.getQueryPlan().getTableRef().getTable().getTableName().getString());
    }
}
 
Example 8
Source File: AlterMultiTenantTableWithViewsIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private boolean checkColumnPartOfPk(PhoenixConnection conn, String columnName, String tableName) throws SQLException {
    String normalizedTableName = SchemaUtil.normalizeIdentifier(tableName);
    PTable table = conn.getTable(new PTableKey(conn.getTenantId(), normalizedTableName));
    List<PColumn> pkCols = table.getPKColumns();
    String normalizedColumnName = SchemaUtil.normalizeIdentifier(columnName);
    for (PColumn pkCol : pkCols) {
        if (pkCol.getName().getString().equals(normalizedColumnName)) {
            return true;
        }
    }
    return false;
}
 
Example 9
Source File: AlterMultiTenantTableWithViewsIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private int getIndexOfPkColumn(PhoenixConnection conn, String columnName, String tableName) throws SQLException {
    String normalizedTableName = SchemaUtil.normalizeIdentifier(tableName);
    PTable table = conn.getTable(new PTableKey(conn.getTenantId(), normalizedTableName));
    List<PColumn> pkCols = table.getPKColumns();
    String normalizedColumnName = SchemaUtil.normalizeIdentifier(columnName);
    int i = 0;
    for (PColumn pkCol : pkCols) {
        if (pkCol.getName().getString().equals(normalizedColumnName)) {
            return i;
        }
        i++;
    }
    return -1;
}
 
Example 10
Source File: RowKeySchemaTest.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Test
public void testClipLeft() throws Exception {
    ImmutableBytesWritable ptr = new ImmutableBytesWritable();
    Connection conn = DriverManager.getConnection(getUrl());
    conn.createStatement().execute("CREATE TABLE T1(K1 CHAR(1) NOT NULL, K2 VARCHAR, K3 VARCHAR, CONSTRAINT pk PRIMARY KEY (K1,K2,K3))  ");
    PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class);
    PTable table;
    RowKeySchema schema;
    table = pconn.getTable(new PTableKey(pconn.getTenantId(), "T1"));
    schema = table.getRowKeySchema();
    KeyRange r, rLeft, expectedResult;
    r = KeyRange.getKeyRange(getKeyPart(table, "A", "B", "C"), true, getKeyPart(table, "B", "C"), true);
    rLeft = schema.clipLeft(0, r, 1, ptr);
    expectedResult = KeyRange.getKeyRange(getKeyPart(table, "A"), true, getKeyPart(table, "B"), true);
    r = KeyRange.getKeyRange(getKeyPart(table, "A", "B", "C"), true, getKeyPart(table, "B"), true);
    rLeft = schema.clipLeft(0, r, 1, ptr);
    expectedResult = KeyRange.getKeyRange(getKeyPart(table, "A"), true, getKeyPart(table, "B"), true);
    assertEquals(expectedResult, rLeft);
    rLeft = schema.clipLeft(0, r, 2, ptr);
    expectedResult = KeyRange.getKeyRange(getKeyPart(table, "A", "B"), true, getKeyPart(table, "B"), true);
    assertEquals(expectedResult, rLeft);
    
    r = KeyRange.getKeyRange(getKeyPart(table, "A", "B", "C"), true, KeyRange.UNBOUND, true);
    rLeft = schema.clipLeft(0, r, 2, ptr);
    expectedResult = KeyRange.getKeyRange(getKeyPart(table, "A", "B"), true, KeyRange.UNBOUND, false);
    assertEquals(expectedResult, rLeft);
    
    r = KeyRange.getKeyRange(KeyRange.UNBOUND, false, getKeyPart(table, "A", "B", "C"), true);
    rLeft = schema.clipLeft(0, r, 2, ptr);
    expectedResult = KeyRange.getKeyRange(KeyRange.UNBOUND, false, getKeyPart(table, "A", "B"), true);
    assertEquals(expectedResult, rLeft);
}
 
Example 11
Source File: IndexColumnNamesTest.java    From phoenix with Apache License 2.0 5 votes vote down vote up
/**
 * 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());
}
 
Example 12
Source File: AsyncIndexDisabledIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Test
public void testAsyncIndexRegularBuild() throws Exception {
    try (Connection conn = DriverManager.getConnection(getUrl())) {
        conn.setAutoCommit(true);
        Statement stmt = conn.createStatement();
        String tableName = "TBL_" + generateUniqueName();
        String indexName = "IND_" + generateUniqueName();
        
        String ddl = "CREATE TABLE " + tableName + " (pk INTEGER NOT NULL PRIMARY KEY, val VARCHAR)";
        stmt.execute(ddl);
        stmt.execute("UPSERT INTO " + tableName + " values(1, 'y')");
        // create the async index
        stmt.execute("CREATE INDEX " + indexName + " ON " + tableName + "(val) ASYNC");

        // it should be built as a regular index
        PhoenixConnection phxConn = conn.unwrap(PhoenixConnection.class);
        PTable table = phxConn.getTable(new PTableKey(null, tableName));
        assertEquals("Index not built", 1, table.getIndexes().size());
        assertEquals("Wrong index created", indexName, table.getIndexes().get(0).getName().getString());
        
        ResultSet rs = stmt.executeQuery("select /*+ INDEX(" + indexName + ")*/ pk, val from " + tableName);
        assertTrue(rs.next());
        assertEquals(1, rs.getInt(1));
        assertEquals("y", rs.getString(2));
        assertFalse(rs.next());
    }
}
 
Example 13
Source File: BaseConnectionlessQueryTest.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static synchronized void doSetup() throws Exception {
    startServer(getUrl());
    ensureTableCreated(getUrl(), ATABLE_NAME);
    ensureTableCreated(getUrl(), ENTITY_HISTORY_TABLE_NAME);
    ensureTableCreated(getUrl(), FUNKY_NAME);
    ensureTableCreated(getUrl(), PTSDB_NAME);
    ensureTableCreated(getUrl(), PTSDB2_NAME);
    ensureTableCreated(getUrl(), PTSDB3_NAME);
    ensureTableCreated(getUrl(), MULTI_CF_NAME);
    ensureTableCreated(getUrl(), TABLE_WITH_ARRAY);
    
    
    Properties props = new Properties();
    props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(HConstants.LATEST_TIMESTAMP));
    PhoenixConnection conn = DriverManager.getConnection(PHOENIX_CONNECTIONLESS_JDBC_URL, props).unwrap(PhoenixConnection.class);
    try {
        PTable table = conn.getTable(new PTableKey(null, ATABLE_NAME));
        ATABLE = table;
        ORGANIZATION_ID = new ColumnRef(new TableRef(table), table.getColumnForColumnName("ORGANIZATION_ID").getPosition()).newColumnExpression();
        ENTITY_ID = new ColumnRef(new TableRef(table), table.getColumnForColumnName("ENTITY_ID").getPosition()).newColumnExpression();
        A_INTEGER = new ColumnRef(new TableRef(table), table.getColumnForColumnName("A_INTEGER").getPosition()).newColumnExpression();
        A_STRING = new ColumnRef(new TableRef(table), table.getColumnForColumnName("A_STRING").getPosition()).newColumnExpression();
        B_STRING = new ColumnRef(new TableRef(table), table.getColumnForColumnName("B_STRING").getPosition()).newColumnExpression();
        A_DATE = new ColumnRef(new TableRef(table), table.getColumnForColumnName("A_DATE").getPosition()).newColumnExpression();
        A_TIME = new ColumnRef(new TableRef(table), table.getColumnForColumnName("A_TIME").getPosition()).newColumnExpression();
        A_TIMESTAMP = new ColumnRef(new TableRef(table), table.getColumnForColumnName("A_TIMESTAMP").getPosition()).newColumnExpression();
        X_DECIMAL = new ColumnRef(new TableRef(table), table.getColumnForColumnName("X_DECIMAL").getPosition()).newColumnExpression();
    } finally {
        conn.close();
    }
}
 
Example 14
Source File: AlterMultiTenantTableWithViewsIT.java    From phoenix with Apache License 2.0 4 votes vote down vote up
private static long getTableSequenceNumber(PhoenixConnection conn, String tableName) throws SQLException {
    PTable table = conn.getTable(new PTableKey(conn.getTenantId(), SchemaUtil.normalizeIdentifier(tableName)));
    return table.getSequenceNumber();
}
 
Example 15
Source File: AlterMultiTenantTableWithViewsIT.java    From phoenix with Apache License 2.0 4 votes vote down vote up
private static short getMaxKeySequenceNumber(PhoenixConnection conn, String tableName) throws SQLException {
    PTable table = conn.getTable(new PTableKey(conn.getTenantId(), SchemaUtil.normalizeIdentifier(tableName)));
    return SchemaUtil.getMaxKeySeq(table);
}
 
Example 16
Source File: AlterTableIT.java    From phoenix with Apache License 2.0 4 votes vote down vote up
@Test
public void testMetadataForImmutableTable() throws Exception {
    String schemaName = "XYZ";
    String baseTableName = generateUniqueName();
    String viewName = generateUniqueName();
    String fullTableName = schemaName + "." + baseTableName;
    String fullViewName = schemaName + "." + viewName;
    try (Connection conn = DriverManager.getConnection(getUrl())) {
        PhoenixConnection phxConn = conn.unwrap(PhoenixConnection.class);
        conn.createStatement().execute("CREATE TABLE IF NOT EXISTS " + fullTableName + " ("
                + " ID char(1) NOT NULL,"
                + " COL1 integer NOT NULL,"
                + " COL2 bigint NOT NULL,"
                + " KV1 VARCHAR"
                + " CONSTRAINT NAME_PK PRIMARY KEY (ID, COL1, COL2)"
                + " ) " + generateDDLOptions("IMMUTABLE_ROWS = true"
                + (!columnEncoded ? ",IMMUTABLE_STORAGE_SCHEME="+ PTable.ImmutableStorageScheme.ONE_CELL_PER_COLUMN : "")));
        PTable baseTable = phxConn.getTable(new PTableKey(phxConn.getTenantId(), fullTableName));
        long initBaseTableSeqNumber = baseTable.getSequenceNumber(); 

        // assert that the client side cache is updated.
        EncodedCQCounter cqCounter = baseTable.getEncodedCQCounter();
        assertEquals( columnEncoded ? (Integer)(ENCODED_CQ_COUNTER_INITIAL_VALUE + 1) : null, cqCounter.getNextQualifier(QueryConstants.DEFAULT_COLUMN_FAMILY));
        
        // assert that the server side metadata is updated correctly.
        assertEncodedCQCounter(DEFAULT_COLUMN_FAMILY, schemaName, baseTableName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 1);
        assertEncodedCQValue(DEFAULT_COLUMN_FAMILY, "KV1", schemaName, baseTableName, ENCODED_CQ_COUNTER_INITIAL_VALUE);
        assertSequenceNumber(schemaName, baseTableName, initBaseTableSeqNumber);

        // now create a view and validate client and server side metadata
        String viewDDL = "CREATE VIEW " + fullViewName + " ( VIEW_COL1 INTEGER, A.VIEW_COL2 VARCHAR ) AS SELECT * FROM " + fullTableName;
        conn.createStatement().execute(viewDDL);
        baseTable = phxConn.getTable(new PTableKey(phxConn.getTenantId(), fullTableName));
        PTable view = phxConn.getTable(new PTableKey(phxConn.getTenantId(), fullViewName));

        // verify that the client side cache is updated. Base table's cq counters should be updated.
        assertEquals( columnEncoded ? (Integer)(ENCODED_CQ_COUNTER_INITIAL_VALUE + 2) : null, baseTable.getEncodedCQCounter().getNextQualifier(DEFAULT_COLUMN_FAMILY));
        assertEquals( columnEncoded ? (Integer)(ENCODED_CQ_COUNTER_INITIAL_VALUE + 1) : null, baseTable.getEncodedCQCounter().getNextQualifier("A"));
        assertNull("A view should always have the null cq counter", view.getEncodedCQCounter().getNextQualifier(DEFAULT_COLUMN_FAMILY));
        
        // assert that the server side metadata for the base table and the view is also updated correctly.
        assertEncodedCQCounter(DEFAULT_COLUMN_FAMILY, schemaName, baseTableName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 2);
        assertEncodedCQCounter("A", schemaName, baseTableName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 1);
        assertEncodedCQValue(DEFAULT_COLUMN_FAMILY, "VIEW_COL1", schemaName, viewName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 1);
        assertEncodedCQValue("A", "VIEW_COL2", schemaName, viewName, ENCODED_CQ_COUNTER_INITIAL_VALUE);
        assertSequenceNumber(schemaName, baseTableName, initBaseTableSeqNumber + (columnEncoded ? 1 : 0));
        assertSequenceNumber(schemaName, viewName, PTable.INITIAL_SEQ_NUM);
    }
}
 
Example 17
Source File: AlterTableIT.java    From phoenix with Apache License 2.0 4 votes vote down vote up
@Test
public void testMetadataForMutableTable() throws Exception {
    String schemaName = "XYZ";
    String baseTableName = generateUniqueName();
    String viewName = generateUniqueName();
    String fullTableName = schemaName + "." + baseTableName;
    String fullViewName = schemaName + "." + viewName;
    try (Connection conn = DriverManager.getConnection(getUrl())) {
        PhoenixConnection phxConn = conn.unwrap(PhoenixConnection.class);
        conn.createStatement().execute("CREATE TABLE IF NOT EXISTS " + fullTableName + " ("
                + " ID char(1) NOT NULL,"
                + " COL1 integer NOT NULL,"
                + " COL2 bigint NOT NULL,"
                + " KV1 VARCHAR"
                + " CONSTRAINT NAME_PK PRIMARY KEY (ID, COL1, COL2)"
                + " ) " + tableDDLOptions);
        PTable baseTable = phxConn.getTable(new PTableKey(phxConn.getTenantId(), fullTableName));
        long initBaseTableSeqNumber = baseTable.getSequenceNumber(); 

        // assert that the client side cache is updated.
        EncodedCQCounter cqCounter = baseTable.getEncodedCQCounter();
        assertEquals( columnEncoded ? (Integer)(ENCODED_CQ_COUNTER_INITIAL_VALUE + 1) : null, cqCounter.getNextQualifier(QueryConstants.DEFAULT_COLUMN_FAMILY));


        // assert that the server side metadata is updated correctly.
        assertEncodedCQCounter(DEFAULT_COLUMN_FAMILY, schemaName, baseTableName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 1);
        assertEncodedCQValue(DEFAULT_COLUMN_FAMILY, "KV1", schemaName, baseTableName, ENCODED_CQ_COUNTER_INITIAL_VALUE);
        assertSequenceNumber(schemaName, baseTableName, initBaseTableSeqNumber);

        // now create a view and validate client and server side metadata
        String viewDDL = "CREATE VIEW " + fullViewName + " ( VIEW_COL1 INTEGER, A.VIEW_COL2 VARCHAR ) AS SELECT * FROM " + fullTableName;
        conn.createStatement().execute(viewDDL);
        baseTable = phxConn.getTable(new PTableKey(phxConn.getTenantId(), fullTableName));
        PTable view = phxConn.getTable(new PTableKey(phxConn.getTenantId(), fullViewName));

        // verify that the client side cache is updated. Base table's cq counters should be updated.
        assertEquals(columnEncoded ? (Integer)(ENCODED_CQ_COUNTER_INITIAL_VALUE + 3) : null, baseTable.getEncodedCQCounter().getNextQualifier(DEFAULT_COLUMN_FAMILY));
        assertNull("A view should always have the null cq counter", view.getEncodedCQCounter().getNextQualifier(DEFAULT_COLUMN_FAMILY));

        // assert that the server side metadata for the base table and the view is also updated correctly.
        assertEncodedCQCounter(DEFAULT_COLUMN_FAMILY, schemaName, baseTableName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 3);
        assertEncodedCQValue(DEFAULT_COLUMN_FAMILY, "VIEW_COL1", schemaName, viewName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 1);
        assertEncodedCQValue("A", "VIEW_COL2", schemaName, viewName, ENCODED_CQ_COUNTER_INITIAL_VALUE + 2);
        assertSequenceNumber(schemaName, baseTableName, initBaseTableSeqNumber + (columnEncoded ? 1 : 0));
        assertSequenceNumber(schemaName, viewName, PTable.INITIAL_SEQ_NUM);
    }
}
 
Example 18
Source File: IndexMaintainerTest.java    From phoenix with Apache License 2.0 4 votes vote down vote up
private void testIndexRowKeyBuilding(String schemaName, String tableName, String dataColumns,
         String pk, String indexColumns, Object[] values, String includeColumns,
         String dataProps, String indexProps, KeyValueBuilder builder) throws Exception {
     Connection conn = DriverManager.getConnection(getUrl());
     String fullTableName = SchemaUtil.getTableName(SchemaUtil.normalizeIdentifier(schemaName),SchemaUtil.normalizeIdentifier(tableName));
     String fullIndexName = SchemaUtil.getTableName(SchemaUtil.normalizeIdentifier(schemaName),SchemaUtil.normalizeIdentifier("idx"));
     conn.createStatement().execute("CREATE TABLE " + fullTableName + "(" + dataColumns + " CONSTRAINT pk PRIMARY KEY (" + pk + "))  " + (dataProps.isEmpty() ? "" : dataProps) );
     try {
         conn.createStatement().execute("CREATE INDEX idx ON " + fullTableName + "(" + indexColumns + ") " + (includeColumns.isEmpty() ? "" : "INCLUDE (" + includeColumns + ") ") + (indexProps.isEmpty() ? "" : indexProps));
         PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class);
         PTable table = pconn.getTable(new PTableKey(pconn.getTenantId(), fullTableName));
         PTable index = pconn.getTable(new PTableKey(pconn.getTenantId(),fullIndexName));
         ImmutableBytesWritable ptr = new ImmutableBytesWritable();
         table.getIndexMaintainers(ptr, pconn);
         List<IndexMaintainer> c1 = IndexMaintainer.deserialize(ptr, builder, true);
         assertEquals(1,c1.size());
         IndexMaintainer im1 = c1.get(0);
         
         StringBuilder buf = new StringBuilder("UPSERT INTO " + fullTableName  + " VALUES(");
         for (int i = 0; i < values.length; i++) {
             buf.append("?,");
         }
         buf.setCharAt(buf.length()-1, ')');
         PreparedStatement stmt = conn.prepareStatement(buf.toString());
         for (int i = 0; i < values.length; i++) {
             stmt.setObject(i+1, values[i]);
         }
         stmt.execute();
         	Iterator<Pair<byte[],List<Cell>>> iterator = PhoenixRuntime.getUncommittedDataIterator(conn);
         List<Cell> dataKeyValues = iterator.next().getSecond();
         Map<ColumnReference,byte[]> valueMap = Maps.newHashMapWithExpectedSize(dataKeyValues.size());
ImmutableBytesWritable rowKeyPtr = new ImmutableBytesWritable(dataKeyValues.get(0).getRowArray(), dataKeyValues.get(0).getRowOffset(), dataKeyValues.get(0).getRowLength());
         byte[] row = rowKeyPtr.copyBytes();
         Put dataMutation = new Put(row);
         for (Cell kv : dataKeyValues) {
             valueMap.put(new ColumnReference(kv.getFamilyArray(), kv.getFamilyOffset(), kv.getFamilyLength(), kv.getQualifierArray(), kv.getQualifierOffset(), kv.getQualifierLength()), CellUtil.cloneValue(kv));
             dataMutation.add(kv);
         }
         ValueGetter valueGetter = newValueGetter(row, valueMap);
         
         List<Mutation> indexMutations = IndexTestUtil.generateIndexData(index, table, dataMutation, ptr, builder);
         assertEquals(1,indexMutations.size());
         assertTrue(indexMutations.get(0) instanceof Put);
         Mutation indexMutation = indexMutations.get(0);
         ImmutableBytesWritable indexKeyPtr = new ImmutableBytesWritable(indexMutation.getRow());
         ptr.set(rowKeyPtr.get(), rowKeyPtr.getOffset(), rowKeyPtr.getLength());
         byte[] mutablelndexRowKey = im1.buildRowKey(valueGetter, ptr, null, null, HConstants.LATEST_TIMESTAMP);
         byte[] immutableIndexRowKey = indexKeyPtr.copyBytes();
         assertArrayEquals(immutableIndexRowKey, mutablelndexRowKey);
         for (ColumnReference ref : im1.getCoveredColumns()) {
             valueMap.get(ref);
         }
         byte[] dataRowKey = im1.buildDataRowKey(indexKeyPtr, null);
         assertArrayEquals(dataRowKey, CellUtil.cloneRow(dataKeyValues.get(0)));
     } finally {
         try {
             conn.rollback();
             conn.createStatement().execute("DROP TABLE " + fullTableName);
         } finally {
             conn.close();
         }
     }
 }
 
Example 19
Source File: CsvBulkLoadToolIT.java    From phoenix with Apache License 2.0 4 votes vote down vote up
/**
 * This test case validates the import using CsvBulkLoadTool in
 * SingleCellArrayWithOffsets table.
 * PHOENIX-4872
 */

@Test
public void testImportInSingleCellArrayWithOffsetsTable() throws Exception {
    Statement stmt = conn.createStatement();
    stmt.execute("CREATE IMMUTABLE TABLE S.TABLE12 (ID INTEGER NOT NULL PRIMARY KEY," +
            " CF0.NAME VARCHAR, CF0.T DATE, CF1.T2 DATE, CF2.T3 DATE) " +
            "IMMUTABLE_STORAGE_SCHEME=SINGLE_CELL_ARRAY_WITH_OFFSETS");
    PhoenixConnection phxConn = conn.unwrap(PhoenixConnection.class);
    PTable table = phxConn.getTable(new PTableKey(null, "S.TABLE12"));

    assertEquals(PTable.ImmutableStorageScheme.SINGLE_CELL_ARRAY_WITH_OFFSETS,
            table.getImmutableStorageScheme());

    FileSystem fs = FileSystem.get(getUtility().getConfiguration());
    FSDataOutputStream outputStream = fs.create(new Path("/tmp/inputSCAWO.csv"));
    PrintWriter printWriter = new PrintWriter(outputStream);
    printWriter.println("1,Name 1,1970/01/01,1970/02/01,1970/03/01");
    printWriter.println("2,Name 2,1970/01/02,1970/02/02,1970/03/02");
    printWriter.println("3,Name 1,1970/01/01,1970/02/03,1970/03/01");
    printWriter.println("4,Name 2,1970/01/02,1970/02/04,1970/03/02");
    printWriter.println("5,Name 1,1970/01/01,1970/02/05,1970/03/01");
    printWriter.println("6,Name 2,1970/01/02,1970/02/06,1970/03/02");
    printWriter.close();

    CsvBulkLoadTool csvBulkLoadTool = new CsvBulkLoadTool();
    csvBulkLoadTool.setConf(new Configuration(getUtility().getConfiguration()));
    csvBulkLoadTool.getConf().set(DATE_FORMAT_ATTRIB,"yyyy/MM/dd");
    int exitCode = csvBulkLoadTool.run(new String[] {
            "--input", "/tmp/inputSCAWO.csv",
            "--table", "table12",
            "--schema", "s",
            "--zookeeper", zkQuorum});
    assertEquals(0, exitCode);

    ResultSet rs = stmt.executeQuery("SELECT COUNT(1) FROM S.TABLE12");
    assertTrue(rs.next());
    assertEquals(6, rs.getInt(1));

    rs.close();
    stmt.close();

}
 
Example 20
Source File: TestUtil.java    From phoenix with Apache License 2.0 4 votes vote down vote up
/**
 * Runs a major compaction, and then waits until the compaction is complete before returning.
 *
 * @param tableName name of the table to be compacted
 */
public static void doMajorCompaction(Connection conn, String tableName) throws Exception {

    tableName = SchemaUtil.normalizeIdentifier(tableName);

    // We simply write a marker row, request a major compaction, and then wait until the marker
    // row is gone
    PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class);
    PTable table = pconn.getTable(new PTableKey(pconn.getTenantId(), tableName));
    ConnectionQueryServices services = conn.unwrap(PhoenixConnection.class).getQueryServices();
    MutationState mutationState = pconn.getMutationState();
    if (table.isTransactional()) {
        mutationState.startTransaction(table.getTransactionProvider());
    }
    try (Table htable = mutationState.getHTable(table)) {
        byte[] markerRowKey = Bytes.toBytes("TO_DELETE");
       
        Put put = new Put(markerRowKey);
        put.addColumn(QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES, QueryConstants.EMPTY_COLUMN_VALUE_BYTES, QueryConstants.EMPTY_COLUMN_VALUE_BYTES);
        htable.put(put);
        Delete delete = new Delete(markerRowKey);
        delete.addColumn(QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES, QueryConstants.EMPTY_COLUMN_VALUE_BYTES);
        htable.delete(delete);
        htable.close();
        if (table.isTransactional()) {
            mutationState.commit();
        }
    
        Admin hbaseAdmin = services.getAdmin();
        hbaseAdmin.flush(TableName.valueOf(tableName));
        hbaseAdmin.majorCompact(TableName.valueOf(tableName));
        hbaseAdmin.close();
    
        boolean compactionDone = false;
        while (!compactionDone) {
            Thread.sleep(6000L);
            Scan scan = new Scan();
            scan.setStartRow(markerRowKey);
            scan.setStopRow(Bytes.add(markerRowKey, new byte[] { 0 }));
            scan.setRaw(true);
    
            try (Table htableForRawScan = services.getTable(Bytes.toBytes(tableName))) {
                ResultScanner scanner = htableForRawScan.getScanner(scan);
                List<Result> results = Lists.newArrayList(scanner);
                LOGGER.info("Results: " + results);
                compactionDone = results.isEmpty();
                scanner.close();
            }
            LOGGER.info("Compaction done: " + compactionDone);
            
            // need to run compaction after the next txn snapshot has been written so that compaction can remove deleted rows
            if (!compactionDone && table.isTransactional()) {
                hbaseAdmin = services.getAdmin();
                hbaseAdmin.flush(TableName.valueOf(tableName));
                hbaseAdmin.majorCompact(TableName.valueOf(tableName));
                hbaseAdmin.close();
            }
        }
    }
}