All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.spredfast.kafka.connect.s3.RecordReader Maven / Gradle / Ivy

There is a newer version: 0.5.0
Show newest version
package com.spredfast.kafka.connect.s3;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.NoSuchElementException;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.connect.errors.DataException;

/**
 * Record oriented version of RecordsReader. Can be used to simplify your implementation.
 */
public interface RecordReader extends S3RecordsReader {

	ConsumerRecord read(String topic, int partition, long offset, BufferedInputStream data) throws IOException;

	static RecordReader of(RecordReader r) { return r; }

	@Override
	default Iterator> readAll(final String topic, final int partition, final InputStream inputStream, final long startOffset) {
		return new Iterator>() {
			ConsumerRecord next;

			final BufferedInputStream buffered = new BufferedInputStream(inputStream);

			long offset = startOffset;

			@Override
			public boolean hasNext() {
				try {
					if (next == null) {
						next = read(topic, partition, offset++, buffered);
					}
				} catch (IOException e) {
					throw new DataException(e);
				}
				return next != null;
			}

			@Override
			public ConsumerRecord next() {
				if (!hasNext()) {
					throw new NoSuchElementException();
				}
				final ConsumerRecord record = this.next;
				next = null;
				return record;
			}

			@Override
			public void remove() {
				throw new UnsupportedOperationException();
			}
		};
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy