Search in sources :

Example 1 with Consumer

use of org.apache.parquet.format.event.Consumers.Consumer in project parquet-format by apache.

the class Util method readFileMetaData.

public static void readFileMetaData(InputStream from, final FileMetaDataConsumer consumer, boolean skipRowGroups) throws IOException {
    try {
        DelegatingFieldConsumer eventConsumer = fieldConsumer().onField(VERSION, new I32Consumer() {

            @Override
            public void consume(int value) {
                consumer.setVersion(value);
            }
        }).onField(SCHEMA, listOf(SchemaElement.class, new Consumer<List<SchemaElement>>() {

            @Override
            public void consume(List<SchemaElement> schema) {
                consumer.setSchema(schema);
            }
        })).onField(NUM_ROWS, new I64Consumer() {

            @Override
            public void consume(long value) {
                consumer.setNumRows(value);
            }
        }).onField(KEY_VALUE_METADATA, listElementsOf(struct(KeyValue.class, new Consumer<KeyValue>() {

            @Override
            public void consume(KeyValue kv) {
                consumer.addKeyValueMetaData(kv);
            }
        }))).onField(CREATED_BY, new StringConsumer() {

            @Override
            public void consume(String value) {
                consumer.setCreatedBy(value);
            }
        });
        if (!skipRowGroups) {
            eventConsumer = eventConsumer.onField(ROW_GROUPS, listElementsOf(struct(RowGroup.class, new Consumer<RowGroup>() {

                @Override
                public void consume(RowGroup rowGroup) {
                    consumer.addRowGroup(rowGroup);
                }
            })));
        }
        new EventBasedThriftReader(protocol(from)).readStruct(eventConsumer);
    } catch (TException e) {
        throw new IOException("can not read FileMetaData: " + e.getMessage(), e);
    }
}
Also used : TException(org.apache.thrift.TException) EventBasedThriftReader(org.apache.parquet.format.event.EventBasedThriftReader) I64Consumer(org.apache.parquet.format.event.TypedConsumer.I64Consumer) I32Consumer(org.apache.parquet.format.event.TypedConsumer.I32Consumer) IOException(java.io.IOException) StringConsumer(org.apache.parquet.format.event.TypedConsumer.StringConsumer) Consumers.fieldConsumer(org.apache.parquet.format.event.Consumers.fieldConsumer) StringConsumer(org.apache.parquet.format.event.TypedConsumer.StringConsumer) DelegatingFieldConsumer(org.apache.parquet.format.event.Consumers.DelegatingFieldConsumer) I64Consumer(org.apache.parquet.format.event.TypedConsumer.I64Consumer) I32Consumer(org.apache.parquet.format.event.TypedConsumer.I32Consumer) Consumer(org.apache.parquet.format.event.Consumers.Consumer) DelegatingFieldConsumer(org.apache.parquet.format.event.Consumers.DelegatingFieldConsumer) List(java.util.List)

Example 2 with Consumer

use of org.apache.parquet.format.event.Consumers.Consumer in project parquet-format by apache.

the class TBaseStructConsumer method listOf.

/**
 * 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);
        }
    };
}
Also used : TList(org.apache.thrift.protocol.TList) ListConsumer(org.apache.parquet.format.event.TypedConsumer.ListConsumer) BoolConsumer(org.apache.parquet.format.event.TypedConsumer.BoolConsumer) StructConsumer(org.apache.parquet.format.event.TypedConsumer.StructConsumer) Consumer(org.apache.parquet.format.event.Consumers.Consumer) TProtocol(org.apache.thrift.protocol.TProtocol) ListConsumer(org.apache.parquet.format.event.TypedConsumer.ListConsumer) ArrayList(java.util.ArrayList) TList(org.apache.thrift.protocol.TList) List(java.util.List)

Aggregations

List (java.util.List)2 Consumer (org.apache.parquet.format.event.Consumers.Consumer)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 DelegatingFieldConsumer (org.apache.parquet.format.event.Consumers.DelegatingFieldConsumer)1 Consumers.fieldConsumer (org.apache.parquet.format.event.Consumers.fieldConsumer)1 EventBasedThriftReader (org.apache.parquet.format.event.EventBasedThriftReader)1 BoolConsumer (org.apache.parquet.format.event.TypedConsumer.BoolConsumer)1 I32Consumer (org.apache.parquet.format.event.TypedConsumer.I32Consumer)1 I64Consumer (org.apache.parquet.format.event.TypedConsumer.I64Consumer)1 ListConsumer (org.apache.parquet.format.event.TypedConsumer.ListConsumer)1 StringConsumer (org.apache.parquet.format.event.TypedConsumer.StringConsumer)1 StructConsumer (org.apache.parquet.format.event.TypedConsumer.StructConsumer)1 TException (org.apache.thrift.TException)1 TList (org.apache.thrift.protocol.TList)1 TProtocol (org.apache.thrift.protocol.TProtocol)1