All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.facebook.presto.decoder.json.RFC2822JsonFieldDecoder Maven / Gradle / Ivy

There is a newer version: 0.289
Show newest version
/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.facebook.presto.decoder.json;

import com.facebook.presto.common.type.Type;
import com.facebook.presto.decoder.DecoderColumnHandle;
import com.facebook.presto.decoder.FieldValueProvider;
import com.facebook.presto.spi.PrestoException;
import com.fasterxml.jackson.databind.JsonNode;
import com.google.common.collect.ImmutableSet;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.util.Locale;
import java.util.Set;

import static com.facebook.presto.common.type.DateType.DATE;
import static com.facebook.presto.common.type.TimeType.TIME;
import static com.facebook.presto.common.type.TimeWithTimeZoneType.TIME_WITH_TIME_ZONE;
import static com.facebook.presto.common.type.TimestampType.TIMESTAMP;
import static com.facebook.presto.common.type.TimestampWithTimeZoneType.TIMESTAMP_WITH_TIME_ZONE;
import static com.facebook.presto.decoder.DecoderErrorCode.DECODER_CONVERSION_NOT_SUPPORTED;
import static com.facebook.presto.decoder.json.JsonRowDecoderFactory.throwUnsupportedColumnType;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;

/**
 * RFC 2822 date format decoder.
 * 

* Uses hardcoded UTC timezone and english locale. */ public class RFC2822JsonFieldDecoder implements JsonFieldDecoder { private static final Set SUPPORTED_TYPES = ImmutableSet.of(DATE, TIME, TIME_WITH_TIME_ZONE, TIMESTAMP, TIMESTAMP_WITH_TIME_ZONE); /** * Todo - configurable time zones and locales. */ private static final DateTimeFormatter FORMATTER = DateTimeFormat.forPattern("EEE MMM dd HH:mm:ss Z yyyy").withLocale(Locale.ENGLISH).withZoneUTC(); private final DecoderColumnHandle columnHandle; public RFC2822JsonFieldDecoder(DecoderColumnHandle columnHandle) { this.columnHandle = requireNonNull(columnHandle, "columnHandle is null"); if (!SUPPORTED_TYPES.contains(columnHandle.getType())) { throwUnsupportedColumnType(columnHandle); } } @Override public FieldValueProvider decode(JsonNode value) { return new RFC2822JsonValueProvider(value, columnHandle); } public static class RFC2822JsonValueProvider extends AbstractDateTimeJsonValueProvider { public RFC2822JsonValueProvider(JsonNode value, DecoderColumnHandle columnHandle) { super(value, columnHandle); } @Override protected long getMillis() { if (value.isValueNode()) { try { return FORMATTER.parseMillis(value.asText()); } catch (IllegalArgumentException e) { throw new PrestoException( DECODER_CONVERSION_NOT_SUPPORTED, format("could not parse value '%s' as '%s' for column '%s'", value.asText(), columnHandle.getType(), columnHandle.getName())); } } throw new PrestoException( DECODER_CONVERSION_NOT_SUPPORTED, format("could not parse non-value node as '%s' for column '%s'", columnHandle.getType(), columnHandle.getName())); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy