private static final Logger LOG = LoggerFactory.getLogger(DatasetOutputFormat.class);
@Override
public RecordWriter<Void, Text> getRecordWriter(FileSystem ignored, final JobConf jobConf, String name,
Progressable progress) throws IOException {
final RecordWritable recordWritable = DatasetAccessor.getRecordWritable(jobConf);
final Type recordType = recordWritable.getRecordType();
return new RecordWriter<Void, Text>() {
@Override
public void write(Void key, Text value) throws IOException {
if (value == null) {
throw new IOException("Writable value is null.");
}
recordWritable.write(new Gson().fromJson(value.toString(), recordType));
}
@Override
public void close(Reporter reporter) throws IOException {
try {
if (recordWritable instanceof TransactionAware) {
try {
// Commit changes made to the dataset being written
// NOTE: because the transaction wrapping a Hive query is a long running one,
// we don't track changes and don't check conflicts - we can just commit the changes.
((TransactionAware) recordWritable).commitTx();
} catch (Exception e) {
LOG.error("Could not commit changes for table {}", recordWritable);
throw new IOException(e);
}
}
} finally {
recordWritable.close();
}
}
};
}