Search in sources :

Example 46 with Timestamp

use of org.apache.hadoop.hive.common.type.Timestamp in project hive by apache.

the class HiveJsonReader method visitLeafNode.

/**
 * Visit a node if it is expected to be a primitive value (JSON leaf node).
 *
 * @param leafNode The node pointing at the JSON object
 * @param oi The ObjectInspector to parse the value (must be a
 *          PrimitiveObjectInspector)
 * @return A Java primitive Object
 * @throws SerDeException The SerDe is not configured correctly
 */
private Object visitLeafNode(final JsonNode leafNode, final ObjectInspector oi) throws SerDeException {
    final PrimitiveObjectInspector poi = (PrimitiveObjectInspector) oi;
    final PrimitiveTypeInfo typeInfo = poi.getTypeInfo();
    if (typeInfo.getPrimitiveCategory() != PrimitiveCategory.STRING) {
        Preconditions.checkArgument(leafNode.getNodeType() != JsonNodeType.OBJECT);
        Preconditions.checkArgument(leafNode.getNodeType() != JsonNodeType.ARRAY);
    }
    switch(typeInfo.getPrimitiveCategory()) {
        case INT:
            return Integer.valueOf(leafNode.asInt());
        case BYTE:
            return Byte.valueOf((byte) leafNode.asInt());
        case SHORT:
            return Short.valueOf((short) leafNode.asInt());
        case LONG:
            return Long.valueOf(leafNode.asLong());
        case BOOLEAN:
            return Boolean.valueOf(leafNode.asBoolean());
        case FLOAT:
            return Float.valueOf((float) leafNode.asDouble());
        case DOUBLE:
            return Double.valueOf(leafNode.asDouble());
        case STRING:
            if (leafNode.isValueNode()) {
                return leafNode.asText();
            } else {
                if (isEnabled(Feature.STRINGIFY_COMPLEX_FIELDS)) {
                    return leafNode.toString();
                } else {
                    throw new SerDeException("Complex field found in JSON does not match table definition: " + typeInfo.getTypeName() + ", please consider enabling `" + JsonSerDe.STRINGIFY_COMPLEX + "` table property");
                }
            }
        case BINARY:
            return getByteValue(leafNode);
        case DATE:
            return Date.valueOf(leafNode.asText());
        case TIMESTAMP:
            return tsParser.parseTimestamp(leafNode.asText());
        case DECIMAL:
            return HiveDecimal.create(leafNode.asText());
        case TIMESTAMPLOCALTZ:
            final Timestamp ts = tsParser.parseTimestamp(leafNode.asText());
            final ZoneId zid = ((TimestampLocalTZTypeInfo) typeInfo).timeZone();
            final TimestampTZ tstz = new TimestampTZ();
            tstz.set(ts.toEpochSecond(), ts.getNanos(), zid);
            return tstz;
        case VARCHAR:
            return new HiveVarchar(leafNode.asText(), ((BaseCharTypeInfo) typeInfo).getLength());
        case CHAR:
            return new HiveChar(leafNode.asText(), ((BaseCharTypeInfo) typeInfo).getLength());
        default:
            throw new SerDeException("Could not convert from string to type: " + typeInfo.getTypeName());
    }
}
Also used : TimestampTZ(org.apache.hadoop.hive.common.type.TimestampTZ) ZoneId(java.time.ZoneId) HiveChar(org.apache.hadoop.hive.common.type.HiveChar) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) TimestampLocalTZTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TimestampLocalTZTypeInfo) HiveVarchar(org.apache.hadoop.hive.common.type.HiveVarchar) Timestamp(org.apache.hadoop.hive.common.type.Timestamp) PrimitiveTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo) SerDeException(org.apache.hadoop.hive.serde2.SerDeException)

Example 47 with Timestamp

use of org.apache.hadoop.hive.common.type.Timestamp in project hive by apache.

the class PrimitiveObjectInspectorUtils method getDate.

