Java Code Examples for org.apache.phoenix.jdbc.PhoenixConnection

The following examples show how to use org.apache.phoenix.jdbc.PhoenixConnection. These examples are extracted from open source projects. 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 Project: phoenix   Source File: SystemCatalogUpgradeIT.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testUpgradeOnlyHappensOnce() throws Exception {
    ConnectionQueryServices services = DriverManager.getConnection(getUrl()).unwrap(PhoenixConnection.class).getQueryServices();
    assertTrue(services instanceof PhoenixUpgradeCountingServices);
    // Check if the timestamp version is changing between the current version and prior version
    boolean wasTimestampChanged = systemTableVersion != MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP;
    reinitialize = true;
    systemTableVersion = MetaDataProtocol.MIN_SYSTEM_TABLE_TIMESTAMP;
    DriverManager.getConnection(getUrl());
    // Confirm that if the timestamp changed, that an upgrade was performed (and that if it
    // didn't, that an upgrade wasn't attempted).
    assertEquals(wasTimestampChanged ? 1 : 0, countUpgradeAttempts);
    // Confirm that another connection does not increase the number of times upgrade was attempted
    DriverManager.getConnection(getUrl());
    assertEquals(wasTimestampChanged ? 1 : 0, countUpgradeAttempts);
}
 
Example 2
Source Project: phoenix   Source File: PhoenixRuntime.java    License: Apache License 2.0 6 votes vote down vote up
@Deprecated
private static List<PColumn> getPkColumns(PTable ptable, Connection conn, boolean forDataTable) throws SQLException {
    PhoenixConnection pConn = conn.unwrap(PhoenixConnection.class);
    List<PColumn> pkColumns = ptable.getPKColumns();
    
    // Skip the salting column and the view index id column if present.
    // Skip the tenant id column too if the connection is tenant specific and the table used by the query plan is multi-tenant
    int offset = (ptable.getBucketNum() == null ? 0 : 1) + (ptable.isMultiTenant() && pConn.getTenantId() != null ? 1 : 0) + (ptable.getViewIndexId() == null ? 0 : 1);
    
    // get a sublist of pkColumns by skipping the offset columns.
    pkColumns = pkColumns.subList(offset, pkColumns.size());
    
    if (ptable.getType() == PTableType.INDEX && forDataTable) {
        // index tables have the same schema name as their parent/data tables.
        String fullDataTableName = ptable.getParentName().getString();
        
        // Get the corresponding columns of the data table.
        List<PColumn> dataColumns = IndexUtil.getDataColumns(fullDataTableName, pkColumns, pConn);
        pkColumns = dataColumns;
    }
    return pkColumns;
}
 
Example 3
private static void checkIndexPendingDisableCount(final PhoenixConnection conn,
        final String indexTableName) throws Exception {
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            try {
                while (!TestUtil.checkIndexState(conn, indexTableName, PIndexState.ACTIVE,
                    0L)) {
                    long count = getPendingDisableCount(conn, indexTableName);
                    if (count > 0) {
                        indexState =
                                new String(
                                        pendingDisableCountResult.getValue(TABLE_FAMILY_BYTES,
                                            PhoenixDatabaseMetaData.INDEX_STATE_BYTES));
                        pendingDisableCount = count;
                    }
                    Thread.sleep(100);
                }
            } catch (Exception e) {
                LOGGER.error("Error in checkPendingDisableCount : " + e);
            }
        }
    };
    Thread t1 = new Thread(runnable);
    t1.start();
}
 
Example 4
Source Project: phoenix   Source File: FromCompiler.java    License: Apache License 2.0 6 votes vote down vote up
private ColumnResolverWithUDF(PhoenixConnection connection, int tsAddition,
                              boolean updateCacheImmediately, Map<String,
        UDFParseNode> udfParseNodes) throws SQLException {
    this.connection = connection;
    this.client = connection == null ? null : new MetaDataClient(connection);
    this.tsAddition = tsAddition;
    functionMap = new HashMap<String, PFunction>(1);
    if (udfParseNodes.isEmpty()) {
        functions = Collections.<PFunction> emptyList();
    } else {
        functions = createFunctionRef(new ArrayList<String>(udfParseNodes.keySet()),
                updateCacheImmediately);
        for (PFunction function : functions) {
            functionMap.put(function.getFunctionName(), function);
        }
    }
}
 
Example 5
Source Project: phoenix   Source File: QueryCompiler.java    License: Apache License 2.0 6 votes vote down vote up
protected QueryPlan compileSubquery(SelectStatement subquery, boolean pushDownMaxRows) throws SQLException {
    PhoenixConnection connection = this.statement.getConnection();
    subquery = SubselectRewriter.flatten(subquery, connection);
    ColumnResolver resolver = FromCompiler.getResolverForQuery(subquery, connection);
    subquery = StatementNormalizer.normalize(subquery, resolver);
    SelectStatement transformedSubquery = SubqueryRewriter.transform(subquery, resolver, connection);
    if (transformedSubquery != subquery) {
        resolver = FromCompiler.getResolverForQuery(transformedSubquery, connection);
        subquery = StatementNormalizer.normalize(transformedSubquery, resolver);
    }
    int maxRows = this.statement.getMaxRows();
    this.statement.setMaxRows(pushDownMaxRows ? maxRows : 0); // overwrite maxRows to avoid its impact on inner queries.
    QueryPlan plan = new QueryCompiler(this.statement, subquery, resolver, bindManager, false, optimizeSubquery, null).compile();
    if (optimizeSubquery) {
        plan = statement.getConnection().getQueryServices().getOptimizer().optimize(statement, plan);
    }
    this.statement.setMaxRows(maxRows); // restore maxRows.
    return plan;
}
 
Example 6
Source Project: phoenix   Source File: CSVCommonsLoaderIT.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testCSVCommonsUpsert_NonExistentTable() throws Exception {
    PhoenixConnection conn = null;
    try {
        conn = DriverManager.getConnection(getUrl()).unwrap(
                PhoenixConnection.class);
        CSVCommonsLoader csvUtil = new CSVCommonsLoader(conn, "NONEXISTENTTABLE",
                null, true, ',', '"', '\\', "!");
        csvUtil.upsert(
                new StringReader("ID,VALARRAY\n"
                        + "1,2!3!4\n"));
        fail("Trying to load a non-existent table should fail");
    } catch (IllegalArgumentException e) {
        assertEquals("Table NONEXISTENTTABLE not found", e.getMessage());
    } finally {
        if (conn != null) {
            conn.close();
        }
    }

}
 
Example 7
Source Project: phoenix   Source File: ColumnEncodedBytesPropIT.java    License: 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 8
Source Project: phoenix   Source File: WhereCompilerTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testInListWithAnd1FilterScankey() throws SQLException {
    String tenantId1 = "000000000000001";
    String tenantId2 = "000000000000002";
    String tenantId3 = "000000000000003";
    String entityId = "00000000000000X";
    String query = String.format("select * from %s where organization_id IN ('%s','%s','%s') AND entity_id='%s'",
            ATABLE_NAME, tenantId1, tenantId3, tenantId2, entityId);
    PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
    PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
    QueryPlan plan = pstmt.optimizeQuery();
    Scan scan = plan.getContext().getScan();
    byte[] startRow = ByteUtil.concat(PVarchar.INSTANCE.toBytes(tenantId1), PVarchar.INSTANCE.toBytes(entityId));
    assertArrayEquals(startRow, scan.getStartRow());
    byte[] stopRow = ByteUtil.concat(PVarchar.INSTANCE.toBytes(tenantId3), PVarchar.INSTANCE.toBytes(entityId));
    assertArrayEquals(ByteUtil.concat(stopRow, QueryConstants.SEPARATOR_BYTE_ARRAY), scan.getStopRow());
    // TODO: validate scan ranges
}
 
Example 9
Source Project: phoenix   Source File: WhereCompilerTest.java    License: Apache License 2.0 6 votes vote down vote up
private void helpTestToNumberFilter(String toNumberClause, BigDecimal expectedDecimal) throws Exception {
        String tenantId = "000000000000001";
        String query = "select * from atable where organization_id='" + tenantId + "' and x_decimal >= " + toNumberClause;
        PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
        PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
        QueryPlan plan = pstmt.optimizeQuery();
        Scan scan = plan.getContext().getScan();
        Filter filter = scan.getFilter();

        assertEquals(
            singleKVFilter(constantComparison(
                CompareOp.GREATER_OR_EQUAL,
                X_DECIMAL,
                expectedDecimal)),
            filter);
}
 
