Example usage for org.joda.time.format ISODateTimeFormat dateOptionalTimeParser

List of usage examples for org.joda.time.format ISODateTimeFormat dateOptionalTimeParser

Introduction

In this page you can find the example usage for org.joda.time.format ISODateTimeFormat dateOptionalTimeParser.

Prototype

public static DateTimeFormatter dateOptionalTimeParser() 

Source Link

Document

Returns a generic ISO datetime parser where the date is mandatory and the time is optional.

Usage

From source file:cn.cnic.bigdatalab.flume.sink.mongodb.EventParser.java

License:Apache License

public Object parseValue(final FieldDefinition fd, final String stringValue) {
    if (fd == null || fd.getType() == null) {
        try {//from  w w w .  ja va2  s . com
            return JSON.parse(stringValue);
        } catch (JSONParseException ex) {
            // XXX: Default to String
            log.trace("Could not parse as JSON, defaulting to String: {}", stringValue);
            return stringValue;
        }
    }
    switch (fd.getType()) {
    case DOUBLE:
        return Double.parseDouble(stringValue);
    case STRING:
        return stringValue;
    case OBJECT:
    case ARRAY:
        // TODO: should we use customizable array representation?
        // TODO: should we check that the result is indeed an array or object?
        return JSON.parse(stringValue);
    case BINARY:
        SimpleFieldDefinition sfd = (SimpleFieldDefinition) fd;
        final String encoding = (sfd.getEncoding() == null) ? DEFAULT_BINARY_ENCODING
                : sfd.getEncoding().toLowerCase(Locale.ENGLISH);
        if ("base64".equals(encoding)) {
            return BaseEncoding.base64().decode(stringValue);
        } else {
            throw new UnsupportedOperationException("Unsupported encoding for binary type: " + encoding);
        }
        // TODO: case "UNDEFINED":
    case OBJECTID:
        return new ObjectId(stringValue);
    case BOOLEAN:
        return Boolean.parseBoolean(stringValue);
    case DATE:
        DateFormat dateFormat = ((DateFieldDefinition) fd).getDateFormat();
        if (dateFormat == null) {
            if (StringUtils.isNumeric(stringValue)) {
                return new Date(Long.parseLong(stringValue));
            } else {
                return ISODateTimeFormat.dateOptionalTimeParser().parseDateTime(stringValue).toDate();
            }
        } else {
            try {
                return dateFormat.parse(stringValue);
            } catch (ParseException ex) {
                // XXX: Default to string
                log.warn("Could not parse date, defaulting to String: {}", stringValue);
                return stringValue;
            }
        }
    case NULL:
        // TODO: Check if this is valid
        return null;
    // TODO: case "REGEX":
    // TODO: case "JAVASCRIPT":
    // TODO: case "SYMBOL":
    // TODO: case "JAVASCRIPT_SCOPE":
    case INT32:
        return Integer.parseInt(stringValue);
    case INT64:
        return Long.parseLong(stringValue);
    case DOCUMENT:
        return populateDocument((DocumentFieldDefinition) fd, stringValue);
    default:
        throw new UnsupportedOperationException("Unsupported type: " + fd.getType().name());
    }
}

From source file:com.capitalone.dashboard.util.ClientUtil.java

License:Apache License

/**
 * Canonicalizes date format returned from source system. Some source
 * systems have incorrectly formatted dates, or date times stamps that are
 * not database friendly.//from ww w.ja v  a 2 s .c  o m
 * 
 * @param nativeRs
 *            Native date format as a string
 * @return A stringified canonical date format
 */
public String toCanonicalDate(String nativeRs) {
    if (nativeRs != null && !nativeRs.isEmpty()) {
        try {
            DateTime dt = ISODateTimeFormat.dateOptionalTimeParser().parseDateTime(nativeRs);
            // add 0's at end for backwards compatability
            return ISODateTimeFormat.dateHourMinuteSecondMillis().print(dt) + "0000";
        } catch (IllegalArgumentException e) {
            LOGGER.error("Failed to parse date: " + nativeRs);
            LOGGER.debug("Exception", e);
        }
    }

    return "";
}

