Search in sources :

Example 1 with DateTimeFormatter

use of org.joda.time.format.DateTimeFormatter in project druid by druid-io.

the class GranularityPathSpec method addInputPaths.

@Override
public Job addInputPaths(HadoopDruidIndexerConfig config, Job job) throws IOException {
    final Set<Interval> intervals = Sets.newTreeSet(Comparators.intervalsByStartThenEnd());
    for (Interval inputInterval : config.getInputIntervals()) {
        for (Interval interval : dataGranularity.getIterable(inputInterval)) {
            intervals.add(trim(inputInterval, interval));
        }
    }
    Path betaInput = new Path(inputPath);
    FileSystem fs = betaInput.getFileSystem(job.getConfiguration());
    Set<String> paths = Sets.newTreeSet();
    Pattern fileMatcher = Pattern.compile(filePattern);
    DateTimeFormatter customFormatter = null;
    if (pathFormat != null) {
        customFormatter = DateTimeFormat.forPattern(pathFormat);
    }
    for (Interval interval : intervals) {
        DateTime t = interval.getStart();
        String intervalPath = null;
        if (customFormatter != null) {
            intervalPath = customFormatter.print(t);
        } else {
            intervalPath = dataGranularity.toPath(t);
        }
        Path granularPath = new Path(betaInput, intervalPath);
        log.info("Checking path[%s]", granularPath);
        for (FileStatus status : FSSpideringIterator.spiderIterable(fs, granularPath)) {
            final Path filePath = status.getPath();
            if (fileMatcher.matcher(filePath.toString()).matches()) {
                paths.add(filePath.toString());
            }
        }
    }
    log.info("Appending path %s", paths);
    StaticPathSpec.addToMultipleInputs(config, job, paths, inputFormat);
    return job;
}
Also used : Path(org.apache.hadoop.fs.Path) Pattern(java.util.regex.Pattern) FileStatus(org.apache.hadoop.fs.FileStatus) FileSystem(org.apache.hadoop.fs.FileSystem) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) DateTime(org.joda.time.DateTime) Interval(org.joda.time.Interval)

Example 2 with DateTimeFormatter

use of org.joda.time.format.DateTimeFormatter in project elasticsearch by elastic.

the class BaseXContentTestCase method testReadableInstant.

public void testReadableInstant() throws Exception {
    assertResult("{'instant':null}", () -> builder().startObject().field("instant", (ReadableInstant) null).endObject());
    assertResult("{'instant':null}", () -> builder().startObject().field("instant").value((ReadableInstant) null).endObject());
    final DateTime t1 = new DateTime(2016, 1, 1, 0, 0, DateTimeZone.UTC);
    String expected = "{'t1':'2016-01-01T00:00:00.000Z'}";
    assertResult(expected, () -> builder().startObject().field("t1", t1).endObject());
    assertResult(expected, () -> builder().startObject().field("t1").value(t1).endObject());
    final DateTime t2 = new DateTime(2016, 12, 25, 7, 59, 42, 213, DateTimeZone.UTC);
    expected = "{'t2':'2016-12-25T07:59:42.213Z'}";
    assertResult(expected, () -> builder().startObject().field("t2", t2).endObject());
    assertResult(expected, () -> builder().startObject().field("t2").value(t2).endObject());
    final DateTimeFormatter formatter = randomFrom(ISODateTimeFormat.basicDate(), ISODateTimeFormat.dateTimeNoMillis());
    final DateTime t3 = DateTime.now();
    expected = "{'t3':'" + formatter.print(t3) + "'}";
    assertResult(expected, () -> builder().startObject().field("t3", t3, formatter).endObject());
    assertResult(expected, () -> builder().startObject().field("t3").value(t3, formatter).endObject());
    final DateTime t4 = new DateTime(randomDateTimeZone());
    expected = "{'t4':'" + formatter.print(t4) + "'}";
    assertResult(expected, () -> builder().startObject().field("t4", t4, formatter).endObject());
    assertResult(expected, () -> builder().startObject().field("t4").value(t4, formatter).endObject());
    // 1970-01-01T00:00:00Z - 2033-05-18T05:33:20.000+02:00
    long date = Math.abs(randomLong() % (2 * (long) 10e11));
    final DateTime t5 = new DateTime(date, randomDateTimeZone());
    expected = "{'t5':'" + XContentBuilder.DEFAULT_DATE_PRINTER.print(t5) + "'}";
    assertResult(expected, () -> builder().startObject().field("t5", t5).endObject());
    assertResult(expected, () -> builder().startObject().field("t5").value(t5).endObject());
    expected = "{'t5':'" + formatter.print(t5) + "'}";
    assertResult(expected, () -> builder().startObject().field("t5", t5, formatter).endObject());
    assertResult(expected, () -> builder().startObject().field("t5").value(t5, formatter).endObject());
    // 2016-01-01T00:00:00.000Z
    Instant i1 = new Instant(1451606400000L);
    expected = "{'i1':'2016-01-01T00:00:00.000Z'}";
    assertResult(expected, () -> builder().startObject().field("i1", i1).endObject());
    assertResult(expected, () -> builder().startObject().field("i1").value(i1).endObject());
    // 2016-12-25T07:59:42.213Z
    Instant i2 = new Instant(1482652782213L);
    expected = "{'i2':'" + formatter.print(i2) + "'}";
    assertResult(expected, () -> builder().startObject().field("i2", i2, formatter).endObject());
    assertResult(expected, () -> builder().startObject().field("i2").value(i2, formatter).endObject());
    expectNonNullFormatterException(() -> builder().startObject().field("t3", t3, null).endObject());
    expectNonNullFormatterException(() -> builder().startObject().field("t3").value(t3, null).endObject());
}
Also used : ReadableInstant(org.joda.time.ReadableInstant) Instant(org.joda.time.Instant) Matchers.containsString(org.hamcrest.Matchers.containsString) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) DateTime(org.joda.time.DateTime)