Example 10
Source Project: phoenix   Source File: WhereCompilerTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testToDateFilter() throws Exception {
    String tenantId = "000000000000001";
    String dateStr = "2012-01-01 12:00:00";
    String query = "select * from atable where organization_id='" + tenantId + "' and a_date >= to_date('" + dateStr + "')";
    PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
    PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
    QueryPlan plan = pstmt.optimizeQuery();
    Scan scan = plan.getContext().getScan();
    Filter filter = scan.getFilter();

    Date date = DateUtil.parseDate(dateStr);

    assertEquals(
        singleKVFilter(constantComparison(
            CompareOp.GREATER_OR_EQUAL,
            A_DATE,
            date)),
        filter);
}
 
Example 11
Source Project: phoenix   Source File: OrphanViewTool.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Go through all the physical links in the system catalog table and update the base table info of the
 * view objects in orphanViewSet. If the base or view object does not exist for a given link, then add the link
 * to orphanLinkSet
 * @param phoenixConnection
 * @throws Exception
 */
private void processPhysicalLinks(PhoenixConnection phoenixConnection)
        throws Exception {
    ResultSet physicalLinkRS = phoenixConnection.createStatement().executeQuery(physicalLinkQuery);
    while (physicalLinkRS.next()) {
        String tenantId = physicalLinkRS.getString(1);
        String schemaName = physicalLinkRS.getString(2);
        String tableName = physicalLinkRS.getString(3);
        Key viewKey = new Key(tenantId, schemaName, tableName);
        View view = orphanViewSet.get(viewKey);

        String baseTenantId = physicalLinkRS.getString(4);
        String baseFullTableName = physicalLinkRS.getString(5);
        Key baseKey = new Key(baseTenantId, baseFullTableName);
        Base base = baseSet.get(baseKey);

        if (view == null || base == null) {
            orphanLinkSet.add(new Link(viewKey, baseKey, PTable.LinkType.PHYSICAL_TABLE));
        }
        else {
            view.setBase(baseKey);
        }
    }
}
 
Example 12
Source Project: phoenix   Source File: SetPropertyIT.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testSetPropertyAndAddColumnForDefaultColumnFamily() throws Exception {
    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
    Connection conn = DriverManager.getConnection(getUrl(), props);
    conn.setAutoCommit(false);
    String ddl = "CREATE TABLE " + dataTableFullName +
            "  (a_string varchar not null, col1 integer" +
            "  CONSTRAINT pk PRIMARY KEY (a_string)) " + tableDDLOptions;
    try {
        conn.createStatement().execute(ddl);
        conn.createStatement().execute("ALTER TABLE " + dataTableFullName + " ADD col2 integer IN_MEMORY=true");
        try (Admin admin = conn.unwrap(PhoenixConnection.class).getQueryServices().getAdmin()) {
            ColumnFamilyDescriptor[] columnFamilies = admin.getDescriptor(TableName.valueOf(dataTableFullName))
                    .getColumnFamilies();
            assertEquals(1, columnFamilies.length);
            assertEquals("0", columnFamilies[0].getNameAsString());
            assertTrue(columnFamilies[0].isInMemory());
        }
    } finally {
        conn.close();
    }
}
 
Example 13
Source Project: phoenix   Source File: RebuildIndexConnectionPropsIT.java    License: Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static synchronized void doSetup() throws Exception {
    Configuration conf = HBaseConfiguration.create();
    hbaseTestUtil = new HBaseTestingUtility(conf);
    Map<String, String> serverProps = new HashMap<>();
    serverProps.put(QueryServices.EXTRA_JDBC_ARGUMENTS_ATTRIB, QueryServicesOptions.DEFAULT_EXTRA_JDBC_ARGUMENTS);
    // need at least one retry otherwise test fails
    serverProps.put(QueryServices.INDEX_REBUILD_RPC_RETRIES_COUNTER, Long.toString(NUM_RPC_RETRIES));
    setUpConfigForMiniCluster(conf, new ReadOnlyProps(serverProps.entrySet().iterator()));
    hbaseTestUtil.startMiniCluster();
    // establish url and quorum. Need to use PhoenixDriver and not PhoenixTestDriver
    zkQuorum = "localhost:" + hbaseTestUtil.getZkCluster().getClientPort();
    url = PhoenixRuntime.JDBC_PROTOCOL + PhoenixRuntime.JDBC_PROTOCOL_SEPARATOR + zkQuorum;
    Properties driverProps = PropertiesUtil.deepCopy(TEST_PROPERTIES);
    DriverManager.registerDriver(PhoenixDriver.INSTANCE);
    try (PhoenixConnection phxConn =
            DriverManager.getConnection(url, driverProps).unwrap(PhoenixConnection.class)) {
    }
}
 
