Something mysterious is happening for me:
What I wanted to do:
1. Save a Protocol Buffers object as SequenceFile format.
2. Read this SequenceFile text and extract the field that I need.
The mystery part is: One field that I wanted to retrieve is always null.
Product_Perf
is the field that I wanted to extract from SequencFiles that is always missing.
Here's my protocol buffers schema:
message ProductJoin {
Signals signals = 1;
int64 id = 2;
}
message Signals {
ProductPerf product_perf = 1;
}
message ProductPerf {
int64 impressions = 1;
}
Here's how I save the protocol buffers as SequenceFiles:
JavaPairRDD<BytesWritable, BytesWritable> bytesWritableJavaPairRdd =
flattenedPjPairRdd.mapToPair(
new PairFunction<Tuple2<Long, ProductJoin>, BytesWritable, BytesWritable>() {
@Override
public Tuple2<BytesWritable, BytesWritable> call(Tuple2<Long, ProductJoin> longProductJoinTuple2) throws Exception {
return new Tuple2<>(
new BytesWritable(longProductJoinTuple2._2().getId().getBytes()),
new BytesWritable(longProductJoinTuple2._2().toByteArray()));
}
}
//dump SequenceFiles
bytesWritableJavaPairRdd.saveAsHadoopFile(
"/tmp/path/",
BytesWritable.class,
BytesWritable.class,
SequenceFileOutputFormat.class
);
Below is the code that how I read the SequenceFile:
sparkSession.sparkContext()
.sequenceFile("tmp/path", BytesWritable.class, BytesWritable.class)
.toJavaRDD()
.mapToPair(
bytesWritableBytesWritableTuple2 -> {
Method parserMethod = clazz.getDeclaredMethod("parser");
Parser<T> parser = (Parser<T>) parserMethod.invoke(null);
return new Tuple2<>(
Text.decode(bytesWritableBytesWritableTuple2._1().getBytes()),
parser.parseFrom(bytesWritableBytesWritableTuple2._2().getBytes()));
}
);