Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
*
* Input Tuples are expected to contain field values in order {@code (f1, f2, f3)}. Tuples may
* contain fewer values than Thrift struct fields (e.g. only {@code (f1, f2)} in the prior example);
* Any remaining fields will be left unset.
*/
public class PigToThrift> {
public static final Logger LOG = LoggerFactory.getLogger(PigToThrift.class);
private TStructDescriptor structDesc;
public static > PigToThrift newInstance(Class tClass) {
return new PigToThrift(tClass);
}
public static > PigToThrift newInstance(TypeRef typeRef) {
return new PigToThrift(typeRef.getRawClass());
}
public PigToThrift(Class tClass) {
structDesc = TStructDescriptor.getInstance(tClass);
// may be TODO : compare the schemas to catch errors early.
}
@SuppressWarnings("unchecked")
public T getThriftObject(Tuple tuple) {
return (T)toThrift(structDesc, tuple);
}
/**
* Construct a Thrift object from the tuple.
*/
@SuppressWarnings("unchecked")
private static TBase, ?> toThrift(TStructDescriptor tDesc, Tuple tuple) {
int size = tDesc.getFields().size();
int tupleSize = tuple.size();
@SuppressWarnings("rawtypes")
TBase tObj = newTInstance(tDesc.getThriftClass());
for(int i = 0; i)pigValue);
case TType.SET:
return toThriftSet(thriftField.getSetElemField(), (DataBag) pigValue);
case TType.LIST:
return toThriftList(thriftField.getListElemField(), (DataBag)pigValue);
case TType.ENUM:
return toThriftEnum(thriftField, (String) pigValue);
default:
// standard types : I32, I64, DOUBLE, etc.
return pigValue;
}
} catch (Exception e) {
// mostly a schema mismatch.
LOG.warn(String.format(
"Failed to set field '%s' of type '%s' with value '%s' of type '%s'",
thriftField.getName(), ThriftUtils.getFieldValueType(thriftField).getName(),
pigValue, pigValue.getClass().getName()), e);
}
return null;
}
/* TType.STRING could be either a DataByteArray or a String */
private static Object toStringType(Object value) {
if (value instanceof String) {
return value;
} else if (value instanceof DataByteArray) {
byte[] buf = ((DataByteArray)value).get();
// mostly there is no need to copy.
return ByteBuffer.wrap(Arrays.copyOf(buf, buf.length));
}
return null;
}
private static Map