From source file:com.cloudera.hive.scd.SQLUpdater.java

License:Open Source License

private long asSCDTime(String text, long defaultValue) {
    if (text == null || text.isEmpty()) {
        return defaultValue;
    } else {/*from w  w w  .j  a  v  a2 s .  com*/
        try {
            return Long.valueOf(text);
        } catch (NumberFormatException e) {
            return ISODateTimeFormat.dateOptionalTimeParser().parseMillis(text);
        }
    }
}

From source file:com.facebook.presto.kudu.properties.KuduTableProperties.java

License:Apache License

private static long toUnixTimeMicros(Object obj, Type type, String name) {
    if (Number.class.isAssignableFrom(obj.getClass())) {
        return ((Number) obj).longValue();
    } else if (obj instanceof String) {
        String s = (String) obj;
        s = s.trim().replace(' ', 'T');
        long millis = ISODateTimeFormat.dateOptionalTimeParser().withZone(DateTimeZone.UTC).parseMillis(s);
        return millis * 1000;
    } else {// w w w.jav a 2  s .  co  m
        handleInvalidValue(name, type, obj);
        return 0;
    }
}

From source file:com.oneops.search.msg.processor.CIMessageProcessor.java

License:Apache License

public static void main(String[] args) {
    Map<String, String> map = new HashMap<>();
    map.put("test", "Nov 5 21:08:38 2019 GMT");
    map.put("test1", "Nov  5 21:08:38 2019 GMT");

    convertIllegalDateFormat(map, "test");
    convertIllegalDateFormat(map, "test1");
    System.out.println(map);/*from   w ww. ja  v a 2s  .com*/
    System.out.println(ISODateTimeFormat.dateOptionalTimeParser().parseDateTime(map.get("test")));
    System.out.println(ISODateTimeFormat.dateOptionalTimeParser().parseDateTime(map.get("test1")));

}

From source file:com.pinterest.secor.parser.AnalyticsMessageParser.java

License:Apache License

/**
 * Note that this depends on the {@link #jsonObject} state.
 *//*from  w ww .  j a  v  a 2  s . co m*/
@Override
public String[] extractPartitions(Message message) {
    String result[] = { defaultType, defaultDate };
    String event_type = "";
    String analytics_type = "";

    /**
     * The idea is to store a directory structure that looks like
     * analytics-bucket/27/identify/2015/05/19/27/xxxxxxxxxxxx.json
     *
     *                  ^-- repeat the hour to spread the load among 24 shards
     *
     * analytics payloads may be of type: "track", event:"user_definded_name"
     *                           or type: "identify"
     *                           or type: "page"
     *                           or type: "screen"
     */

    if (jsonObject != null) {
        Object fieldType = jsonObject.get(mConfig.getMessageTypeName()); //type
        Object fieldValue = jsonObject.get(mConfig.getMessageTimestampName()); //timestamp

        if (fieldType != null) {
            analytics_type = fieldType.toString();
            if (analytics_type.equals("track")) {
                Object fieldSecondary = jsonObject.get("event");
                event_type = sanitizePath(fieldSecondary.toString());
            } else {
                event_type = analytics_type;
            }
        }

        if (fieldValue != null) {
            try {
                DateTimeFormatter inputFormatter = ISODateTimeFormat.dateOptionalTimeParser();
                LocalDateTime datetime = LocalDateTime.parse(fieldValue.toString(), inputFormatter);
                result[1] = datetime.toString(mConfig.getMessageTimestampBucketFormat());
            } catch (Exception e) {
                LOG.warn("date = " + fieldValue.toString() + " could not be parsed with ISODateTimeFormat."
                        + " Using date default=" + defaultDate);
            }
        }
    }

    // The hour bucket where the event happened
    String hour = result[1].split("/")[3];
    result[0] = hour + "/" + event_type;

    return result;
}

From source file:com.pinterest.secor.parser.DataLakeMessageParser.java

License:Apache License