public static Date getDate(Object o, PrimitiveObjectInspector oi) {
    if (o == null) {
        return null;
    }
    Date result = null;
    switch(oi.getPrimitiveCategory()) {
        case VOID:
            break;
        case STRING:
            StringObjectInspector soi = (StringObjectInspector) oi;
            String s = soi.getPrimitiveJavaObject(o).trim();
            if (s.length() == DATE_LENGTH) {
                result = DateParser.parseDate(s);
            } else {
                Timestamp ts = getTimestampFromString(s);
                if (ts != null) {
                    result = Date.ofEpochMilli(ts.toEpochMilli());
                }
            }
            break;
        case CHAR:
        case VARCHAR:
            {
                String val = getString(o, oi).trim();
                if (val.length() == DATE_LENGTH) {
                    result = DateParser.parseDate(val);
                } else {
                    Timestamp ts = getTimestampFromString(val);
                    if (ts != null) {
                        result = Date.ofEpochMilli(ts.toEpochMilli());
                    }
                }
                break;
            }
        case DATE:
            result = ((DateObjectInspector) oi).getPrimitiveWritableObject(o).get();
            break;
        case TIMESTAMP:
            result = DateWritableV2.timeToDate(((TimestampObjectInspector) oi).getPrimitiveWritableObject(o).getSeconds());
            break;
        case TIMESTAMPLOCALTZ:
            String tstz = oi.getPrimitiveWritableObject(o).toString();
            int divSpace = tstz.indexOf(' ');
            if (divSpace == -1) {
                return null;
            }
            result = Date.valueOf(tstz.substring(0, divSpace));
            break;
        default:
            throw new RuntimeException("Cannot convert to Date from: " + oi.getTypeName());
    }
    return result;
}
Also used : Timestamp(org.apache.hadoop.hive.common.type.Timestamp) Date(org.apache.hadoop.hive.common.type.Date)

Example 48 with Timestamp

use of org.apache.hadoop.hive.common.type.Timestamp in project hive by apache.

the class PrimitiveObjectInspectorUtils method getTimestamp.

public static Timestamp getTimestamp(Object o, PrimitiveObjectInspector inputOI, boolean intToTimestampInSeconds) {
    if (o == null) {
        return null;
    }
    Timestamp result = null;
    long longValue = 0;
    switch(inputOI.getPrimitiveCategory()) {
        case VOID:
            result = null;
            break;
        case BOOLEAN:
            longValue = ((BooleanObjectInspector) inputOI).get(o) ? 1 : 0;
            result = TimestampWritableV2.longToTimestamp(longValue, intToTimestampInSeconds);
            break;
        case BYTE:
            longValue = ((ByteObjectInspector) inputOI).get(o);
            result = TimestampWritableV2.longToTimestamp(longValue, intToTimestampInSeconds);
            break;
        case SHORT:
            longValue = ((ShortObjectInspector) inputOI).get(o);
            result = TimestampWritableV2.longToTimestamp(longValue, intToTimestampInSeconds);
            break;
        case INT:
            longValue = ((IntObjectInspector) inputOI).get(o);
            result = TimestampWritableV2.longToTimestamp(longValue, intToTimestampInSeconds);
            break;
        case LONG:
            longValue = ((LongObjectInspector) inputOI).get(o);
            result = TimestampWritableV2.longToTimestamp(longValue, intToTimestampInSeconds);
            break;
        case FLOAT:
            result = TimestampUtils.doubleToTimestamp(((FloatObjectInspector) inputOI).get(o));
            break;
        case DOUBLE:
            result = TimestampUtils.doubleToTimestamp(((DoubleObjectInspector) inputOI).get(o));
            break;
        case DECIMAL:
            result = TimestampUtils.decimalToTimestamp(((HiveDecimalObjectInspector) inputOI).getPrimitiveJavaObject(o));
            break;
        case STRING:
            StringObjectInspector soi = (StringObjectInspector) inputOI;
            String s = soi.getPrimitiveJavaObject(o);
            result = getTimestampFromString(s);
            break;
        case CHAR:
        case VARCHAR:
            result = getTimestampFromString(getString(o, inputOI));
            break;
        case DATE:
            result = Timestamp.ofEpochMilli(((DateObjectInspector) inputOI).getPrimitiveWritableObject(o).get().toEpochMilli());
            break;
        case TIMESTAMP:
            result = ((TimestampObjectInspector) inputOI).getPrimitiveWritableObject(o).getTimestamp();
            break;
        case TIMESTAMPLOCALTZ:
            String tstz = inputOI.getPrimitiveWritableObject(o).toString();
            int index = tstz.indexOf(" ");
            index = tstz.indexOf(" ", index + 1);
            if (index == -1) {
                return null;
            }
            result = Timestamp.valueOf(tstz.substring(0, index));
            break;
        default:
            throw new RuntimeException("Hive 2 Internal error: unknown type: " + inputOI.getTypeName());
    }
    return result;
}
Also used : Timestamp(org.apache.hadoop.hive.common.type.Timestamp)

