Example usage for java.time LocalDateTime format

List of usage examples for java.time LocalDateTime format

Introduction

In this page you can find the example usage for java.time LocalDateTime format.

Prototype

@Override 
public String format(DateTimeFormatter formatter) 

Source Link

Document

Formats this date-time using the specified formatter.

Usage

From source file:com.streamsets.pipeline.stage.origin.jdbc.cdc.oracle.OracleCDCSource.java

private void startGeneratorThread(String lastSourceOffset) throws StageException, SQLException {
    Offset offset = null;/*from   w  w  w.  j av a 2 s  .  c o  m*/
    LocalDateTime startTimestamp;
    try {
        startLogMnrForRedoDict();
        if (!StringUtils.isEmpty(lastSourceOffset)) {
            offset = new Offset(lastSourceOffset);
            if (lastSourceOffset.startsWith("v3")) {
                if (!useLocalBuffering) {
                    throw new StageException(JDBC_82);
                }
                startTimestamp = offset.timestamp.minusSeconds(configBean.txnWindow);
            } else {
                if (useLocalBuffering) {
                    throw new StageException(JDBC_83);
                }
                startTimestamp = getDateForSCN(new BigDecimal(offset.scn));
            }
            offset.timestamp = startTimestamp;
            adjustStartTimeAndStartLogMnr(startTimestamp);
        } else { // reset the start date only if it not set.
            if (configBean.startValue != StartValues.SCN) {
                LocalDateTime startDate;
                if (configBean.startValue == StartValues.DATE) {
                    startDate = LocalDateTime.parse(configBean.startDate, dateTimeColumnHandler.dateFormatter);
                } else {
                    startDate = nowAtDBTz();
                }
                startDate = adjustStartTimeAndStartLogMnr(startDate);
                offset = new Offset(version, startDate, ZERO, 0, "");
            } else {
                BigDecimal startCommitSCN = new BigDecimal(configBean.startSCN);
                startLogMnrSCNToDate.setBigDecimal(1, startCommitSCN);
                final LocalDateTime start = getDateForSCN(startCommitSCN);
                LocalDateTime endTime = getEndTimeForStartTime(start);
                startLogMnrSCNToDate.setString(2, endTime.format(dateTimeColumnHandler.dateFormatter));
                startLogMnrSCNToDate.execute();
                offset = new Offset(version, start, startCommitSCN.toPlainString(), 0, "");
            }
        }
    } catch (SQLException ex) {
        LOG.error("SQLException while trying to setup record generator thread", ex);
        generationStarted = false;
        throw new StageException(JDBC_52, ex);
    }
    final Offset os = offset;
    final PreparedStatement select = selectFromLogMnrContents;
    generationExecutor.submit(() -> {
        try {
            generateRecords(os, select);
        } catch (Throwable ex) {
            LOG.error("Error while producing records", ex);
            generationStarted = false;
        }
    });
}

From source file:rjc.jplanner.model.DateTime.java

