-
Notifications
You must be signed in to change notification settings - Fork 465
PARQUET-79: add a streaming Thrift API, to enable processing the metadata as we read it and skipping unnecessary fields. #8
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
Closed
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
71c85de
first stab
julienledem 6368bdc
streaming thrift reader
julienledem bee937a
refactor, cleanup
julienledem 9be786a
added simple readMetaData method
julienledem 958726f
javadoc; fix apis
julienledem 8dd801e
Merge branch 'master' into streaming_metadata
julienledem cb386ce
RIP TypedConsumerProvider, @tsdeng did not like you
julienledem e5c78fc
#simplify
julienledem a58913d
rename add to consume
julienledem 621769a
cleanup refactoring
julienledem File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| package parquet.format.event; | ||
|
|
||
| import static java.util.Collections.unmodifiableMap; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import org.apache.thrift.TBase; | ||
| import org.apache.thrift.TException; | ||
| import org.apache.thrift.TFieldIdEnum; | ||
| import org.apache.thrift.protocol.TList; | ||
| import org.apache.thrift.protocol.TProtocol; | ||
| import org.apache.thrift.protocol.TProtocolUtil; | ||
|
|
||
| import parquet.format.event.Consumers.Consumer; | ||
| import parquet.format.event.TypedConsumer.BoolConsumer; | ||
| import parquet.format.event.TypedConsumer.ListConsumer; | ||
| import parquet.format.event.TypedConsumer.StructConsumer; | ||
|
|
||
| /** | ||
| * Entry point for reading thrift in a streaming fashion | ||
| * | ||
| * @author Julien Le Dem | ||
| * | ||
| */ | ||
| public class Consumers { | ||
|
|
||
| /** | ||
| * To consume objects coming from a DelegatingFieldConsumer | ||
| * @author Julien Le Dem | ||
| * | ||
| * @param <T> the type of consumed objects | ||
| */ | ||
| public static interface Consumer<T> { | ||
| void consume(T t); | ||
| } | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add a comment: The delegatingFieldConsumer delegates addField call to corresponding TypedConsumer? |
||
| /** | ||
| * Delegates reading the field to TypedConsumers. | ||
| * There is one TypedConsumer per thrift type. | ||
| * use {@link DelegatingFieldConsumer#onField(TFieldIdEnum, BoolConsumer)} et al. to consume specific thrift fields. | ||
| * @see Consumers#fieldConsumer() | ||
| * @author Julien Le Dem | ||
| * | ||
| */ | ||
| public static class DelegatingFieldConsumer implements FieldConsumer { | ||
|
|
||
| private final Map<Short, TypedConsumer> contexts; | ||
| private final FieldConsumer defaultFieldEventConsumer; | ||
|
|
||
| private DelegatingFieldConsumer(FieldConsumer defaultFieldEventConsumer, Map<Short, TypedConsumer> contexts) { | ||
| this.defaultFieldEventConsumer = defaultFieldEventConsumer; | ||
| this.contexts = unmodifiableMap(contexts); | ||
| } | ||
|
|
||
| private DelegatingFieldConsumer() { | ||
| this(new SkippingFieldConsumer()); | ||
| } | ||
|
|
||
| private DelegatingFieldConsumer(FieldConsumer defaultFieldEventConsumer) { | ||
| this(defaultFieldEventConsumer, Collections.<Short, TypedConsumer>emptyMap()); | ||
| } | ||
|
|
||
| public DelegatingFieldConsumer onField(TFieldIdEnum e, TypedConsumer typedConsumer) { | ||
| Map<Short, TypedConsumer> newContexts = new HashMap<Short, TypedConsumer>(contexts); | ||
| newContexts.put(e.getThriftFieldId(), typedConsumer); | ||
| return new DelegatingFieldConsumer(defaultFieldEventConsumer, newContexts); | ||
| } | ||
|
|
||
| @Override | ||
| public void consumeField( | ||
| TProtocol protocol, EventBasedThriftReader reader, | ||
| short id, byte type) throws TException { | ||
| TypedConsumer delegate = contexts.get(id); | ||
| if (delegate != null) { | ||
| delegate.read(protocol, reader, type); | ||
| } else { | ||
| defaultFieldEventConsumer.consumeField(protocol, reader, id, type); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * call onField on the resulting DelegatingFieldConsumer to handle individual fields | ||
| * @return a new DelegatingFieldConsumer | ||
| */ | ||
| public static DelegatingFieldConsumer fieldConsumer() { | ||
| return new DelegatingFieldConsumer(); | ||
| } | ||
|
|
||
| /** | ||
| * To consume a list of elements | ||
| * @param c the type of the list content | ||
| * @param consumer the consumer that will receive the list | ||
| * @return a ListConsumer that can be passed to the DelegatingFieldConsumer | ||
| */ | ||
| public static <T extends TBase<T,? extends TFieldIdEnum>> ListConsumer listOf(Class<T> c, final Consumer<List<T>> consumer) { | ||
| class ListConsumer implements Consumer<T> { | ||
| List<T> list; | ||
| @Override | ||
| public void consume(T t) { | ||
| list.add(t); | ||
| } | ||
| } | ||
| final ListConsumer co = new ListConsumer(); | ||
| return new DelegatingListElementsConsumer(struct(c, co)) { | ||
| @Override | ||
| public void consumeList(TProtocol protocol, | ||
| EventBasedThriftReader reader, TList tList) throws TException { | ||
| co.list = new ArrayList<T>(); | ||
| super.consumeList(protocol, reader, tList); | ||
| consumer.consume(co.list); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * To consume list elements one by one | ||
| * @param consumer the consumer that will read the elements | ||
| * @return a ListConsumer that can be passed to the DelegatingFieldConsumer | ||
| */ | ||
| public static ListConsumer listElementsOf(TypedConsumer consumer) { | ||
| return new DelegatingListElementsConsumer(consumer); | ||
| } | ||
|
|
||
| public static <T extends TBase<T,? extends TFieldIdEnum>> StructConsumer struct(final Class<T> c, final Consumer<T> consumer) { | ||
| return new TBaseStructConsumer<T>(c, consumer); | ||
| } | ||
| } | ||
|
|
||
| class SkippingFieldConsumer implements FieldConsumer { | ||
| @Override | ||
| public void consumeField(TProtocol protocol, EventBasedThriftReader reader, short id, byte type) throws TException { | ||
| TProtocolUtil.skip(protocol, type); | ||
| } | ||
| } | ||
|
|
||
| class DelegatingListElementsConsumer extends ListConsumer { | ||
|
|
||
| private TypedConsumer elementConsumer; | ||
|
|
||
| protected DelegatingListElementsConsumer(TypedConsumer consumer) { | ||
| this.elementConsumer = consumer; | ||
| } | ||
|
|
||
| @Override | ||
| public void consumeElement(TProtocol protocol, EventBasedThriftReader reader, byte elemType) throws TException { | ||
| elementConsumer.read(protocol, reader, elemType); | ||
| } | ||
| } | ||
| class TBaseStructConsumer<T extends TBase<T, ? extends TFieldIdEnum>> extends StructConsumer { | ||
|
|
||
| private final Class<T> c; | ||
| private Consumer<T> consumer; | ||
|
|
||
| public TBaseStructConsumer(Class<T> c, Consumer<T> consumer) { | ||
| this.c = c; | ||
| this.consumer = consumer; | ||
| } | ||
|
|
||
| @Override | ||
| public void consumeStruct(TProtocol protocol, EventBasedThriftReader reader) throws TException { | ||
| T o = newObject(); | ||
| o.read(protocol); | ||
| consumer.consume(o); | ||
| } | ||
|
|
||
| protected T newObject() { | ||
| try { | ||
| return c.newInstance(); | ||
| } catch (InstantiationException e) { | ||
| throw new RuntimeException(c.getName(), e); | ||
| } catch (IllegalAccessException e) { | ||
| throw new RuntimeException(c.getName(), e); | ||
| } | ||
| } | ||
|
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems the Consumers here is just used as a namespace. So should it be a package name?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missed, it's providing some static util methods