Example 49 with Timestamp

use of org.apache.hadoop.hive.common.type.Timestamp in project hive by apache.

the class JavaTimestampObjectInspector method set.

public Object set(Object o, TimestampWritableV2 tw) {
    if (tw == null) {
        return null;
    }
    Timestamp t = (Timestamp) o;
    t.set(tw.getTimestamp());
    return t;
}
Also used : Timestamp(org.apache.hadoop.hive.common.type.Timestamp)

Example 50 with Timestamp

use of org.apache.hadoop.hive.common.type.Timestamp in project hive by apache.

the class TestGenericUDFFromUnixTime method testTimestampDefaultTimezone.

@Test
public void testTimestampDefaultTimezone() throws HiveException {
    ObjectInspector valueLongOI = PrimitiveObjectInspectorFactory.writableLongObjectInspector;
    GenericUDFFromUnixTime udf = new GenericUDFFromUnixTime();
    ObjectInspector[] args = { valueLongOI };
    udf.initialize(args);
    Timestamp ts = Timestamp.valueOf("1470-01-01 00:00:00");
    TimestampTZ tstz = TimestampTZUtil.convert(ts, ZoneId.systemDefault());
    runAndVerify(udf, new LongWritable(tstz.getEpochSecond()), new Text("1470-01-01 00:00:00"));
    // test null values
    runAndVerify(udf, null, null);
}
Also used : TimestampTZ(org.apache.hadoop.hive.common.type.TimestampTZ) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) Text(org.apache.hadoop.io.Text) LongWritable(org.apache.hadoop.io.LongWritable) Timestamp(org.apache.hadoop.hive.common.type.Timestamp) Test(org.junit.Test)

Aggregations

Timestamp (org.apache.hadoop.hive.common.type.Timestamp)116 Test (org.junit.Test)36 TimestampWritableV2 (org.apache.hadoop.hive.serde2.io.TimestampWritableV2)32 Date (org.apache.hadoop.hive.common.type.Date)27 BytesWritable (org.apache.hadoop.io.BytesWritable)25 LongWritable (org.apache.hadoop.io.LongWritable)25 HiveDecimalWritable (org.apache.hadoop.hive.serde2.io.HiveDecimalWritable)24 Text (org.apache.hadoop.io.Text)22 HiveDecimal (org.apache.hadoop.hive.common.type.HiveDecimal)21 IntWritable (org.apache.hadoop.io.IntWritable)21 HiveChar (org.apache.hadoop.hive.common.type.HiveChar)20 HiveVarchar (org.apache.hadoop.hive.common.type.HiveVarchar)20 BooleanWritable (org.apache.hadoop.io.BooleanWritable)19 FloatWritable (org.apache.hadoop.io.FloatWritable)19 PrimitiveTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo)18 ArrayList (java.util.ArrayList)17 DateWritableV2 (org.apache.hadoop.hive.serde2.io.DateWritableV2)17 HiveIntervalDayTime (org.apache.hadoop.hive.common.type.HiveIntervalDayTime)16 List (java.util.List)15 ByteWritable (org.apache.hadoop.hive.serde2.io.ByteWritable)12