/****************************************** toString *******************************************/
public String toString(String format) {
    // convert to string in specified format
    LocalDateTime ldt = LocalDateTime.ofEpochSecond(m_milliseconds / 1000L,
            (int) (m_milliseconds % 1000 * 1000000), ZoneOffset.UTC);

    // to support half-of-year using Bs, quote any unquoted Bs in format
    StringBuilder newFormat = new StringBuilder();
    boolean inQuote = false;
    boolean inB = false;
    char here;/*  w w  w .j  a va  2s  . com*/
    for (int i = 0; i < format.length(); i++) {
        here = format.charAt(i);

        // are we in quoted text?
        if (here == QUOTE)
            inQuote = !inQuote;

        // replace unquoted Bs with special code
        if (inB && here == CHARB) {
            newFormat.append(CODE);
            continue;
        }

        // come to end of unquoted Bs
        if (inB && here != CHARB) {
            newFormat.append(QUOTE);
            inB = false;
            inQuote = false;
        }

        // start of unquoted Bs, start quote with special code
        if (!inQuote && here == CHARB) {
            // avoid creating double quotes
            if (newFormat.length() > 0 && newFormat.charAt(newFormat.length() - 1) == QUOTE) {
                newFormat.deleteCharAt(newFormat.length() - 1);
                newFormat.append(CODE);
            } else
                newFormat.append("'" + CODE);
            inQuote = true;
            inB = true;
        } else {
            newFormat.append(here);
        }
    }

    // close quote if quote still open
    if (inQuote)
        newFormat.append(QUOTE);

    String str = ldt.format(DateTimeFormatter.ofPattern(newFormat.toString()));

    // no special code so can return string immediately
    if (!str.contains(CODE))
        return str;

    // determine half-of-year
    String yearHalf;
    if (month() < 7)
        yearHalf = "1";
    else
        yearHalf = "2";

    // four or more Bs is not allowed
    String Bs = StringUtils.repeat(CODE, 4);
    if (str.contains(Bs))
        throw new IllegalArgumentException("Too many pattern letters: B");

    // replace three Bs
    Bs = StringUtils.repeat(CODE, 3);
    if (yearHalf.equals("1"))
        str = str.replace(Bs, yearHalf + "st half");
    else
        str = str.replace(Bs, yearHalf + "nd half");

    // replace two Bs
    Bs = StringUtils.repeat(CODE, 2);
    str = str.replace(Bs, "H" + yearHalf);

    // replace one Bs
    Bs = StringUtils.repeat(CODE, 1);
    str = str.replace(Bs, yearHalf);

    return str;
}

From source file:com.streamsets.pipeline.stage.origin.jdbc.cdc.oracle.OracleCDCSource.java

@NotNull
private LocalDateTime adjustStartTimeAndStartLogMnr(LocalDateTime startDate)
        throws SQLException, StageException {
    startDate = adjustStartTime(startDate);
    LocalDateTime endTime = getEndTimeForStartTime(startDate);
    startLogMinerUsingGivenDates(startDate.format(dateTimeColumnHandler.dateFormatter),
            endTime.format(dateTimeColumnHandler.dateFormatter));
    LOG.debug(START_TIME_END_TIME, startDate, endTime);
    return startDate;
}

From source file:org.apache.hadoop.yarn.server.resourcemanager.security.TestHopsworksRMAppSecurityActions.java

@Test
public void testConfUpdate() throws Exception {
    LocalDateTime now = LocalDateTime.now();
    Date nbf = DateUtils.localDateTime2Date(now);
    LocalDateTime expiration = now.plus(10, ChronoUnit.MINUTES);
    Date expirationDate = DateUtils.localDateTime2Date(expiration);
    JWTClaimsSet masterClaims = new JWTClaimsSet();
    masterClaims.setSubject("master_token");
    masterClaims.setExpirationTime(expirationDate);
    masterClaims.setNotBeforeTime(nbf);/* w  w  w. jav  a  2s.  c o  m*/
    String newMasterToken = jwtIssuer.generate(masterClaims);
    Assert.assertNotNull(newMasterToken);

    String[] newRenewalTokens = new String[5];
    JWTClaimsSet renewClaims = new JWTClaimsSet();
    renewClaims.setSubject("renew_token");
    renewClaims.setExpirationTime(expirationDate);
    renewClaims.setNotBeforeTime(nbf);
    for (int i = 0; i < newRenewalTokens.length; i++) {
        String renewToken = jwtIssuer.generate(renewClaims);
        Assert.assertNotNull(renewToken);
        newRenewalTokens[i] = renewToken;
    }
    RMAppSecurityActions actor = new TestingHopsworksActions(newMasterToken, expirationDate, newRenewalTokens);
    ((Configurable) actor).setConf(conf);
    actor.init();
    TimeUnit.MILLISECONDS.sleep(500);

    // Renewal must have happened, check new values in ssl-server
    Configuration sslConf = new Configuration();
    sslConf.addResource(conf.get(SSLFactory.SSL_SERVER_CONF_KEY));
    String newMasterTokenConf = sslConf.get(YarnConfiguration.RM_JWT_MASTER_TOKEN, "");
    Assert.assertEquals(newMasterToken, newMasterTokenConf);
    for (int i = 0; i < newRenewalTokens.length; i++) {
        String confKey = String.format(YarnConfiguration.RM_JWT_RENEW_TOKEN_PATTERN, i);
        String newRenewalToken = sslConf.get(confKey, "");
        Assert.assertEquals(newRenewalTokens[i], newRenewalToken);
    }

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
    Assert.assertEquals(expiration.format(formatter),
            ((HopsworksRMAppSecurityActions) actor).getMasterTokenExpiration().format(formatter));
    actor.destroy();
}

