use of org.apache.crunch.fn.CompositeMapFn in project crunch by cloudera.
the class TextFileReaderFactory method read.
@Override
public Iterator<T> read(FileSystem fs, Path path) {
MapFn mapFn = null;
if (String.class.equals(ptype.getTypeClass())) {
mapFn = IdentityFn.getInstance();
} else {
// Check for a composite MapFn for the PType.
// Note that this won't work for Avro-- need to solve that.
MapFn input = ptype.getInputMapFn();
if (input instanceof CompositeMapFn) {
mapFn = ((CompositeMapFn) input).getSecond();
}
}
mapFn.setConfigurationForTest(conf);
mapFn.initialize();
FSDataInputStream is = null;
try {
is = fs.open(path);
} catch (IOException e) {
LOG.info("Could not read path: " + path, e);
return Iterators.emptyIterator();
}
final BufferedReader reader = new BufferedReader(new InputStreamReader(is));
final MapFn<String, T> iterMapFn = mapFn;
return new UnmodifiableIterator<T>() {
private String nextLine;
@Override
public boolean hasNext() {
try {
return (nextLine = reader.readLine()) != null;
} catch (IOException e) {
LOG.info("Exception reading text file stream", e);
return false;
}
}
@Override
public T next() {
return iterMapFn.map(nextLine);
}
};
}
use of org.apache.crunch.fn.CompositeMapFn in project crunch by cloudera.
the class Writables method derived.
public static <S, T> PType<T> derived(Class<T> clazz, MapFn<S, T> inputFn, MapFn<T, S> outputFn, PType<S> base) {
WritableType<S, ?> wt = (WritableType<S, ?>) base;
MapFn input = new CompositeMapFn(wt.getInputMapFn(), inputFn);
MapFn output = new CompositeMapFn(outputFn, wt.getOutputMapFn());
return new WritableType(clazz, wt.getSerializationClass(), input, output, base.getSubTypes().toArray(new PType[0]));
}
Aggregations