Example 14
Source Project: phoenix   Source File: OrphanViewTool.java    License: Apache License 2.0 6 votes vote down vote up
private void removeOrLogOrphanLinks(PhoenixConnection phoenixConnection) {
    for (Link link : orphanLinkSet) {
        try {
            byte linkType = getLinkType(link.type);
            if (outputPath != null) {
                writer[linkType].write(link.src.getSerializedValue() + "-->" + link.dst.getSerializedValue());
                writer[linkType].newLine();
            }
            else if (!clean){
                System.out.println(link.src.getSerializedValue() + "-(" + link.type + ")->" + link.dst.getSerializedValue());
            }
            if (clean) {
                removeLink(phoenixConnection, link.src, link.dst, link.type);
            }
        } catch (Exception e) {
            // ignore
        }
    }
}
 
Example 15
Source Project: phoenix   Source File: Task.java    License: Apache License 2.0 6 votes vote down vote up
public static void deleteTask(PhoenixConnection conn, PTable.TaskType taskType, Timestamp ts, String tenantId,
        String schemaName, String tableName, boolean accessCheckEnabled) throws IOException {
    PreparedStatement stmt = null;
    try {
        stmt = conn.prepareStatement("DELETE FROM " +
                PhoenixDatabaseMetaData.SYSTEM_TASK_NAME +
                " WHERE " + PhoenixDatabaseMetaData.TASK_TYPE + " = ? AND " +
                PhoenixDatabaseMetaData.TASK_TS + " = ? AND " +
                PhoenixDatabaseMetaData.TENANT_ID + (tenantId == null ? " IS NULL " : " = '" + tenantId + "'") + " AND " +
                PhoenixDatabaseMetaData.TABLE_SCHEM + (schemaName == null ? " IS NULL " : " = '" + schemaName + "'") + " AND " +
                PhoenixDatabaseMetaData.TABLE_NAME + " = ?");
        stmt.setByte(1, taskType.getSerializedValue());
        stmt.setTimestamp(2, ts);
        stmt.setString(3, tableName);
    } catch (SQLException e) {
        throw new IOException(e);
    }
    mutateSystemTaskTable(conn, stmt, accessCheckEnabled);
}
 
Example 16
Source Project: phoenix   Source File: MutationState.java    License: Apache License 2.0 6 votes vote down vote up
MutationState(int maxSize, long maxSizeBytes, PhoenixConnection connection,
        Map<TableRef, MultiRowMutationState> mutations, boolean subTask, PhoenixTransactionContext txContext) {
    this.maxSize = maxSize;
    this.maxSizeBytes = maxSizeBytes;
    this.connection = connection;
    this.batchSize = connection.getMutateBatchSize();
    this.batchSizeBytes = connection.getMutateBatchSizeBytes();
    this.mutations = mutations;
    boolean isMetricsEnabled = connection.isRequestLevelMetricsEnabled();
    this.mutationMetricQueue = isMetricsEnabled ? new MutationMetricQueue()
            : NoOpMutationMetricsQueue.NO_OP_MUTATION_METRICS_QUEUE;
    if (subTask) {
        // this code path is only used while running child scans, we can't pass the txContext to child scans
        // as it is not thread safe, so we use the tx member variable
        phoenixTransactionContext = txContext.newTransactionContext(txContext, subTask);
    } else if (txContext != null) {
        isExternalTxContext = true;
        phoenixTransactionContext = txContext.newTransactionContext(txContext, subTask);
    }
}
 