@Override
public String[] extractPartitions(Message message) {
    JSONObject jsonObject = (JSONObject) JSONValue.parse(message.getPayload());
    String result[] = { defaultType, defaultDate };

    if (jsonObject != null) {
        Object fieldType = jsonObject.get(mConfig.getMessageTypeName()); //type
        Object fieldValue = jsonObject.get(mConfig.getMessageTimestampName()); //@timestamp
        if (fieldType != null) {
            result[0] = sanitizePath(fieldType.toString());
        }/*from  w w w  . j av  a 2 s.com*/
        if (fieldValue != null) {
            try {

                DateTimeFormatter inputFormatter = ISODateTimeFormat.dateOptionalTimeParser();
                LocalDateTime datetime = LocalDateTime.parse(fieldValue.toString(), inputFormatter);
                result[1] = datetime.toString(mConfig.getMessageTimestampBucketFormat());
            } catch (Exception e) {
                LOG.warn("date = " + fieldValue.toString() + " could not be parsed with ISODateTimeFormat."
                        + " Using date default=" + defaultDate);
            }
        }
    }

    return result;
}

From source file:com.stratio.ingestion.serializer.elasticsearch.ElasticSearchSerializerWithMapping.java

License:Apache License

TimestampedEvent(Event base) {
    super();//ww  w  .  ja  v a  2  s  .co m
    setBody(base.getBody());
    Map<String, String> headers = Maps.newHashMap(base.getHeaders());

    String timestampHeader = headers.get("@timestamp");
    Long ts = null;
    if (!StringUtils.isBlank(timestampHeader)) {
        try {
            ts = Long.parseLong(timestampHeader);
            headers.put("@timestamp", ISODateTimeFormat.dateTime().withZoneUTC().print(ts));
        } catch (RuntimeException ex) {
            log.trace("Could not parse timestamp as long: {}", timestampHeader);
            try {
                ts = ISODateTimeFormat.dateOptionalTimeParser().withZoneUTC().parseMillis(timestampHeader);
            } catch (RuntimeException ex2) {
                log.trace("Could not parse timestamp as dateOptionalTime: {}", timestampHeader);
            }
        }
    }

    if (ts == null) {
        DateTime now = DateTime.now();
        ts = now.getMillis();
        headers.put("@timestamp", ISODateTimeFormat.dateTime().withZoneUTC().print(now));
    }

    this.timestamp = ts;

    setHeaders(headers);
}

From source file:com.stratio.ingestion.serializer.elasticsearch.TimeStampedEvent.java

License:Apache License

TimeStampedEvent(Event base) {
    super();/*from   ww  w  .j  a va2  s.c  om*/
    setBody(base.getBody());
    Map<String, String> headers = Maps.newHashMap(base.getHeaders());

    String timestampHeader = headers.get("@timestamp");
    Long ts = null;
    if (!StringUtils.isBlank(timestampHeader)) {
        try {
            ts = Long.parseLong(timestampHeader);
            headers.put("@timestamp", ISODateTimeFormat.dateTime().withZoneUTC().print(ts));
        } catch (RuntimeException ex) {
            log.trace("Could not parse timestamp as long: {}", timestampHeader);
            try {
                ts = ISODateTimeFormat.dateOptionalTimeParser().withZoneUTC().parseMillis(timestampHeader);
            } catch (RuntimeException ex2) {
                log.trace("Could not parse timestamp as dateOptionalTime: {}", timestampHeader);
            }
        }
    }

    if (ts == null) {
        DateTime now = DateTime.now();
        ts = now.getMillis();
        headers.put("@timestamp", ISODateTimeFormat.dateTime().withZoneUTC().print(now));
    }

    this.timestamp = ts;

    setHeaders(headers);
}

From source file:com.stratio.ingestion.sink.cassandra.EventParser.java

License:Apache License

private static Date parseDate(final String rawValue) {
    if (StringUtils.isNumeric(rawValue)) {
        return new Date(Long.parseLong(rawValue));
    } else {// w  w  w .  j  a  v  a 2 s . co  m
        return ISODateTimeFormat.dateOptionalTimeParser().parseDateTime(rawValue).toDate();
    }
}