From source file:com.streamsets.pipeline.stage.origin.jdbc.cdc.oracle.OracleCDCSource.java

private void generateRecords(Offset startingOffset, PreparedStatement selectChanges) {
    // When this is called the first time, Logminer was started either from SCN or from a start date, so we just keep
    // track of the start date etc.
    LOG.info("Attempting to generate records");
    boolean error;
    StringBuilder query = new StringBuilder();
    BigDecimal lastCommitSCN = new BigDecimal(startingOffset.scn);
    int sequenceNumber = startingOffset.sequence;
    LocalDateTime startTime = adjustStartTime(startingOffset.timestamp);
    String lastTxnId = startingOffset.txnId;
    LocalDateTime endTime = getEndTimeForStartTime(startTime);
    ResultSet resultSet = null;/*from   w w w  . java  2  s  . c  o m*/
    while (!getContext().isStopped()) {
        error = false;
        generationStarted = true;
        try {
            recordQueue.put(new RecordOffset(dummyRecord,
                    new Offset(version, startTime, lastCommitSCN.toPlainString(), sequenceNumber, lastTxnId)));
            selectChanges = getSelectChangesStatement();
            if (!useLocalBuffering) {
                selectChanges.setBigDecimal(1, lastCommitSCN);
                selectChanges.setInt(2, sequenceNumber);
                selectChanges.setBigDecimal(3, lastCommitSCN);
                if (shouldTrackDDL) {
                    selectChanges.setBigDecimal(4, lastCommitSCN);
                }
            }
            selectChanges.setFetchSize(configBean.jdbcFetchSize);
            resultSet = selectChanges.executeQuery();
            while (resultSet.next() && !getContext().isStopped()) {
                String queryFragment = resultSet.getString(5);
                BigDecimal scnDecimal = resultSet.getBigDecimal(1);
                String scn = scnDecimal.toPlainString();
                String xidUsn = String.valueOf(resultSet.getLong(10));
                String xidSlt = String.valueOf(resultSet.getString(11));
                String xidSqn = String.valueOf(resultSet.getString(12));
                String xid = xidUsn + "." + xidSlt + "." + xidSqn;
                // Query Fragment is not null -> we need to process
                // Query Fragment is null AND the query string buffered from previous rows due to CSF == 0 is null,
                // nothing to do, go to next row
                // Query Fragment is null, but there is previously buffered data in the query, go ahead and process.
                if (queryFragment != null) {
                    query.append(queryFragment);
                } else if (queryFragment == null && query.length() == 0) {
                    LOG.debug(READ_NULL_QUERY_FROM_ORACLE, scn, xid);
                    continue;
                }

                // CSF is 1 if the query is incomplete, so read the next row before parsing
                // CSF being 0 means query is complete, generate the record
                if (resultSet.getInt(9) == 0) {
                    if (query.length() == 0) {
                        LOG.debug(READ_NULL_QUERY_FROM_ORACLE, scn, xid);
                        continue;
                    }
                    String queryString = query.toString();
                    query.setLength(0);
                    String username = resultSet.getString(2);
                    short op = resultSet.getShort(3);
                    String timestamp = resultSet.getString(4);
                    LocalDateTime tsDate = Timestamp.valueOf(timestamp).toLocalDateTime();
                    delay.getValue().put("delay", getDelay(tsDate));
                    String table = resultSet.getString(6);
                    BigDecimal commitSCN = resultSet.getBigDecimal(7);
                    int seq = resultSet.getInt(8);

                    String rsId = resultSet.getString(13);
                    Object ssn = resultSet.getObject(14);
                    String schema = String.valueOf(resultSet.getString(15));
                    int rollback = resultSet.getInt(16);
                    String rowId = resultSet.getString(17);
                    SchemaAndTable schemaAndTable = new SchemaAndTable(schema, table);
                    TransactionIdKey key = new TransactionIdKey(xid);
                    bufferedRecordsLock.lock();
                    try {
                        if (useLocalBuffering && bufferedRecords.containsKey(key) && bufferedRecords.get(key)
                                .contains(new RecordSequence(null, null, 0, 0, rsId, ssn, null))) {
                            continue;
                        }
                    } finally {
                        bufferedRecordsLock.unlock();
                    }
                    Offset offset = null;
                    if (LOG.isDebugEnabled()) {
                        LOG.debug(
                                "Commit SCN = {}, SCN = {}, Operation = {}, Txn Id = {}, Timestamp = {}, Row Id = {}, Redo SQL = {}",
                                commitSCN, scn, op, xid, tsDate, rowId, queryString);
                    }

                    if (op != DDL_CODE && op != COMMIT_CODE && op != ROLLBACK_CODE) {
                        if (!useLocalBuffering) {
                            offset = new Offset(version, tsDate, commitSCN.toPlainString(), seq, xid);
                        }
                        Map<String, String> attributes = new HashMap<>();
                        attributes.put(SCN, scn);
                        attributes.put(USER, username);
                        attributes.put(TIMESTAMP_HEADER, timestamp);
                        attributes.put(TABLE, table);
                        attributes.put(SEQ, String.valueOf(seq));
                        attributes.put(XID, xid);
                        attributes.put(RS_ID, rsId);
                        attributes.put(SSN, ssn.toString());
                        attributes.put(SCHEMA, schema);
                        attributes.put(ROLLBACK, String.valueOf(rollback));
                        attributes.put(ROWID_KEY, rowId);
                        if (!useLocalBuffering || getContext().isPreview()) {
                            if (commitSCN.compareTo(lastCommitSCN) < 0
                                    || (commitSCN.compareTo(lastCommitSCN) == 0 && seq < sequenceNumber)) {
                                continue;
                            }
                            lastCommitSCN = commitSCN;
                            sequenceNumber = seq;
                            if (configBean.keepOriginalQuery) {
                                attributes.put(QUERY_KEY, queryString);
                            }
                            try {
                                Record record = generateRecord(queryString, attributes, op);
                                if (record != null && record.getEscapedFieldPaths().size() > 0) {
                                    recordQueue.put(new RecordOffset(record, offset));
                                }
                            } catch (UnparseableSQLException ex) {
                                LOG.error("Parsing failed", ex);
                                unparseable.offer(queryString);
                            }
                        } else {
                            bufferedRecordsLock.lock();
                            try {
                                HashQueue<RecordSequence> records = bufferedRecords.computeIfAbsent(key, x -> {
                                    x.setTxnStartTime(tsDate);
                                    return createTransactionBuffer(key.txnId);
                                });

                                int nextSeq = records.isEmpty() ? 1 : records.tail().seq + 1;
                                RecordSequence node = new RecordSequence(attributes, queryString, nextSeq, op,
                                        rsId, ssn, tsDate);
                                records.add(node);
                            } finally {
                                bufferedRecordsLock.unlock();
                            }
                        }
                    } else if (!getContext().isPreview() && useLocalBuffering
                            && (op == COMMIT_CODE || op == ROLLBACK_CODE)) {
                        // so this commit was previously processed or it is a rollback, so don't care.
                        if (op == ROLLBACK_CODE || scnDecimal.compareTo(lastCommitSCN) < 0) {
                            bufferedRecordsLock.lock();
                            try {
                                bufferedRecords.remove(key);
                            } finally {
                                bufferedRecordsLock.unlock();
                            }
                        } else {
                            bufferedRecordsLock.lock();
                            try {
                                HashQueue<RecordSequence> records = bufferedRecords.getOrDefault(key,
                                        EMPTY_LINKED_HASHSET);
                                if (lastCommitSCN.equals(scnDecimal) && xid.equals(lastTxnId)) {
                                    removeProcessedRecords(records, sequenceNumber);
                                }
                                int bufferedRecordsToBeRemoved = records.size();
                                LOG.debug(FOUND_RECORDS_IN_TRANSACTION, bufferedRecordsToBeRemoved, xid);
                                lastCommitSCN = scnDecimal;
                                lastTxnId = xid;
                                sequenceNumber = addRecordsToQueue(tsDate, scn, xid);
                            } finally {
                                bufferedRecordsLock.unlock();
                            }
                        }
                    } else {
                        offset = new Offset(version, tsDate, scn, 0, xid);
                        boolean sendSchema = false;
                        // Commit/rollback in Preview will also end up here, so don't really do any of the following in preview
                        // Don't bother with DDL events here.
                        if (!getContext().isPreview()) {
                            // Event is sent on every DDL, but schema is not always sent.
                            // Schema sending logic:
                            // CREATE/ALTER: Schema is sent if the schema after the ALTER is newer than the cached schema
                            // (which we would have sent as an event earlier, at the last alter)
                            // DROP/TRUNCATE: Schema is not sent, since they don't change schema.
                            DDL_EVENT type = getDdlType(queryString);
                            if (type == DDL_EVENT.ALTER || type == DDL_EVENT.CREATE) {
                                sendSchema = refreshSchema(scnDecimal, new SchemaAndTable(schema, table));
                            }
                            recordQueue.put(new RecordOffset(createEventRecord(type, queryString,
                                    schemaAndTable, offset.toString(), sendSchema, timestamp), offset));
                        }
                    }
                    query.setLength(0);
                }
            }
        } catch (SQLException ex) {
            error = true;
            // force a restart from the same timestamp.
            if (ex.getErrorCode() == MISSING_LOG_FILE) {
                LOG.warn("SQL Exception while retrieving records", ex);
                addToStageExceptionsQueue(new StageException(JDBC_86, ex));
            } else if (ex.getErrorCode() != RESULTSET_CLOSED_AS_LOGMINER_SESSION_CLOSED) {
                LOG.warn("SQL Exception while retrieving records", ex);
            } else if (ex.getErrorCode() == QUERY_TIMEOUT) {
                LOG.warn("LogMiner select query timed out");
            } else if (ex.getErrorCode() == LOGMINER_START_MUST_BE_CALLED) {
                LOG.warn("Last LogMiner session did not start successfully. Will retry", ex);
            } else {
                LOG.error("Error while reading data", ex);
                addToStageExceptionsQueue(new StageException(JDBC_52, ex));
            }
        } catch (StageException e) {
            LOG.error("Error while reading data", e);
            error = true;
            addToStageExceptionsQueue(e);
        } catch (InterruptedException ex) {
            LOG.error("Interrupted while waiting to add data");
            Thread.currentThread().interrupt();
        } catch (Exception ex) {
            LOG.error("Error while reading data", ex);
            error = true;
            addToStageExceptionsQueue(new StageException(JDBC_52, ex));
        } finally {
            // If an incomplete batch is seen, it means we are going to move the window forward
            // Ending this session and starting a new one helps reduce PGA memory usage.
            try {
                if (resultSet != null && !resultSet.isClosed()) {
                    resultSet.close();
                }
                if (selectChanges != null && !selectChanges.isClosed()) {
                    selectChanges.close();
                }
            } catch (SQLException ex) {
                LOG.warn("Error while attempting to close SQL statements", ex);
            }
            try {
                endLogMnr.execute();
            } catch (SQLException ex) {
                LOG.warn("Error while trying to close logminer session", ex);
            }
            try {
                if (error) {
                    resetConnectionsQuietly();
                } else {
                    discardOldUncommitted(startTime);
                    startTime = adjustStartTime(endTime);
                    endTime = getEndTimeForStartTime(startTime);
                }
                startLogMinerUsingGivenDates(startTime.format(dateTimeColumnHandler.dateFormatter),
                        endTime.format(dateTimeColumnHandler.dateFormatter));
            } catch (SQLException ex) {
                LOG.error("Error while attempting to start LogMiner", ex);
                addToStageExceptionsQueue(new StageException(JDBC_52, ex));
            } catch (StageException ex) {
                LOG.error("Error while attempting to start logminer for redo log dictionary", ex);
                addToStageExceptionsQueue(ex);
            }
        }
    }
}