Example 3 with DateTimeFormatter

use of org.joda.time.format.DateTimeFormatter in project elasticsearch by elastic.

the class BaseXContentTestCase method testObject.

public void testObject() throws Exception {
    Map<String, Object> object = new HashMap<>();
    object.put("{'object':false}", Boolean.FALSE);
    object.put("{'object':13}", (byte) 13);
    object.put("{'object':5.0}", 5.0d);
    object.put("{'object':8.0}", 8.0f);
    object.put("{'object':{'lat':45.759429931640625,'lon':4.8394775390625}}", GeoPoint.fromGeohash("u05kq4k"));
    object.put("{'object':3}", 3);
    object.put("{'object':2}", 2L);
    object.put("{'object':1}", (short) 1);
    object.put("{'object':'string'}", "string");
    object.put("{'object':'a'}", new Text("a"));
    object.put("{'object':'b'}", new Text(new BytesArray("b")));
    object.put("{'object':null}", null);
    object.put("{'object':'OPEN'}", IndexMetaData.State.OPEN);
    object.put("{'object':'NM'}", DistanceUnit.NAUTICALMILES);
    object.put("{'object':{'f1':'v1'}}", singletonMap("f1", "v1"));
    object.put("{'object':{'f1':{'f2':'v2'}}}", singletonMap("f1", singletonMap("f2", "v2")));
    object.put("{'object':[1,2,3]}", Arrays.asList(1, 2, 3));
    final String path = Constants.WINDOWS ? "{'object':'a\\\\b\\\\c'}" : "{'object':'a/b/c'}";
    object.put(path, PathUtils.get("a", "b", "c"));
    final DateTimeFormatter formatter = XContentBuilder.DEFAULT_DATE_PRINTER;
    final Date d1 = new DateTime(2016, 1, 1, 0, 0, DateTimeZone.UTC).toDate();
    object.put("{'object':'" + formatter.print(d1.getTime()) + "'}", d1);
    final DateTime d2 = DateTime.now();
    object.put("{'object':'" + formatter.print(d2) + "'}", d2);
    final Calendar c1 = new DateTime(2010, 1, 1, 0, 0, DateTimeZone.UTC).toCalendar(Locale.ROOT);
    object.put("{'object':'2010-01-01T00:00:00.000Z'}", c1);
    final ToXContent x1 = (builder, params) -> builder.startObject().field("f1", "v1").field("f2", 2).array("f3", 3, 4, 5).endObject();
    final ToXContent x2 = (builder, params) -> builder.startObject().field("f1", "v1").field("f2", x1).endObject();
    object.put("{'object':{'f1':'v1','f2':{'f1':'v1','f2':2,'f3':[3,4,5]}}}", x2);
    for (Map.Entry<String, Object> o : object.entrySet()) {
        final String expected = o.getKey();
        assertResult(expected, () -> builder().humanReadable(true).startObject().field("object", o.getValue()).endObject());
        assertResult(expected, () -> builder().humanReadable(true).startObject().field("object").value(o.getValue()).endObject());
    }
    assertResult("{'objects':[null,null,null]}", () -> builder().startObject().array("objects", null, null, null).endObject());
}
Also used : ElasticsearchException(org.elasticsearch.ElasticsearchException) DateTimeZone(org.joda.time.DateTimeZone) Arrays(java.util.Arrays) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) Date(java.util.Date) HashMap(java.util.HashMap) Strings(org.elasticsearch.common.Strings) ArrayList(java.util.ArrayList) BytesArray(org.elasticsearch.common.bytes.BytesArray) Calendar(java.util.Calendar) ByteArrayInputStream(java.io.ByteArrayInputStream) Text(org.elasticsearch.common.text.Text) Locale(java.util.Locale) Map(java.util.Map) GeoPoint(org.elasticsearch.common.geo.GeoPoint) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) BigInteger(java.math.BigInteger) Collections.singletonMap(java.util.Collections.singletonMap) ESTestCase(org.elasticsearch.test.ESTestCase) ParseField(org.elasticsearch.common.ParseField) JsonParseException(com.fasterxml.jackson.core.JsonParseException) Path(java.nio.file.Path) PathUtils(org.elasticsearch.common.io.PathUtils) ISODateTimeFormat(org.joda.time.format.ISODateTimeFormat) Collections.emptyMap(java.util.Collections.emptyMap) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) BytesRef(org.apache.lucene.util.BytesRef) Matchers.allOf(org.hamcrest.Matchers.allOf) DateTime(org.joda.time.DateTime) Matchers(org.hamcrest.Matchers) ReadableInstant(org.joda.time.ReadableInstant) IOException(java.io.IOException) BytesReference(org.elasticsearch.common.bytes.BytesReference) Matchers.startsWith(org.hamcrest.Matchers.startsWith) TimeUnit(java.util.concurrent.TimeUnit) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) List(java.util.List) Constants(org.apache.lucene.util.Constants) DistanceUnit(org.elasticsearch.common.unit.DistanceUnit) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) Token(org.elasticsearch.common.xcontent.XContentParser.Token) Matcher(org.hamcrest.Matcher) Instant(org.joda.time.Instant) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Collections(java.util.Collections) Matchers.containsString(org.hamcrest.Matchers.containsString) BytesArray(org.elasticsearch.common.bytes.BytesArray) HashMap(java.util.HashMap) Calendar(java.util.Calendar) Text(org.elasticsearch.common.text.Text) Matchers.containsString(org.hamcrest.Matchers.containsString) Date(java.util.Date) DateTime(org.joda.time.DateTime) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) HashMap(java.util.HashMap) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap) Collections.emptyMap(java.util.Collections.emptyMap)

Example 4 with DateTimeFormatter

use of org.joda.time.format.DateTimeFormatter in project elasticsearch by elastic.

the class SimpleJodaTests method assertDateFormatParsingThrowingException.

private void assertDateFormatParsingThrowingException(String pattern, String invalidDate) {
    try {
        FormatDateTimeFormatter formatter = Joda.forPattern(pattern);
        DateTimeFormatter parser = formatter.parser();
        parser.parseMillis(invalidDate);
        fail(String.format(Locale.ROOT, "Expected parsing exception for pattern [%s] with date [%s], but did not happen", pattern, invalidDate));
    } catch (IllegalArgumentException e) {
    }
}
Also used : FormatDateTimeFormatter(org.elasticsearch.common.joda.FormatDateTimeFormatter) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) FormatDateTimeFormatter(org.elasticsearch.common.joda.FormatDateTimeFormatter)

Example 5 with DateTimeFormatter

use of org.joda.time.format.DateTimeFormatter in project elasticsearch by elastic.

the class SimpleJodaTests method testIsoVsCustom.

public void testIsoVsCustom() {
    DateTimeFormatter formatter = ISODateTimeFormat.dateOptionalTimeParser().withZone(DateTimeZone.UTC);
    long millis = formatter.parseMillis("1970-01-01T00:00:00");
    assertThat(millis, equalTo(0L));
    formatter = DateTimeFormat.forPattern("yyyy/MM/dd HH:mm:ss").withZone(DateTimeZone.UTC);
    millis = formatter.parseMillis("1970/01/01 00:00:00");
    assertThat(millis, equalTo(0L));
    FormatDateTimeFormatter formatter2 = Joda.forPattern("yyyy/MM/dd HH:mm:ss");
    millis = formatter2.parser().parseMillis("1970/01/01 00:00:00");
    assertThat(millis, equalTo(0L));
}
Also used : FormatDateTimeFormatter(org.elasticsearch.common.joda.FormatDateTimeFormatter) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) FormatDateTimeFormatter(org.elasticsearch.common.joda.FormatDateTimeFormatter)

Aggregations

DateTimeFormatter (org.joda.time.format.DateTimeFormatter)189 DateTime (org.joda.time.DateTime)88 Date (java.util.Date)40 Test (org.junit.Test)25 DateTimeZone (org.joda.time.DateTimeZone)19 ArrayList (java.util.ArrayList)14 SolrInputDocument (org.apache.solr.common.SolrInputDocument)12 IndexSchema (org.apache.solr.schema.IndexSchema)12 DateTimeFormatterBuilder (org.joda.time.format.DateTimeFormatterBuilder)12 HashMap (java.util.HashMap)10 CswSourceConfiguration (org.codice.ddf.spatial.ogc.csw.catalog.common.CswSourceConfiguration)10 IOException (java.io.IOException)9 Calendar (java.util.Calendar)8 Map (java.util.Map)8 DatasetConfigDTO (com.linkedin.thirdeye.datalayer.dto.DatasetConfigDTO)7 FormatDateTimeFormatter (org.elasticsearch.common.joda.FormatDateTimeFormatter)7 Test (org.testng.annotations.Test)7 TimeSpec (com.linkedin.thirdeye.api.TimeSpec)6 FilterType (net.opengis.filter.v_1_1_0.FilterType)6 Deployment (org.activiti.engine.test.Deployment)6