Example 17
Source Project: phoenix   Source File: TaskRegionObserver.java    License: Apache License 2.0 6 votes vote down vote up
public static void setEndTaskStatus(PhoenixConnection connForTask, Task.TaskRecord taskRecord, String taskStatus)
        throws IOException, SQLException {
    // update data with details.
    String data = taskRecord.getData();
    if (Strings.isNullOrEmpty(data)) {
        data = "{}";
    }
    JsonNode jsonNode = JacksonUtil.getObjectReader().readTree(data);
    ((ObjectNode) jsonNode).put(TASK_DETAILS, taskStatus);
    data = jsonNode.toString();

    Timestamp endTs = new Timestamp(EnvironmentEdgeManager.currentTimeMillis());
    Task.addTask(connForTask, taskRecord.getTaskType(), taskRecord.getTenantId(), taskRecord.getSchemaName(),
            taskRecord.getTableName(), taskStatus, data, taskRecord.getPriority(),
            taskRecord.getTimeStamp(), endTs, true);
}
 
Example 18
Source Project: phoenix   Source File: ExpressionUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * For table with salted/multiTenant/viewIndexId,some leading rowkey columns should be skipped.
 * @param table
 * @param phoenixConnection
 * @return
 */
public static int getRowKeyColumnOffset(PTable table, PhoenixConnection phoenixConnection) {
    boolean isSalted = table.getBucketNum() != null;
    boolean isMultiTenant = phoenixConnection.getTenantId() != null && table.isMultiTenant();
    boolean isSharedViewIndex = table.getViewIndexId() != null;
    return (isSalted ? 1 : 0) + (isMultiTenant ? 1 : 0) + (isSharedViewIndex ? 1 : 0);
}
 
Example 19
Source Project: phoenix   Source File: PostIndexDDLCompilerTest.java    License: 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 20
Source Project: phoenix   Source File: TransactionUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static Long getTableTimestamp(PhoenixConnection connection, boolean transactional, TransactionFactory.Provider provider) throws SQLException {
	Long timestamp = null;
	if (!transactional) {
		return timestamp;
	}
	MutationState mutationState = connection.getMutationState();
	if (!mutationState.isTransactionStarted()) {
		mutationState.startTransaction(provider);
	}
	timestamp = convertToMilliseconds(mutationState.getInitialWritePointer());
	return timestamp;
}
 
Example 21
Source Project: phoenix   Source File: MetaDataUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static void deleteViewIndexSequences(PhoenixConnection connection, PName name, boolean isNamespaceMapped)
        throws SQLException {
    String schemaName = getViewIndexSequenceSchemaName(name, isNamespaceMapped);
    String sequenceName = getViewIndexSequenceName(name, null, isNamespaceMapped);
    connection.createStatement().executeUpdate("DELETE FROM " + PhoenixDatabaseMetaData.SYSTEM_SEQUENCE + " WHERE "
            + PhoenixDatabaseMetaData.SEQUENCE_SCHEMA
            + (schemaName.length() > 0 ? "='" + schemaName + "'" : " IS NULL") + (isNamespaceMapped
                    ? " AND " + PhoenixDatabaseMetaData.SEQUENCE_NAME + " = '" + sequenceName + "'" : ""));

}
 
Example 22
Source Project: phoenix   Source File: UpsertValuesIT.java    License: Apache License 2.0 5 votes vote down vote up
public void testColumnQualifierForUpsertedValues() throws Exception {
    String schemaName = "A";
    String tableName = "TEST";
    String fullTableName = SchemaUtil.getTableName(schemaName, tableName);
    String ddl = "create table " + fullTableName 
            + " (" 
            + " K varchar primary key,"
            + " CF1.V1 varchar, CF2.V2 VARCHAR, CF2.V3 VARCHAR)";
    try (Connection conn = DriverManager.getConnection(getUrl())) {
        conn.createStatement().execute(ddl);
    }
    String dml = "UPSERT INTO " + fullTableName + " VALUES (?, ?, ?, ?)";
    try (Connection conn = DriverManager.getConnection(getUrl())) {
        PreparedStatement stmt = conn.prepareStatement(dml);
        stmt.setString(1, "KEY1");
        stmt.setString(2, "VALUE1");
        stmt.setString(3, "VALUE2");
        stmt.setString(4, "VALUE3");
        stmt.executeUpdate();
        conn.commit();
    }
    // Issue a raw hbase scan and assert that key values have the expected column qualifiers.
    try (Connection conn = DriverManager.getConnection(getUrl())) {
        Table table = conn.unwrap(PhoenixConnection.class).getQueryServices().getTable(Bytes.toBytes(fullTableName));
        ResultScanner scanner = table.getScanner(new Scan());
        Result next = scanner.next();
        assertTrue(next.containsColumn(Bytes.toBytes("CF1"), PInteger.INSTANCE.toBytes(1)));
        assertTrue(next.containsColumn(Bytes.toBytes("CF2"), PInteger.INSTANCE.toBytes(2)));
        assertTrue(next.containsColumn(Bytes.toBytes("CF2"), PInteger.INSTANCE.toBytes(3)));
    }
}
 
