ORACLE CDC Incremental Synchronization Initialization

  // Step 1 Find the source tables for which the subscriber has access
        // privileges.


rs = stmt.executeQuery("SELECT * FROM ALL_SOURCE_TABLES");
            sources = new ArrayList<OracleCDCSource>();
            while (rs.next())
            {
                String srcSchema = rs.getString("SOURCE_SCHEMA_NAME");
                String srcTable = rs.getString("SOURCE_TABLE_NAME");
                if (logger.isDebugEnabled())
                    logger.debug("Subscribing to " + srcSchema + "." + srcTable);
                sources.add(new OracleCDCSource(srcSchema, srcTable));
            }



             // Step 2 Find the change set names and columns for which the subscriber
        // has access privileges.


for (Iterator<OracleCDCSource> iterator = sources.iterator(); iterator
                .hasNext();)
        {
            OracleCDCSource src = iterator.next();
            rs = stmt
                        .executeQuery("SELECT UNIQUE CHANGE_SET_NAME, PUB.COLUMN_NAME,"
                                + " PUB_ID, COL.COLUMN_ID "
                                + " FROM ALL_PUBLISHED_COLUMNS PUB, ALL_TAB_COLUMNS COL "
                                + " WHERE SOURCE_SCHEMA_NAME = '"
                                + src.getSchema()
                                + "'"
                                + " AND SOURCE_TABLE_NAME = '"
                                + src.getTable()
                                + "'"
                                + " AND SOURCE_SCHEMA_NAME = COL.OWNER "
                                + " AND SOURCE_TABLE_NAME = COL.TABLE_NAME"
                                + " AND PUB.COLUMN_NAME = COL.COLUMN_NAME"
                                + " ORDER BY COL.COLUMN_ID");

                while (rs.next())
                {
                    String changeSetName = rs.getString("CHANGE_SET_NAME");
                    String columnName = rs.getString("COLUMN_NAME");
                    long pubId = rs.getLong("PUB_ID");
                    src.addPublication(changeSetName, columnName, pubId);

                    changeSets.add(changeSetName);
                    if (logger.isDebugEnabled())
                        logger.debug("Found column " + changeSetName + "\t"
                                + columnName + "\t" + pubId);
                }
            }

           
// Step 3 Create subscriptions.

   subscriberViews = new HashMap<String, OracleCDCSource>();

        for (Iterator<OracleCDCSource> iterator = sources.iterator(); iterator
                .hasNext();)
        {
            OracleCDCSource src = iterator.next();
            Map<Long, OracleCDCPublication> publications = src
                    .getPublications();

            StringBuffer subscribeStmt = new StringBuffer();
            for (OracleCDCPublication pub : publications.values())
            {
                if (changeSets.remove(pub.getPublicationName()))
                {
                    if (logger.isDebugEnabled())
                        logger.debug("Creating subscription to "
                                + pub.getPublicationName());

                    /*
                     * Dropping subscription if it already exists : this can
                     * happen if release code was not called
                     */
                    executeQuery("BEGIN DBMS_CDC_SUBSCRIBE.DROP_SUBSCRIPTION("
                            + "subscription_name => 'TUNGSTEN_PUB');END;", true);

                    executeQuery(
                            "BEGIN DBMS_CDC_SUBSCRIBE.CREATE_SUBSCRIPTION("
                                    + "change_set_name => '"
                                    + pub.getPublicationName()
                                    + "', description => 'Change data used by Tungsten', "
                                    + "subscription_name => 'TUNGSTEN_PUB"
                                    + "');end;", false);
                }


// Step 4 Subscribe to a source table and the columns in the
                // source table.


String viewName = "VW_TUNGSTEN_CDC" + i;
                subscribeStmt
                        .append("DBMS_CDC_SUBSCRIBE.SUBSCRIBE(subscription_name => 'TUNGSTEN_PUB"
                                + "', "
                                + "publication_id    => "
                                + pub.getPublicationId()
                                + ","
                                + "column_list => '"
                                + pub.getColumnList()
                                + "',"
                                + "subscriber_view => '"
                                + viewName
                                + "');");

                subscriberViews.put(viewName, src);
                src.setSubscriptionView(viewName, pub.getPublicationId());

                if (logger.isDebugEnabled())
                    logger.debug("Creating change view " + viewName
                            + " - Now handling "
                            + subscriberViews.keySet().size() + " views");

                i++;

            }

            executeQuery("BEGIN " + subscribeStmt.toString() + " END;", false);
        }


// Step 5 Activate the subscription.
           
   executeQuery("BEGIN DBMS_CDC_SUBSCRIBE.ACTIVATE_SUBSCRIPTION("
                + "subscription_name => 'TUNGSTEN_PUB'" + ");END;", false);

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326676515&siteId=291194637