Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generalise source by getting types from context #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@
<dependency>
<groupId>cascading</groupId>
<artifactId>cascading-hadoop</artifactId>
<version>2.0.4-wip-335</version>
<version>2.0.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
Expand Down
153 changes: 82 additions & 71 deletions src/main/java/com/squareup/cascading2/scheme/ProtobufScheme.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,75 +24,86 @@
* and Protocol Buffers serialized objects wrapped in BytesWritable values.
*/
public class ProtobufScheme extends SequenceFile {
private transient Message.Builder prototype;
private final String fieldName;
private final String messageClassName;

public ProtobufScheme(String fieldName, Class<? extends Message> messageClass) {
super(new Fields(fieldName));
this.fieldName = fieldName;
messageClassName = messageClass.getName();
}

@Override public void sourcePrepare(FlowProcess<JobConf> flowProcess,
SourceCall<Object[], RecordReader> sourceCall) {
}

@Override
public void sinkConfInit(FlowProcess<JobConf> flowProcess,
Tap<JobConf, RecordReader, OutputCollector> tap, JobConf conf) {
conf.setOutputKeyClass(NullWritable.class);
conf.setOutputValueClass(BytesWritable.class);

conf.setOutputFormat(SequenceFileOutputFormat.class);
}

@Override
public boolean source(FlowProcess<JobConf> flowProcess,
SourceCall<Object[], RecordReader> sourceCall) throws IOException {
// TODO: cache this BytesWritable in the context
BytesWritable value = new BytesWritable();
boolean result = sourceCall.getInput().next(NullWritable.get(), value);

if (!result) return false;

Tuple tuple = sourceCall.getIncomingEntry().getTuple();
tuple.clear();

tuple.add(getPrototype().mergeFrom(value.getBytes(), 0, value.getLength()).build());

return true;
}

private Message.Builder getPrototype() {
if (prototype == null) {
prototype = Util.builderFromMessageClass(messageClassName);
}
return prototype;
}

@Override
public void sink(FlowProcess<JobConf> flowProcess, SinkCall<Void, OutputCollector> sinkCall)
throws IOException {
TupleEntry tupleEntry = sinkCall.getOutgoingEntry();

Message message = (Message)tupleEntry.getObject(fieldName);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
message.writeTo(baos);
// TODO: cache this BytesWritable
BytesWritable outputWritable = new BytesWritable(baos.toByteArray());

sinkCall.getOutput().collect(NullWritable.get(), outputWritable);
}

@Override
public boolean equals(Object object) {
if (this == object) return true;
if (!(object instanceof ProtobufScheme)) return false;
if (!super.equals(object)) return false;

// TODO: reimplement this

return true;
}
private transient Message.Builder prototype;
private final String fieldName;
private final String messageClassName;

public ProtobufScheme(String fieldName, Class<? extends Message> messageClass) {
super(new Fields(fieldName));
this.fieldName = fieldName;
messageClassName = messageClass.getName();
}

@Override public void sourcePrepare(FlowProcess<JobConf> flowProcess,
SourceCall<Object[], RecordReader> sourceCall) {
Object[] pair = new Object[]{sourceCall.getInput().createKey(),
sourceCall.getInput().createValue()};
sourceCall.setContext( pair );
}

@Override
public void sourceCleanup( FlowProcess<JobConf> flowProcess, SourceCall<Object[], RecordReader> sourceCall )
{
sourceCall.setContext( null );
}

@Override
public void sinkConfInit(FlowProcess<JobConf> flowProcess,
Tap<JobConf, RecordReader, OutputCollector> tap, JobConf conf) {
conf.setOutputKeyClass(NullWritable.class);
conf.setOutputValueClass(BytesWritable.class);

conf.setOutputFormat(SequenceFileOutputFormat.class);
}

@Override
public boolean source(FlowProcess<JobConf> flowProcess,
SourceCall<Object[], RecordReader> sourceCall) throws IOException {
// TODO: cache this BytesWritable in the context
Object key = sourceCall.getContext()[ 0 ];
Object val = sourceCall.getContext()[ 1 ];

boolean result = sourceCall.getInput().next(key, val);
if (!result) return false;

Tuple tuple = sourceCall.getIncomingEntry().getTuple();
tuple.clear();

BytesWritable value = (BytesWritable)val;
tuple.add(getPrototype().mergeFrom(value.getBytes(), 0, value.getLength()).build());
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry about the auto-indent making it all diff'ed. the notable change is here.


return true;
}

private Message.Builder getPrototype() {
if (prototype == null) {
prototype = Util.builderFromMessageClass(messageClassName);
}
return prototype;
}

@Override
public void sink(FlowProcess<JobConf> flowProcess, SinkCall<Void, OutputCollector> sinkCall)
throws IOException {
TupleEntry tupleEntry = sinkCall.getOutgoingEntry();

Message message = (Message)tupleEntry.getObject(fieldName);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
message.writeTo(baos);
// TODO: cache this BytesWritable
BytesWritable outputWritable = new BytesWritable(baos.toByteArray());

sinkCall.getOutput().collect(NullWritable.get(), outputWritable);
}

@Override
public boolean equals(Object object) {
if (this == object) return true;
if (!(object instanceof ProtobufScheme)) return false;
if (!super.equals(object)) return false;

// TODO: reimplement this

return true;
}
}