use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaStringObjectInspector in project hive by apache.
the class ColumnarSerDe method serialize.
/**
* Serialize a row of data.
*
* @param obj
* The row object
* @param objInspector
* The ObjectInspector for the row object
* @return The serialized Writable object
* @see AbstractSerDe#serialize(Object, ObjectInspector)
*/
@Override
public Writable serialize(Object obj, ObjectInspector objInspector) throws SerDeException {
if (objInspector.getCategory() != Category.STRUCT) {
throw new SerDeException(getClass().toString() + " can only serialize struct types, but we got: " + objInspector.getTypeName());
}
// Prepare the field ObjectInspectors
StructObjectInspector soi = (StructObjectInspector) objInspector;
List<? extends StructField> fields = soi.getAllStructFieldRefs();
List<Object> list = soi.getStructFieldsDataAsList(obj);
List<? extends StructField> declaredFields = (serdeParams.getRowTypeInfo() != null && ((StructTypeInfo) serdeParams.getRowTypeInfo()).getAllStructFieldNames().size() > 0) ? ((StructObjectInspector) getObjectInspector()).getAllStructFieldRefs() : null;
try {
// used for avoid extra byte copy
serializeStream.reset();
serializedSize = 0;
int count = 0;
// Serialize each field
for (int i = 0; i < fields.size(); i++) {
// Get the field objectInspector and the field object.
ObjectInspector foi = fields.get(i).getFieldObjectInspector();
Object f = (list == null ? null : list.get(i));
if (declaredFields != null && i >= declaredFields.size()) {
throw new SerDeException("Error: expecting " + declaredFields.size() + " but asking for field " + i + "\n" + "data=" + obj + "\n" + "tableType=" + serdeParams.getRowTypeInfo().toString() + "\n" + "dataType=" + TypeInfoUtils.getTypeInfoFromObjectInspector(objInspector));
}
// delimited way.
if (!foi.getCategory().equals(Category.PRIMITIVE) && (declaredFields == null || declaredFields.get(i).getFieldObjectInspector().getCategory().equals(Category.PRIMITIVE))) {
LazySimpleSerDe.serialize(serializeStream, SerDeUtils.getJSONString(f, foi), PrimitiveObjectInspectorFactory.javaStringObjectInspector, serdeParams.getSeparators(), 1, serdeParams.getNullSequence(), serdeParams.isEscaped(), serdeParams.getEscapeChar(), serdeParams.getNeedsEscape());
} else {
LazySimpleSerDe.serialize(serializeStream, f, foi, serdeParams.getSeparators(), 1, serdeParams.getNullSequence(), serdeParams.isEscaped(), serdeParams.getEscapeChar(), serdeParams.getNeedsEscape());
}
field[i].set(serializeStream.getData(), count, serializeStream.getLength() - count);
count = serializeStream.getLength();
}
serializedSize = serializeStream.getLength();
lastOperationSerialize = true;
lastOperationDeserialize = false;
} catch (IOException e) {
throw new SerDeException(e);
}
return serializeCache;
}
use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaStringObjectInspector in project hive by apache.
the class TestGenericUDFTrunc method testDateWritableToDateWithMonthFormat.
public void testDateWritableToDateWithMonthFormat() throws HiveException {
GenericUDFTrunc udf = new GenericUDFTrunc();
ObjectInspector valueOI0 = PrimitiveObjectInspectorFactory.writableDateObjectInspector;
ObjectInspector valueOI1 = PrimitiveObjectInspectorFactory.javaStringObjectInspector;
ObjectInspector[] initArgs = { valueOI0, valueOI1 };
DeferredObject valueObjFmt = new DeferredJavaObject(new Text("MM"));
DeferredObject valueObj0;
DeferredObject[] evalArgs;
// test date string
valueObj0 = new DeferredJavaObject(new DateWritable(Date.valueOf("2014-01-01")));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2014-01-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new DateWritable(Date.valueOf("2014-01-14")));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2014-01-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new DateWritable(Date.valueOf("2014-01-31")));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2014-01-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new DateWritable(Date.valueOf("2014-02-02")));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2014-02-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new DateWritable(Date.valueOf("2014-02-28")));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2014-02-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new DateWritable(Date.valueOf("2016-02-03")));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2016-02-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new DateWritable(Date.valueOf("2016-02-28")));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2016-02-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new DateWritable(Date.valueOf("2016-02-29")));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2016-02-01", udf, initArgs, evalArgs);
}
use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaStringObjectInspector in project hive by apache.
the class TestGenericUDFTrunc method testDateWritableToDateWithYearFormat.
public void testDateWritableToDateWithYearFormat() throws HiveException {
GenericUDFTrunc udf = new GenericUDFTrunc();
ObjectInspector valueOI0 = PrimitiveObjectInspectorFactory.writableDateObjectInspector;
ObjectInspector valueOI1 = PrimitiveObjectInspectorFactory.javaStringObjectInspector;
ObjectInspector[] initArgs = { valueOI0, valueOI1 };
DeferredObject valueObjFmt = new DeferredJavaObject(new Text("YY"));
DeferredObject valueObj0;
DeferredObject[] evalArgs;
// test date string
valueObj0 = new DeferredJavaObject(new DateWritable(Date.valueOf("2014-01-01")));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2014-01-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new DateWritable(Date.valueOf("2014-01-14")));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2014-01-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new DateWritable(Date.valueOf("2014-01-31")));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2014-01-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new DateWritable(Date.valueOf("2014-02-02")));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2014-01-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new DateWritable(Date.valueOf("2014-02-28")));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2014-01-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new DateWritable(Date.valueOf("2016-02-03")));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2016-01-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new DateWritable(Date.valueOf("2016-02-28")));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2016-01-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new DateWritable(Date.valueOf("2016-02-29")));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2016-01-01", udf, initArgs, evalArgs);
}
use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaStringObjectInspector in project hive by apache.
the class TestGenericUDFTrunc method testStringToDateWithMonthFormat.
public void testStringToDateWithMonthFormat() throws HiveException {
GenericUDFTrunc udf = new GenericUDFTrunc();
ObjectInspector valueOI0 = PrimitiveObjectInspectorFactory.javaStringObjectInspector;
ObjectInspector valueOI1 = PrimitiveObjectInspectorFactory.javaStringObjectInspector;
ObjectInspector[] initArgs = { valueOI0, valueOI1 };
DeferredObject valueObjFmt = new DeferredJavaObject(new Text("MONTH"));
DeferredObject valueObj0;
DeferredObject[] evalArgs;
// test date string
valueObj0 = new DeferredJavaObject(new Text("2014-01-01"));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2014-01-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new Text("2014-01-14"));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2014-01-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new Text("2014-01-31"));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2014-01-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new Text("2014-02-02"));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2014-02-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new Text("2014-02-28"));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2014-02-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new Text("2016-02-03"));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2016-02-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new Text("2016-02-28"));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2016-02-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new Text("2016-02-29"));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2016-02-01", udf, initArgs, evalArgs);
// test timestamp string
valueObj0 = new DeferredJavaObject(new Text("2014-01-01 10:30:45"));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2014-01-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new Text("2014-01-14 10:30:45"));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2014-01-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new Text("2014-01-31 10:30:45"));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2014-01-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new Text("2014-02-02 10:30:45"));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2014-02-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new Text("2014-02-28 10:30:45"));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2014-02-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new Text("2016-02-03 10:30:45"));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2016-02-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new Text("2016-02-28 10:30:45"));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2016-02-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new Text("2016-02-29 10:30:45"));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2016-02-01", udf, initArgs, evalArgs);
}
use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaStringObjectInspector in project hive by apache.
the class HBaseRowSerializer method serializeKeyField.
byte[] serializeKeyField(Object keyValue, StructField keyField, ColumnMapping keyMapping) throws IOException {
if (keyValue == null) {
throw new IOException("HBase row key cannot be NULL");
}
ObjectInspector keyFieldOI = keyField.getFieldObjectInspector();
if (!keyFieldOI.getCategory().equals(ObjectInspector.Category.PRIMITIVE) && keyMapping.isCategory(ObjectInspector.Category.PRIMITIVE)) {
// we always serialize the String type using the escaped algorithm for LazyString
return serialize(SerDeUtils.getJSONString(keyValue, keyFieldOI), PrimitiveObjectInspectorFactory.javaStringObjectInspector, 1, false);
}
// use the serialization option switch to write primitive values as either a variable
// length UTF8 string or a fixed width bytes if serializing in binary format
boolean writeBinary = keyMapping.binaryStorage.get(0);
return serialize(keyValue, keyFieldOI, 1, writeBinary);
}
Aggregations