Example 23
Source Project: phoenix   Source File: QueryUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Util that generates a PreparedStatement against syscat to fetch schema listings.
 */
public static PreparedStatement getSchemasStmt(
    PhoenixConnection connection, String catalog, String schemaPattern) throws SQLException {
    List<String> parameterValues = new ArrayList<String>(4);
    StringBuilder buf = new StringBuilder("select distinct \n" +
            TABLE_SCHEM + "," +
            TENANT_ID + " " + TABLE_CATALOG +
            " from " + SYSTEM_CATALOG + " " + SYSTEM_CATALOG_ALIAS +
            " where " + COLUMN_NAME + " is null");
    addTenantIdFilter(connection, buf, catalog, parameterValues);
    if (schemaPattern != null) {
        buf.append(" and " + TABLE_SCHEM + " like ?");
        parameterValues.add(schemaPattern);
    }
    if (SchemaUtil.isNamespaceMappingEnabled(null, connection.getQueryServices().getProps())) {
        buf.append(" and " + TABLE_NAME + " = '" + MetaDataClient.EMPTY_TABLE + "'");
    }

    // TODO: we should union this with SYSTEM.SEQUENCE too, but we only have support for
    // UNION ALL and we really need UNION so that it dedups.

    PreparedStatement stmt = connection.prepareStatement(buf.toString());
    for(int i = 0; i < parameterValues.size(); i++) {
        stmt.setString(i+1, parameterValues.get(i));
    }
    return stmt;
}
 
Example 24
Source Project: phoenix   Source File: ConnectionQueryServicesImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public PhoenixConnection connect(String url, Properties info) throws SQLException {
    checkClosed();
    PMetaData metadata = latestMetaData;
    if (metadata == null) {
        throwConnectionClosedException();
    }
    return new PhoenixConnection(this, url, info, metadata);
}
 
Example 25
Source Project: phoenix   Source File: UpgradeUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static SQLException closeConnection(PhoenixConnection conn, SQLException sqlEx) {
    SQLException toReturn = sqlEx;
    try {
        conn.close();
    } catch (SQLException e) {
        if (toReturn != null) {
            toReturn.setNextException(e);
        } else {
            toReturn = e;
        }
    }
    return toReturn;
}
 
