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

com.adgear.anoa.library.tools.runnable.AvroGrep Maven / Gradle / Ivy

package com.adgear.anoa.library.tools.runnable;

import com.adgear.anoa.AnoaReflectionUtils;
import com.adgear.anoa.library.tools.function.AnoaSqlWhereFilter;
import com.adgear.anoa.write.AvroConsumers;
import com.adgear.anoa.write.WriteConsumer;
import com.adgear.anoa.read.AvroStreams;

import org.apache.avro.specific.SpecificRecord;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UncheckedIOException;

public class AvroGrep implements Runnable {

  final protected Class recordClass;
  final protected String filterExpression;
  final protected InputStream inputStream;
  final protected OutputStream outputStream;

  public AvroGrep(Class recordClass,
                  String filterExpression,
                  InputStream inputStream,
                  OutputStream outputStream) {
    this.recordClass = recordClass;
    this.filterExpression = filterExpression;
    this.inputStream = inputStream;
    this.outputStream = outputStream;
  }

  static public void main(String[] args) throws Exception {
    new AvroGrep<>(AnoaReflectionUtils.getAvroClass(System.getProperty("recordClass")),
                   System.getProperty("filterExpression"),
                   System.in,
                   System.out)
        .run();
  }

  @Override
  public void run() {
    AnoaSqlWhereFilter predicate = new AnoaSqlWhereFilter<>(recordClass, filterExpression);
    try (WriteConsumer consumer = AvroConsumers.batch(recordClass, outputStream)) {
      AvroStreams.batch(recordClass, inputStream)
          .filter(predicate)
          .forEach(consumer);
    } catch (IOException e) {
      throw new UncheckedIOException(e);
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy