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());
}
}
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;
}
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;
}
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;
}
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);
}
Aggregations