Example 26
Source Project: phoenix   Source File: IndexCoprocIT.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateCoprocs() throws Exception {
    String schemaName = "S" + generateUniqueName();
    String tableName = "T_" + generateUniqueName();
    String indexName = "I_" + generateUniqueName();
    String physicalTableName = SchemaUtil.getPhysicalHBaseTableName(schemaName, tableName,
        isNamespaceMapped).getString();
    String physicalIndexName = SchemaUtil.getPhysicalHBaseTableName(schemaName,
        indexName, isNamespaceMapped).getString();
    Admin admin = ((PhoenixConnection) getConnection()).getQueryServices().getAdmin();

    createBaseTable(schemaName, tableName, isMultiTenant, 0, null);
    createIndexTable(schemaName, tableName, indexName);

    TableDescriptor baseDescriptor = admin.getDescriptor(TableName.valueOf(physicalTableName));
    TableDescriptorBuilder baseDescBuilder = TableDescriptorBuilder.newBuilder(baseDescriptor);
    TableDescriptor indexDescriptor = admin.getDescriptor(TableName.valueOf(physicalIndexName));
    TableDescriptorBuilder indexDescBuilder = TableDescriptorBuilder.newBuilder(indexDescriptor);

    assertCoprocsContains(IndexRegionObserver.class, baseDescriptor);
    assertCoprocsContains(GlobalIndexChecker.class, indexDescriptor);

    removeCoproc(IndexRegionObserver.class, baseDescBuilder, admin);
    removeCoproc(IndexRegionObserver.class, indexDescBuilder, admin);
    removeCoproc(GlobalIndexChecker.class, indexDescBuilder, admin);

    Map<String, String> props = new HashMap<String, String>();
    props.put(NonTxIndexBuilder.CODEC_CLASS_NAME_KEY, PhoenixIndexCodec.class.getName());
    Indexer.enableIndexing(baseDescBuilder, PhoenixIndexBuilder.class,
        props, QueryServicesOptions.DEFAULT_COPROCESSOR_PRIORITY);
    admin.modifyTable(baseDescBuilder.build());
    baseDescriptor = admin.getDescriptor(TableName.valueOf(physicalTableName));
    indexDescriptor = admin.getDescriptor(TableName.valueOf(physicalIndexName));
    assertUsingOldCoprocs(baseDescriptor, indexDescriptor);

    createBaseTable(schemaName, tableName, true, 0, null);
    baseDescriptor = admin.getDescriptor(TableName.valueOf(physicalTableName));
    indexDescriptor = admin.getDescriptor(TableName.valueOf(physicalIndexName));
    assertUsingOldCoprocs(baseDescriptor, indexDescriptor);
}
 
Example 27
Source Project: phoenix   Source File: CreateFunctionCompiler.java    License: Apache License 2.0 5 votes vote down vote up
public MutationPlan compile(final CreateFunctionStatement create) throws SQLException {
    final PhoenixConnection connection = statement.getConnection();
    PhoenixConnection connectionToBe = connection;
    final StatementContext context = new StatementContext(statement);
    final MetaDataClient client = new MetaDataClient(connectionToBe);
    
    return new CreateFunctionMutationPlan(context, create, client, connection);
}
 
Example 28
Source Project: phoenix   Source File: FormatToBytesWritableMapper.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void setup(Context context) throws IOException, InterruptedException {

    Configuration conf = context.getConfiguration();

    // pass client configuration into driver
    Properties clientInfos = new Properties();
    for (Map.Entry<String, String> entry : conf) {
        clientInfos.setProperty(entry.getKey(), entry.getValue());
    }

    try {
        conn = (PhoenixConnection) QueryUtil.getConnectionOnServer(clientInfos, conf);
        // We are dependent on rolling back before performing commits, so we need to be sure
        // that auto-commit is not turned on
        conn.setAutoCommit(false);

        final String tableNamesConf = conf.get(TABLE_NAMES_CONFKEY);
        final String logicalNamesConf = conf.get(LOGICAL_NAMES_CONFKEY);
        tableNames = TargetTableRefFunctions.NAMES_FROM_JSON.apply(tableNamesConf);
        logicalNames = TargetTableRefFunctions.NAMES_FROM_JSON.apply(logicalNamesConf);

        initColumnIndexes();
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }

    upsertListener = new MapperUpsertListener<RECORD>(
            context, conf.getBoolean(IGNORE_INVALID_ROW_CONFKEY, true));
    upsertExecutor = buildUpsertExecutor(conf);
    preUpdateProcessor = PhoenixConfigurationUtil.loadPreUpsertProcessor(conf);
}
 
Example 29
Source Project: phoenix   Source File: MutationTest.java    License: Apache License 2.0 5 votes vote down vote up
private void assertDurability(Connection conn, Durability durability) throws SQLException {
    PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class);
    Iterator<Pair<byte[], List<Mutation>>> it = pconn.getMutationState().toMutations();
    assertTrue(it.hasNext());
    while (it.hasNext()) {
        Pair<byte[], List<Mutation>> pair = it.next();
        assertFalse(pair.getSecond().isEmpty());
        for (Mutation m : pair.getSecond()) {
            assertEquals(durability, m.getDurability());
        }
    }
}
 
Example 30
Source Project: phoenix   Source File: BaseConnectionlessQueryTest.java    License: 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();
    }
}